Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google GSutil create folder

How can u create a new folder inside a bucket in google cloud storage using the gsutil command?

I tried using the same command in creating bucket but still got an error

gsutil mb -l us-east1 gs://my-awesome-bucket/new_folder/

Thanks!

like image 525
Bongsky Avatar asked Aug 17 '18 09:08

Bongsky


People also ask

What is the difference between gcloud and gsutil?

"gcloud" can create and manage Google Cloud resources while "gsutil" cannot do so. "gsutil" can manipulate buckets, bucket's objects and bucket ACLs on GCS(Google Cloud Storage) while "gcloud" cannot do so.


3 Answers

The concept of directory is abstract in Google Cloud Storage. From the docs (How Subdirectories Work) :

gsutil provides the illusion of a hierarchical file tree atop the "flat" name space supported by the Google Cloud Storage service. To the service, the object gs://your-bucket/abc/def.txt is just an object that happens to have "/" characters in its name. There is no "abc" directory; just a single object with the given name.

So you cannot "create" a directory like in a traditional File System.

like image 123
norbjd Avatar answered Oct 06 '22 07:10

norbjd


If you're clear about what folders and objects already exist in the bucket, then you can create a new 'folder' with gsutil by copying an object into the folder.

>mkdir test
>touch test/file1
>gsutil cp -r test gs://my-bucket
Copying file://test\file1 [Content- 
Type=application/octet-stream]...
/ [1 files][    0.0 B/    0.0 B]
Operation completed over 1 objects.

>gsutil ls gs://my-bucket
gs://my-bucket/test/

>gsutil ls gs://my-bucket/test
gs://my-bucket/test/file1

It won't work if the local directory is empty.

More simply:

>touch file2
>gsutil cp file2 gs://my-bucket/new-folder/
Copying file://test\file2 [Content- ...

>gsutil ls gs://my-bucket/new-folder
gs://my-bucket/new-folder/file2

Be aware of the potential for Surprising Destination Subdirectory Naming. E.g. if the target directory already exists as an object. For an automated process, a more robust approach would be to use rsync.

I don't know if its possible to create an empty folder with gsutil. For that, use the console's Create Folder button.

like image 45
intotecho Avatar answered Oct 06 '22 07:10

intotecho


You cannot create folders with gsutil as gsutil does not support it (workaround see below).

However, it is supported via:

  • UI in browser
  • write your own GCS client (we have written our own custom client which can create folders)

So even if Google has a flat name space structure as the other answer correctly points out, it still has the possibility to create single folders as individual objects. Unfortunately gsutil does not expose this.

(Ugly) workaround with gsutil: Add a dummy file into a folder and upload this dummy file - but the folder will be gone once you delete this file, unless other files in that folder are present.

like image 2
eci Avatar answered Oct 06 '22 09:10

eci