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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20863747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:25:06  来源:igfitidea点击:

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

pythongoogle-app-enginecloudgoogle-cloud-storage

提问by orcaman

Is there a python equivalent to the getPublicUrl PHP method?

是否有与 getPublicUrl PHP 方法等效的 python方法

$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.

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

采纳答案by orcaman

Daniel, Isaac - Thank you both.

Daniel, Isaac - 谢谢你们。

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

在我看来,谷歌故意让你不要直接从 GCS 提供服务(带宽原因?不知道)。因此,根据文档的两个替代方案是使用 Blobstore 或图像服务(用于图像)。

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

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

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

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

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

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

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

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

FileServer.py:

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

回答by Daniel Roseman

You need to use get_serving_urlfrom the Images API. As that page explains, you need to call create_gs_key()first to get the key to pass to the Images API.

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

回答by Isaac

It's not available, but I've filed a bug. In the meantime, try this:

它不可用,但我已经提交了一个错误。与此同时,试试这个:

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

For example:

例如:

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

回答by Danny Hong

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

有关如何构建 URL,请参阅https://cloud.google.com/storage/docs/reference-uris

For public URLs, there are two formats:

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

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

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