为名称中带有 space 的云存储对象获取签名 url 时的 NoSuchKey

NoSuchKey when getting a signed url for a cloudstorage object with a space in the name

下面的应用引擎代码使用 app_identity.sign_blob() 来请求已签名的 url。当 GCS 文件名中没有 space 时,此代码工作正常。 object names 中允许 space。为了测试,我使用了 SDK。

我看到很多关于这个问题的问题,但我无法创建解决方案

是bug还是?

def sign_url(bucket_object, expires_after_seconds=6, bucket=default_bucket):

    method = 'GET'
    gcs_filename = urllib.quote('/%s/%s' % (bucket, bucket_object))
    content_md5, content_type = None, None

    # expiration : number of seconds since epoch
    expiration_dt = datetime.utcnow() +    timedelta(seconds=expires_after_seconds)
    expiration = int(time.mktime(expiration_dt.timetuple()))

    # Generate the string to sign.
    signature_string = '\n'.join([
        method,
        content_md5 or '',
        content_type or '',
        str(expiration),
        gcs_filename])

    signature_bytes = app_identity.sign_blob(signature_string)[1]

    # Set the right query parameters. we use a gae service account for the id
    query_params = {'GoogleAccessId': google_access_id,
                    'Expires': str(expiration),
                    'Signature': base64.b64encode(signature_bytes)}

    # Return the built URL.
    result = '{endpoint}{resource}? {querystring}'.format(endpoint=GCS_API_ACCESS_ENDPOINT,
                                                          resource=gcs_filename,    
                                                          querystring=urllib.urlencode(query_params))
return result

更新。使用带符号的 url:

时的响应
<Error>
    <Code>NoSuchKey</Code>
    <Message>The specified key does not exist.</Message>
</Error>

问题已解决

不知道为什么。我没有改变任何东西。 您可以使用下面的代码和此 link:

试用 GAE 应用程序
https://voscausa-gae.appspot.com/download_signed?bucket_object=download/test signed1 .txt

代码在这里:appengine-gcs-signed-url

您还可以使用带有保单文件的签名 url 直接上传到 GCS。
代码:https://github.com/voscausa/appengine-gcs-upload