Create signed URLs for Google Cloud Storage with node.js for direct upload from browser(使用 node.js 为 Google Cloud Storage 创建签名 URL,以便从浏览器直接上传)
问题描述
actual testcase code: https://github.com/HenrikJoreteg/google-cloud-signedurl-test-case
I'm trying to add ability for my API to return signed URLs for direct upload to Google Cloud Storage from the client.
Serverside, I'm using the gcloud
SDK for this:
const gcloud = require('gcloud')
const gcs = gcloud.storage({
projectId: 'my project',
keyFilename: __dirname + '/path/to/JSON/file.json'
})
const bucket = gcs.bucket('bucket-name')
bucket.file('IMG_2540.png').getSignedUrl({
action: 'write',
expires: Date.now() + 60000
}, (error, signedUrl) => {
if (error == null) {
console.log(signedUrl)
}
})
Then in the browser I've got an <input type='file'/>
that I've selected a file with, then I attempt to post it to the URL generated from my server-side script like this:
function upload(blobOrFile, url) {
var xhr = new XMLHttpRequest();
xhr.open('PUT', url, true);
xhr.onload = function(e) {
console.log('DONE!')
};
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
console.log((e.loaded / e.total) * 100)
}
};
xhr.send(blobOrFile);
}
// grab the `File` object dropped (which incidentally
// matches the file name used when generating the signed URL
upload($('[name=file]').files[0], 'URL GENERATED FROM SERVER-SIDE SCRIPT HERE');
What happens?
Response is:
<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>PUT
image/png
1476631908
/bucket-name/IMG_2540.png</StringToSign>
</Error>
I've re-downloaded the JSON key file to make sure it's current and has proper permissions to that bucket and I don't get any errors or anything when generating the signed URL.
The clientside code appears to properly initiate an upload (I see progress updates logged out) then I get the 403 error above. Filenames match, content-types seem to match expected values, expiration seems reasonable.
The official SDK generated the URL, so it seems like it'd be ok.
I'm stuck, any help appreciated.
As was pointed out by Philip Roberts, aka @LatentFlip on my github repo containing this case, adding a content-type to the signature took care of it.
https://github.com/HenrikJoreteg/google-cloud-signedurl-test-case/pull/1/commits/84290918e7b82dd8c1f22ffcd2c7cdc06b08d334
Also, it sounds like the Google folks are going to update docs/error to be a bit more helpful: https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1695
这篇关于使用 node.js 为 Google Cloud Storage 创建签名 URL,以便从浏览器直接上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 node.js 为 Google Cloud Storage 创建签名 URL,以便从浏览器直接上传
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01