gpt4 book ai didi

node.js - 如何使用 google-cloud/storage 的签名策略来上传数据?

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:32 25 4
gpt4 key购买 nike

ref: https://cloud.google.com/nodejs/docs/reference/storage/1.5.x/File#getSignedPolicy

我正在尝试使用签名策略将文件上传到我的存储桶。使用文档中的示例,我从存储桶对象中获得了 3 个项目的响应。

"policyString": "{\"expiration\":\"2019-10-18\",\"conditions\":[[\"eq\",\"$key\",\"image.jpg\"],{\"bucket\":\"my-bucket\"},[\"eq\",\"$Content-Type\",\"image/jpeg\"],[\"content-length-range\",0,1024]]}",
"policyBase64": "[some-long-string]",
"policySignature": "[some-long-string]"

在文档中,它还向您展示了如何使用curl上传对象。

ref: https://cloud.google.com/storage/docs/object-basics#upload-object-json

如何将我返回的签名策略和上传 API 组合起来

https://www.googleapis.com/upload/storage/v1/b/[BUCKET_NAME]/o?uploadType=media&name=[OBJECT_NAME]

使用 axios 将文件上传到我的存储桶?是否还有其他 header 要附加?

这是我所做的尝试,但我不确定。

const options = {
headers: {
'Authorization': ?
'Content-Type': file.type
}
}
axios.put(concatenatedPolicySignatureUrl, file, options)
.then(response => {
console.log('success!');
}

最佳答案

签名的策略字符串仅适用于 XML API,这意味着 POST 到 https://storage.googleapis.com/bucket/object(而不是 www.googleapis.com/upload )。

有关如何生成和使用策略的更多信息,请参阅 https://cloud.google.com/storage/docs/xml-api/post-object#policydocument

下面是设置允许用户使用策略字符串上传对象的 HTTP 表单的示例,不过您也可以通过 JavaScript 触发等效的 POST 请求:

<form action="http://travel-maps.storage.googleapis.com" method="post" enctype="multipart/form-data">
<input type="text" name="key" value="">
<input type="hidden" name="bucket" value="travel-maps">
<input type="hidden" name="Content-Type" value="image/jpeg">
<input type="hidden" name="GoogleAccessId" value="1234567890123@developer.gserviceaccount.com">
<input type="hidden" name="acl" value="bucket-owner-read">
<input type="hidden" name="success_action_redirect" value="http://www.example.com/success_notification.html">
<input type="hidden" name="policy" value="eyJleHBpcmF0aW9uIjogIjIwMTAtMDYtMTZUMTE6MTE6MTFaIiwNCiAiY29uZGl0aW9ucyI6IFsNCiAgWyJzdGFydHMtd2l0aCIsICJrZXkiLCAiIiBdLA0KICB7ImFjbCI6ICJidWNrZXQtb3duZXItcmVhZCIgfSwNCiAgeyJidWNrZXQiOiAidHJhdmVsLW1hcHMifSwNCiAgeyJzdWNjZXNzX2FjdGlvbl9yZWRpcmVjdCI6ICJodHRwOi8vd3d3LmV4YW1wbGUuY29tL3N1Y2Nlc3Nfbm90aWZpY2F0aW9uLmh0bWwiIH0sDQogIFsiZXEiLCAiQ29udGVudC1UeXBlIiwgImltYWdlL2pwZWciIF0sDQogIFsiY29udGVudC1sZW5ndGgtcmFuZ2UiLCAwLCAxMDAwMDAwXQ0KICBdDQp9">
<input type="hidden" name="signature" value="BSAMPLEaASAMPLE6SAMPLE+SAMPPLEqSAMPLEPSAMPLE+SAMPLEgSAMPLEzCPlgWREeF7oPGowkeKk7J4WApzkzxERdOQmAdrvshKSzUHg8Jqp1lw9tbiJfE2ExdOOIoJVmGLoDeAGnfzCd4fTsWcLbal9sFpqXsQI8IQi1493mw=">

<input name="file" type="file">
<input type="submit" value="Upload">
</form>

关于node.js - 如何使用 google-cloud/storage 的签名策略来上传数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48635804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com