How to rename a folder in google storage programmatically?

I have google storage java client.

I want to rename a folder in the cloud.

Is there a way to do this?

I saw the update post , but I'm not sure how to change the name metadata.

here is my attempt but i dont know what to fill "entity"

in and nooac.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:

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

      

+3


source to share


1 answer


GCS does not support real folders - the namespace is flat and the "/" value is really enforced by clients (and client libraries). Thus, folders cannot be renamed atomically - you will need to rename each of the contained files, for example, which does the gsutil mv command. You can see this by running the command:

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

      



The -d option will cause it to dump the sequence of prompts that gsutil makes to perform the rename.

+4


source







All Articles