bazinga
bazinga

Reputation: 2250

How to upload an image and get make it public in Google Cloud Bucket

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

Answers (1)

Brandon Yarbrough
Brandon Yarbrough

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

Related Questions