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
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')
1 Comment
Paul
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!