繁体   English   中英

Google Cloud Storage 生成上传/下载 URL 失败,blobname 中包含 unicode 个字符

[英]Google Cloud Storage generate upload/download URL fails with unicode characters in blobname

我最近一直在测试在 GCS 上上传和下载,只要 blobname 或“路径”都是英文字母,包括空格和特殊字符,我就能够毫无问题地生成上传和下载 URL。 但是,一旦我在文件名(blobname)中使用 unicode 个字符,它就会因KeyError而失败

文档提到支持 unicode:

Blob 的名称。 这对应object在桶中的唯一路径。 如果是字节,将被转换为 unicode object。Blob / object 名称可以包含任何有效的 unicode 字符序列,UTF-8 编码时长度为 1-1024 字节。

代码是几行:

generate_download_signed_url_v4("storage_files", "202209/جراج.png")

def generate_download_signed_url_v4(bucket_name, blob_name):
    storage_client = storage.Client.from_service_account_json('s_account.json')
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(blob_name)
    
    url = blob.generate_signed_url(
        expiration=datetime.timedelta(minutes=10), # This URL is valid for 10 minutes
        method="GET", # Allow GET requests using this URL.
    )
    
    print "Generated GET signed URL:", url
    return url

错误指的是 keyerror 以及 unicode 文件名部分 (blobname) 中第一个字符的 unicode 值

  Traceback (most recent call last):
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Users/k/Documents/app30/gae/users_app/helpers.py", line 68, in dispatch
    webapp2.RequestHandler.dispatch(self) # dispatch the main handler
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/k/Documents/app30/gae/users_app/helpers.py", line 431, in check_admin
    return myfunction(self, *args, **kwargs)
  File "/Users/k/Documents/app30/gae/users_app/main.py", line 10796, in get
    my_file_link = self.generate_download_signed_url_v4(root_bucket_name, "572680296//202209/جراج.png")
  File "/Users/k/Documents/app30/gae/users_app/main.py", line 10863, in generate_download_signed_url_v4
    method="GET", # Allow GET requests using this URL.
  File "/Users/k/Documents/app30/gae/users_app/lib/google/cloud/storage/blob.py", line 338, in generate_signed_url
    quoted_name=quote(self.name))
  File "/Users/k/Documents/app30/google-cloud-sdk/platform/google_appengine/google/appengine/dist27/urllib.py", line 1294, in quote
    return ''.join(map(quoter, s))
KeyError: u'\u062c'
*******> Exception: u'\u062c'

值得一提的是,这是在本地开发服务器 v.340 上

问题已于 2018 年修复。查看中的行号和代码

 File "/Users/k/Documents/app30/gae/users_app/lib/google/cloud/storage/blob.py", line 338, in generate_signed_url
    quoted_name=quote(self.name))

确认正在使用该修复之前的版本(它们与提交更改的版本相匹配)。 升级到1.11.0 之后的库版本

暂无
暂无

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

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