Google cloud platform 谷歌云存储:CORS设置不';不适用于已签名的URL

Google cloud platform 谷歌云存储:CORS设置不';不适用于已签名的URL,google-cloud-platform,google-cloud-storage,Google Cloud Platform,Google Cloud Storage,带有签名URL的PUT请求的响应不包含头访问控制允许来源 导入操作系统 从日期时间导入时间增量 导入请求 从google.cloud导入存储 os.environ[“谷歌应用程序凭据”]= client=storage.client() bucket=client.get_bucket('my_bucket')) 政策=[ { “来源”:[“*”], '方法':['PUT'], } ] bucket.cors=策略 bucket.update() blob=bucket.blob('new_fi

带有签名URL的
PUT
请求的响应不包含头
访问控制允许来源

导入操作系统
从日期时间导入时间增量
导入请求
从google.cloud导入存储
os.environ[“谷歌应用程序凭据”]=
client=storage.client()
bucket=client.get_bucket('my_bucket'))
政策=[
{
“来源”:[“*”],
'方法':['PUT'],
}
]
bucket.cors=策略
bucket.update()
blob=bucket.blob('new_file')
url=blob.generate\u signed\u url(timedelta(days=30),method='PUT')
response=requests.put(url,data='some data')
对于响应中的标头。headers.keys():
打印(页眉)
输出:

X-GUploader-UploadID
ETag
x-goog-generation
x-goog-metageneration
x-goog-hash
x-goog-stored-content-length
x-goog-stored-content-encoding
Vary
Content-Length
Date
Server
Content-Type
Alt-Svc

正如您所看到的,没有CORS标头。那么,我能否得出结论,GCS不能正确/充分地支持CORS?

跨来源资源共享(CORS)允许来自不同来源的资源之间进行交互默认情况下,为了防止恶意行为,在谷歌云存储中禁止/禁用它。

您可以使用Rest API或Cloud SDK启用它,请记住以下规则:

  • 使用具有云存储类型:
    FULL\u CONTROL
    权限的用户/服务帐户进行身份验证

  • 要获取正确的CORS标头,请使用以下两个URL之一:

  • Origin
    storage.cloud.google.com/[BUCKET\u NAME]
    将不响应CORS头

  • 对于您的代码,请求需要正确的源标头以匹配的第3点中所述的bucket策略源配置:
  • 给出以下输出:

    X-GUploader-UploadID
    ETag
    x-goog-generation
    x-goog-metageneration
    x-goog-hash
    x-goog-stored-content-length
    x-goog-stored-content-encoding
    Access-Control-Allow-Origin
    Access-Control-Expose-Headers
    Content-Length
    Date
    Server
    Content-Type
    

    我有这个问题。对我来说,问题是我使用的是
    POST
    而不是
    PUT
    。此外,我必须设置上传的
    内容类型
    ,以匹配用于生成表单的内容类型。演示中的默认内容类型是“application/octet stream”,所以我不得不将其更改为上传的内容类型。在执行XMLHttpRequest时,我只需要直接发送文件,而不是使用FormData

    这就是我获得签名url的方式

        const options = {
            version: 'v4',
            action: 'write',
            expires: Date.now() + 15 * 60 * 1000, // 15 minutes
            contentType: 'application/octet-stream',
        };
    
        // Get a v4 signed URL for uploading file
        const [url] = await storage
            .bucket("lsa-storage")
            .file(upload.id)
            .getSignedUrl(options as any);
    

    这仅适用于GET请求。但对PUT不起作用。看看我提供的代码。我做了与您建议的相同的事情,只是您在示例中的bucket策略中使用的方法是PUT.BTW DELETE不起作用。该请求在除GET、HEAD或POST之外的任何其他方法中都会被预引导。这包括:发布和删除。你试过检查吗?是的。飞行前请求的响应不包含CORS标头。在我的例子中,它被放置(删除也被选中)。我确实复制了你的问题,并相应地更新了答案。它对我有用。
    X-GUploader-UploadID
    ETag
    x-goog-generation
    x-goog-metageneration
    x-goog-hash
    x-goog-stored-content-length
    x-goog-stored-content-encoding
    Access-Control-Allow-Origin
    Access-Control-Expose-Headers
    Content-Length
    Date
    Server
    Content-Type
    
        const options = {
            version: 'v4',
            action: 'write',
            expires: Date.now() + 15 * 60 * 1000, // 15 minutes
            contentType: 'application/octet-stream',
        };
    
        // Get a v4 signed URL for uploading file
        const [url] = await storage
            .bucket("lsa-storage")
            .file(upload.id)
            .getSignedUrl(options as any);