Get Public URL for File - Google Cloud Storage - App Engine (Python)

orcaman picture orcaman · Dec 31, 2013 · Viewed 31.4k times · Source

Is there a python equivalent to the getPublicUrl PHP method?

$public_url = CloudStorageTools::getPublicUrl("gs://my_bucket/some_file.txt", true);

I am storing some files using the Google Cloud Client Library for Python, and I'm trying to figure out a way of programatically getting the public URL of the files I am storing.

Answer

Danny Hong picture Danny Hong · Jan 20, 2014

Please refer to https://cloud.google.com/storage/docs/reference-uris on how to build URLs.

For public URLs, there are two formats:

http(s)://storage.googleapis.com/[bucket]/[object]

or

http(s)://[bucket].storage.googleapis.com/[object]

Example:

bucket = 'my_bucket'
file = 'some_file.txt'
gcs_url = 'https://%(bucket)s.storage.googleapis.com/%(file)s' % {'bucket':bucket, 'file':file}
print gcs_url

Will output this:

https://my_bucket.storage.googleapis.com/some_file.txt