Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename a folder in Google-storage programmatically?

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();
like image 945
Elad Benda Avatar asked Apr 02 '17 08:04

Elad Benda


1 Answers

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.

like image 106
Mike Schwartz Avatar answered Sep 21 '22 10:09

Mike Schwartz