2

I know how to upload the objects using gsutil in command prompt, however, it doesn't seems flexible enough for me. So I am wondering how could I upload the objects to Google Cloud Storage using Python scripts?

1 Answer 1

3

You might want to take a look at the Google Cloud Client Library for Python in particular, the client interacting with the Storage API.

You can install the libraries using pip install:

pip install --upgrade google-cloud

Uploading a file would go something like:

from google.cloud import storage

client = storage.Client(project='project-xxx')
bucket = client.get_bucket('bucket-xxx')
blob = bucket.blob('test.txt')
blob.upload_from_filename('test.txt')
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @atsang01 you can also consider using the "subprocess" module, which allows you to run shell commands from within a python script, as mentioned in my response to a (loosely!) related question here here This can be useful because it lets you use gsutil commands directly, which is sometimes a little more convenient. Happy coding!

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.