7

I have a google-storage java client.

I want to rename a folder on the cloud.

It there a way to do it?

I saw the update post but i'm not sure how to change the name meta data.

here is my try but i don't know what to fill in "entity" and there is no oac.setName()

public void renameDirectory(String oldPath, String newName) throws IOException {

    final Storage gsClient = GCSlientFactory.get(PromptoConfig.s.GCP_PROJECT_ID).getGSClient();
    final URI uri = URI.create(oldPath);

    ObjectAccessControl oac = new ObjectAccessControl();
    oac.setId("newName");

    final Storage.ObjectAccessControls.Update update = gsClient.objectAccessControls().update(BUCKET_NAME, uri.toString().replace("gs://"+BUCKET_NAME+"/", ""), "", oac);
    update.execute();
}

and also:

final Storage gsClient = GCSlientFactory.get(PromptoConfig.s.GCP_PROJECT_ID).getGSClient();
final URI uri = URI.create(oldPath);

ObjectAccessControl oac = new ObjectAccessControl();
oac.set("name", newName);

final Storage.ObjectAccessControls.Update update = gsClient.objectAccessControls().update(BUCKET_NAME, uri.toString().replace("gs://"+BUCKET_NAME+"/", ""), "allUsers", oac);
update.execute();
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
Elad Benda
  • 35,076
  • 87
  • 265
  • 471

1 Answers1

7

GCS doesn't support true folders -- the namespace is flat, and the meaning of "/" is really imposed by clients (and client libraries). As such, folders can't be renamed atomically - you would need to rename each of the contained files, like what the gsutil mv command does. You can see this by running a command like:

gsutil -d mv gs://my-bucket/folder1 gs://my-bucket/folder2

The -d option will cause it to output the sequence of requests gsutil generates to do the rename.

Mike Schwartz
  • 11,511
  • 1
  • 33
  • 36