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

前端 未结 4 1649
慢半拍i
慢半拍i 2020-12-24 15:28

Is there a python equivalent to the getPublicUrl PHP method?

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


        
4条回答
  •  感动是毒
    2020-12-24 15:54

    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')
    

提交回复
热议问题