'How to create a empty folder in google storage(bucket) using gsutil command?

How we can create the folder using gsutil command. I am using Bashoperator in airflow where I need to use the gsutil Bash command, Bucket is already created I want to create a folder inside bucket. I already tried with below command but It's not working for me.

$ gsutil cp <new_folder> gs://<bucketname>/  

I am getting error - CommandException: No URLs matched: new_folder



Solution 1:[1]

Google Storage does not work like a regular file system as in Windows/Linux. It appears to have folders but in the background it behaves as it does not. It only allows us to create "folders" so we can organize better and for our comfort.

If you want to save data in specific folders from gsutil try this.

gsutil cp [filetocopy] gs://your-bucket/folderyouwant/your-file

It will store the item in a "folder".

Check this link for more gsutil cp information.

This is the logic behind Google Cloud Storage "Folders".

gsutil will make a bucket listing request for the named bucket, using delimiter="/" and prefix="abc". It will then examine the bucket listing results and determine whether there are objects in the bucket whose path starts with gs://your-bucket/abc/, to determine whether to treat the target as an object name or a directory name. In turn this impacts the name of the object you create: If the above check indicates there is an "abc" directory you will end up with the object gs://your-bucket/abc/your-file; otherwise you will end up with the object gs://your-bucket/abc.

Here you have more interesting information about this if you want.

Solution 2:[2]

Apparently the ability to create an empty folder using gsutil is a request that has been seen a few times but not yet satisfied. There appears to be some workarounds by using API that can then be scripted. The GitHub issue for the ability to create empty folders through scripting can be found here:

https://github.com/GoogleCloudPlatform/gsutil/issues/388

Solution 3:[3]

You cannot create or copy an empty folder to GCS with gsutil as far as I researched and tried about it. Yes, it's inconvenient somehow.

A folder must not be empty to be created or copied to GCS and don't forget the flag "-r" to create or copy a folder to GCS as shown below otherwise you will get error if a folder is empty or you forgot the flag -r:

gsutil cp -r <non-empty-folder> gs://your-bucket
      // "-r" is needed for folder

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Kolban
Solution 3