Copy object in Google Cloud Storage - java

I'm using the Java client for Google Cloud Platform to copy an object from inside a bucket to a different place in the same bucket.
To do that I followed the instructions here, but I'm getting a GoogleJsonResponseException 404 Bad Request:
class com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Required",
"reason" : "required"
} ],
"message" : "Required"
}
This is my code:
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("key.json"))
.createScoped(Collections.singleton(StorageScopes.CLOUD_PLATFORM));
storageClient = new Storage.Builder(httpTransport, jsonFactory, credential).build();
StorageObject newObject = new StorageObject();
newObject.setName(newName);
Storage.Objects.Copy request = storageClient.objects().copy(bucketName, objectName, bucketName, newName, newObject);
request.setDestinationPredefinedAcl("publicread");
request.execute();
So far this is the only operation that fails, upload, download, getLength and a few others work just fine.
Update:
I manage to make it work removing this line:
request.setDestinationPredefinedAcl("publicread");
Anyway I would like to know what's wrong since in the future I would like to be able to chose if the new object is public or private.

I had the exact error when trying to copy an object from one place to another. The solution is to set newObject as null:
Storage.Objects.Copy request = storageClient.objects().copy(bucketName, objectName, bucketName, newName, null);

Related

How to retrieve the subscriber list from a channel on YouTube?

I am trying to retrieve a subscriber list from a channel that it is not mine. If I go to a channel on YouTube the subscriber list is visible to the public. When I try to use the API to retrieve the list:
YouTube.Subscriptions.List list = subscriptions.list("snippet, contentDetails");
list.setChannelId(channelID);
SubscriptionListResponse searchResponse = list.execute();
List<Subscription> searchResultList = searchResponse.getItems();
for(Subscription result : searchResultList){
SubscriptionSnippet snippet = result.getSnippet();
ResourceId resourceId = snippet.getResourceId();
String id = resourceId.getChannelId();
dbConnection.addYouTubeRelationship(channel, id);
}
I get the following error:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "youtube.subscription",
"message" : "The requester is not allowed to access the requested subscriptions.",
"reason" : "subscriptionForbidden"
} ],
"message" : "The requester is not allowed to access the requested subscriptions."
}
I am using an API-key based identification. Please, let me know if and how I can obtain the subscription list from the channels on YouTube.
Thank you.

400 Error even if email exists in firebase

I am trying user authentication with firebase in java using spring framework. When ever I try to authenticate a user using the provided emailId and password I am getting a 400 Error, but the emailId does exist in the fireBase authentication as well as in the firebase realtime database page. I am also able to retrieve data using uuid which is available in fireBase authentication web page.
My code:
String googleAuthUrl =
env.getProperty("google.auth.identity.toolkit.url") +
env.getProperty("google.auth.identity.toolkit.key");
Client client = Client.create();
WebResource webResource = client.resource(googleAuthUrl);
ClientResponse response = webResource.accept("application/json").type("application/json")
.post(ClientResponse.class, inputJson);
Integer responseCode = response.getStatus();
String responseBody = response.getEntity(String.class);
logger.info("Google response : {}", responseBody);
// ...
My input :
{
"email": "abc#gmail.com",
"password":"test123"
}
Exception encountered:
Google response : {
"error": {
"code": 400,
"message": "EMAIL_NOT_FOUND",
"errors": [
{
"message": "EMAIL_NOT_FOUND",
"domain": "global",
"reason": "invalid"
}
]
}
}
Can anyone explain the reason for this exception and how to overcome the problem ! Instead of making a REST call is there an SDK available for user authentication ?
I just had a similar problem with same library but in flutter.
The problem was that I was sending the email with a white space at the end...
So, I don't know if you solved it but a trim() function can solved it, if is the case sure

Listing teamdrive in google drive sdk with service account

I am trying to list the team drives using service account (domain wide delegation is enabled), when i do so with just the "teamdrives().list()" i am getting a blank response.
{
"kind" : "drive#teamDriveList",
"teamDrives" : [ ]
}
If i use "teamdrives().list().setUseDomainAdminAccess(true).setQ("name='Test'")" or "teamdrives().list().setUseDomainAdminAccess(true)" i am getting below error.
400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"location" : "q",
"locationType" : "parameter",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
}
Below is the code block,
InputStream in = new FileInputStream("client_secret.json");
new GoogleCredential();
GoogleCredential credential = GoogleCredential
.fromStream(in)
.createScoped(SCOPES);
Drive _service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
tdllist = _service.teamdrives().list().setUseDomainAdminAccess(true).setQ("name='Test'");
TeamDriveList _tdl = tdllist.execute();
Hoping someone will point what i am doing wrong or an efficient way to do this.
Thanks in advance.
Thanks and Regards
KKG
Update
After some research got to know that "teamdrives().list()" is listing the teamdrive that the user has access to, so i guess that part is working as it is suppose to. But the remaining is still a mystery.

Appending values to a google sheet

What am i doing wrong? I wanna append values to a sheet:
String valueInputOption = "USER_ENTERED";
String range = "1:1";
String ranges = "Student!2:2";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
AppendValuesResponse appendValuesResponse = service.spreadsheets()
.values()
.append(spreadsheetId, ranges, response)
.setValueInputOption(valueInputOption)
.execute();
but i get exception:
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Request range[Student!2:2] does not match value's
range[Student!A1:Z1]",
"reason" : "badRequest"
} ],
"message" : "Request range[Student!2:2] does not match value's
range[Student!A1:Z1]",
"status" : "INVALID_ARGUMENT"
}
i dont know why my range is wrong...
Check the Reading a single range and Appending values and it should demonstrate how to use the API properly. It seems the error stems from using 2 different ranges. Try to use only one to get rid of this error and work your way from there.

how to compose a request google cloud storage (for parallel upload) using java

I'm trying to upload a file (500 MB) to the google cloud storage. (java)
I created a List of Soource objects and I need to add ObjectPreconditions , ifGenerationMatch () to each of the object in that list.
I don't understand how to use it.
the errors I got from google were not informative at all.
StorageObject metadata = new StorageObject()
.setContentType("text/plain");
List<SourceObjects> sourceObjects = new ArrayList<SourceObjects>();
for (int i = 0; i <= chunkNumber; i++) {
sourceObjects.add( new SourceObjects().setName(objectName + ".chunk" + i) );
}
ComposeRequest composeReq = new ComposeRequest()
.setSourceObjects(sourceObjects)
.setDestination(metadata);
storage.objects().compose(bucketName, fileName,composeReq).execute();
and this is the error:
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Not Found",
"reason" : "notFound"
} ],
"message" : "Not Found"

Categories

Resources