我正在创建一个 Firebase HTTP 函数,该函数将文件上传到 Cloud Storage,为该文件创建一个签名 URL,然后将客户端重定向到该 URL。使用 Postman 并打开自动重定向跟踪,可以正确检索文件。但是,如果我在使用 cURL ( ) 时尝试打开重定向curl -L -H "Content-Type: application/json" "https://us-central1-example.cloudfunctions.net/exampleFunction" -d '{"example": true}'
,Cloud Storage 会返回以下错误:
<?xml version='1.0' encoding='UTF-8'?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message>
<StringToSign>GET
application/json
1602245678
/example.appspot.com/exampleBucket/exampleFile.txt</StringToSign>
</Error>
如果我使用表单编码数据发出请求,它也可以在 cURL 中使用:curl -L "https://us-central1-example.cloudfunctions.net/exampleFunction" -d "example=true"
如果我尝试手动向 Postman 中的 URL 发出 GET 请求,则会收到等效错误:
<?xml version='1.0' encoding='UTF-8'?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message>
<StringToSign>GET
1602246219
/www.google.com/example.appspot.com/exampleBucket/exampleFile.txt</StringToSign>
</Error>
如果我将 URL 粘贴到浏览器中或使用 cURL 下载签名的 URL,则文件也会正确下载。
我正在使用以下函数来获取签名的 url:
async getSignedUrl(file: File, expireAt: number): Promise<string> {
const [url] = await file
.getSignedUrl({
action: "read",
expires: expireAt
});
return url
}
它返回以下格式的签名 URL:(
https://storage.googleapis.com/example.appspot.com/exampleBucket/exampleFile.txt?GoogleAccessId=[Access ID]&Expires=1602246219&Signature=[Signature]
我注意到“Expires”的值与标签中返回的值相同)。
我怀疑 Postman 和 cURL 在请求中添加了一些内容,导致签名不同,但我不确定到底发生了什么。
当让 cURL 跟随重定向或在 Postman 中创建 GET 请求时会发生什么,这会导致签名的这种差异?