moondaisy
moondaisy

Reputation: 4491

Copy object in Google Cloud Storage

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.

Upvotes: 2

Views: 1546

Answers (1)

Gini
Gini

Reputation: 53

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);

Upvotes: 1

Related Questions