Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an empty folder on Google Storage with Google API?

How to create an empty folder on Google Storage with Google API? (Assume that / is path separator.)

like image 940
porton Avatar asked Jul 16 '16 23:07

porton


People also ask

How do I create a folder in Google Drive API?

To create a file in a folder, use the files. create method and specify the folder ID in the parents property of the file. The following code snippet shows how to create a file in a specific folder using a client library: Note: If you're using the older Drive API v2, use the files.

Is Google Cloud Storage API free?

Similar to Amazon Drive or Microsoft OneDrive, it offers free and fee-based storage plans - you start with 15 GB of cloud space for free, but then you can upgrade your storage space up to 100 GB for $1.99/month, 1 TB for $9.99/month or 10 TB for $99.99/month.

What is Google storage Apis?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.

What is Gsutil command?

gsutil is a Python application that lets you access Cloud Storage from the command line. You can use gsutil to do a wide range of bucket and object management tasks, including: Creating and deleting buckets. Uploading, downloading, and deleting objects. Listing buckets and objects.


2 Answers

@SheRey - looking at folders created via the GCS web interface, the Content-Type is set to application/x-www-form-urlencoded;charset=UTF-8 but it doesn't really matter. Here's what worked for me in python:

# pip install google-cloud-storage  from google.cloud import storage  gcs_client = storage.Client(project='some_project') bucket = gcs_client.get_bucket('some_bucket') blob = bucket.blob('some/folder/name/')  blob.upload_from_string('', content_type='application/x-www-form-urlencoded;charset=UTF-8') 
like image 72
user2526241 Avatar answered Oct 05 '22 09:10

user2526241


Google Cloud Storage does not have folders or subdirectories. However, there is some support for emulating them. gsutil's How Subdirectories Work is a good read for some background.

Google Cloud Storage objects are a flat namespace, but many tools, including gsutil and the Google Cloud Storage UI, create an illusion of a hierarchical file tree.

There are two widely used conventions for creating the illusion of an empty subdirectory:

  1. (recommended) Create an object that ends in a trailing slash. For example, to create a subdirectory called foo at the root of a bucket, you would create an empty object (size 0) called foo/.

  2. (legacy) Create an object with _$folder$ appended to the name. For example, to create a subdirectory called foo at the root of a bucket, you would create an empty object (size 0) called foo_$folder$.

Note that most tools and utilities are using method 1 now. Method 2 is less frequently used.

like image 34
jterrace Avatar answered Oct 05 '22 09:10

jterrace