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

前端 未结 4 1637
慢半拍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:51

    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'
    

提交回复
热议问题