Search code examples
pythongoogle-app-enginecloudgoogle-cloud-storage

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


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.


Solution

  • Daniel, Isaac - Thank you both.

    It looks to me like Google is deliberately aiming for you not to directly serve from GCS (bandwidth reasons? dunno). So the two alternatives according to the docs are either using Blobstore or Image Services (for images).

    What I ended up doing is serving the files with blobstore over GCS.

    To get the blobstore key from a GCS path, I used:

    blobKey = blobstore.create_gs_key('/gs' + gcs_filename)
    

    Then, I exposed this URL on the server - Main.py:

    app = webapp2.WSGIApplication([
    ...
        ('/blobstore/serve', scripts.FileServer.GCSServingHandler),
    ...
    

    FileServer.py:

    class GCSServingHandler(blobstore_handlers.BlobstoreDownloadHandler):
        def get(self):
            blob_key = self.request.get('id')
            if (len(blob_key) > 0):
                self.send_blob(blob_key)
            else: 
                self.response.write('no id given')