Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating/Uploading new file at Google Cloud Storage bucket using Python

How to create new empty files in Google Cloud Storage using Python with client libraries available?

Or how to upload a new file to a selected bucket using blob function "upload_from_filename()" ? To initialize the blob object we should have file already in the cloud bucket, but I want to create a new file name, and copy the content from the file stored locally. I want to do this using python.

Thanks in advance

like image 225
Akhil_Singh_Rana Avatar asked Apr 06 '17 19:04

Akhil_Singh_Rana


People also ask

How do I import a CSV file into a GCP bucket?

Select All Settings > Raw Data Export > CSV Upload. Select Google Cloud Storage from the dropdown menu. Upload your Service Account Key credential file. This is the JSON file created in the Google Cloud Console. Enter your Google Cloud Storage bucket name.

How do I upload a directory bucket to GCP?

In the Google Cloud console, go to the Cloud Storage Buckets page. Navigate to the bucket. Click on Create folder to create an empty new folder, or Upload folder to upload an existing folder.


1 Answers

First you need to load the library

import google.cloud.storage

Assuming you have a service account created and JSON file loaded somewhere, you need to create a client object

storage_client = google.cloud.storage.Client.from_service_account_json('JSON filepath')

Then you need to provide the bucket object

bucket = storage_client.get_bucket('bucket_name')

Now define the path within your bucket and the file name

d = 'path/name'

The blob method creates the new file, also an object.

d = bucket.blob(d)

The upload_from_string method add the contents of the file. There are other methods allowing you to perform different tasks.

d.upload_from_string('V')

The first few steps take the longest to be ready the first time.

Good luck!

like image 76
Jie Avatar answered Oct 31 '22 09:10

Jie