繁体   English   中英

获取文件的公共 URL - Google Cloud Storage - App Engine (Python)

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

是否有与 getPublicUrlPHP 方法等效的 python方法

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

我正在使用适用于 Python 的 Google Cloud 客户端库存储一些文件,并且我正在尝试找出一种以编程方式获取我正在存储的文件的公共 URL 的方法。

请参阅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

您需要使用Images API中的get_serving_url 就像该页面所说明的那样,您需要先调用create_gs_key()以获得将密钥传递给Images API。

丹尼尔,以撒-谢谢你们。

在我看来,Google故意让您不要直接从GCS提供服务(带宽原因?Dunno)。 因此,根据文档的两种选择是使用Blobstore或Image Services (用于图像)。

我最终要做的是通过GCS通过blobstore提供文件。

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

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

然后,我在服务器-Main.py上公开此URL:

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

它不可用,但是我已经提交了一个错误 同时,请尝试以下操作:

import urlparse

def GetGsPublicUrl(gsUrl, secure=True):
  u = urlparse.urlsplit(gsUrl)
  if u.scheme == 'gs':
    return urlparse.urlunsplit((
        'https' if secure else 'http',
        '%s.storage.googleapis.com' % u.netloc,
        u.path, '', ''))

例如:

>>> GetGsPublicUrl('gs://foo/bar.tgz')
'https://foo.storage.googleapis.com/bar.tgz'

最后是: blob.public_url

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM