11

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

2 Answers 2

20

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!

Sign up to request clarification or add additional context in comments.

3 Comments

Worked perfectly on the first try
What if I want to upload a list of files?
I have been using a loop, uploading one file at a time.
8

If somebody is looking for the same thing, here is the solution. First you need to create/initialize bucket variable with the bucket name you want to create a new blob in. After that instead of calling blob you create blob using bucket.blob("filename"), this creates a new blob if the given filename is not there in the bucket already. Then you can use blob.create_from_filename("filename_which_you want to copy"), this will copy the contents to the blob created earlier for more detail solution you can read blob documentation here:

https://googlecloudplatform.github.io/google-cloud-python/stable/storage-blobs.html

Hope this will help someone. Below is the sample python code:

client=storage.Client();  
bucket=client.get_bucket('your bucket name');  
blob=bucket.blob('newfile you want to create')
blob.upload_from_filename('localdirectory/filename.mp4')

4 Comments

It generates: AttributeError: 'module' object has no attribute 'Client'
Hi jack, Sorry for late response! Are you able to atleast import storage package("from google.cloud import storage")? Please share more details about the code. You can also go through the documentation here -> cloud.google.com/storage/docs/reference/…
Important for me was also to specify the content type and timeout: blob.upload_from_filename(source_file_name, timeout=None, content_type="video/mp4")
Link in the answer is broken

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.