12
votes

I can't rename an existing folder in GCS. How do I do this?

As per the documentation, this should be:

gsutil mv gs://my_bucket/olddir gs://my_bucket/newdir

However, what happens is that olddir is placed under newdir, i.e the directory structure is like this (after the call to gsutil mv):

my_bucket
    newdir
        olddir

instead of (what I would expect)

my_bucket
    newdir

I've tried all four combinations of putting trailing slashes or not, but none of them worked.

2
What is the output from the gsutil command when you run it and end up with the unwanted nested folder structure? Normally you would see a Copying... and Removing... line recursively for all the files contained under your olddir. - yoape
Possible duplicate of: stackoverflow.com/questions/45896712/… ...which confirms my answer - while I even provided a viable solution approach to the problem, instead of just telling "it's not possible". - Martin Zeitler
@yoape: Yes, I see lots of "Copying gs://[..]" and "Removing gs://[..]" lines. - knub
Generally speaking, in object storage systems like GS and AWS S3, a folder is not a real first-class item the way a file is. GS seems to have fixed this bug, but in object management you should design around files, not folders. - Jack Parsons

2 Answers

10
votes

This is a confirmed bug in GCS, see https://issuetracker.google.com/issues/112817360

It actually only happens, when the directory name of newdir is a substring of olddir. So the gsutil call from the question actually works, but the following one would not:

gsutil mv gs://my-organization-empty-bucket/dir_old gs://my-organization-empty-bucket/dir
1
votes

I reproduced your case by having a bucket with a folder named olddir of which I want to move the content to newdir folder.

the following command:

 gsutils mv gs://<bucketname>/olddir gs://<bucketname>/newdir

moved the whole content of folder to the newly created newdir folder.

Olddir and newdir folders were then at the same level, in the bucket root. after that I just had to remove the folder called olddir.

Objects in a bucket cannot be renamed.

The gsutil mv command does not remove the previous folder object like the mv comand would do in Unix CLI.

I guess that if you have tried moving folders several times by using "/" characters placed differently, the structure and hierarchy of the folders will have changed after issuing the initial command.

Please try again from the beginning.

Bear in mind that once you have a subfolder inside a folder, objects will have to be moved one by one using the full path.