获取文件的公共URL-谷歌云存储-应用程序引擎(Python)

2024-06-28 20:06:06 发布

您现在位置:Python中文网/ 问答频道 /正文

是否有与getPublicUrlPHP method等价的python?

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

我正在使用googlecloudclientlibraryforpython来存储一些文件,并且我正在试图找出一种以编程方式获取我所存储文件的公共URL的方法。


Tags: 文件txtgstrueurlbucketmysome
3条回答

请参阅https://cloud.google.com/storage/docs/reference-uris了解如何构建URL。

对于公共URL,有两种格式:

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

或者

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

示例:

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

将输出:

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

您需要使用图像API中的^{}。正如该页所解释的,您需要首先调用create_gs_key(),以获取传递给Images API的密钥。

丹尼尔,艾萨克-谢谢你们。

在我看来,谷歌有意让你不要直接从GCS服务(带宽原因?不知道)。因此,根据文档,这两个选项要么使用Blobstore,要么使用Image Services(用于图像)。

我最后做的是通过GCS为这些文件提供blobstore服务。

要从GCS路径获取blobstore密钥,我使用:

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

然后,我在服务器上公开了这个URL- 主.py:

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

文件服务器.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')

相关问题 更多 >