Reputation: 2250
I am trying to upload an image to google cloud bucket and get its URI(gc://my_bucket/file.jpg) using python.
Can anyone please tell me how to do that as I don't have any prior experience in python.
Upvotes: 2
Views: 3710
Reputation: 38399
Here's the easiest way I know of. It requires first installing gcloud-python, which you can do with the command pip install --upgrade gcloud
:
from gcloud import storage
bucket_name = 'name-of-bucket'
object_name = 'objectName.jpg'
client = storage.Client()
bucket = client.get_bucket(bucket_name)
blob = bucket.get_blob(object_name)
blob.upload_from_filename('localImageFileName.jpg', content_type='image/jpeg')
blob.make_public()
uri = "gs://%s/%s" % (bucket_name, object_name)
Upvotes: 5