gpt4 book ai didi

node.js - 使用 Node.js 创建 google 云存储对象的 SignedURL

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

我需要使用 Node.js 创建存储在 Google Cloud Storage 中的对象(通过 Google Developer Console 上传)的签名 URL。

这是我的代码:

var gcloud = require('gcloud');


// Google Cloud Storage Bucket Name
const BUCKET_NAME = 'testBucketName123';
// Google Developer Console project ID
const PROJECT_ID = 'testProjectName123';
/* Google Developer Console -> API Manager -> Credentials ->
Add credentials -> Service account -> JSON -> Create */
const KEY_FILENAME = 'testKey.json' // relative path
const SECONDS = 1000; // seconds in milliseconds
const URL_VALID_DURATION = 30 * SECONDS;

var gcs = gcloud.storage({
projectId: PROJECT_ID,
keyFilename: KEY_FILENAME
});

var filename ='tasksel.png';
var file = gcs.bucket(BUCKET_NAME).file(filename);

file.getSignedUrl({
// More documention on options at
// https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.24.1/storage/file?method=getSignedUrl
action: 'write',
expires: Date.now() * URL_VALID_DURATION
}, function (err, url) {
if (err) {
console.error(err);
return;
}
console.log("URL");
console.log("-----");
console.log(url);
console.log("-----");
console.log("PUT to this URL to upload to Google Cloud Storage as " + filename);
});

我得到了这样的 URL 输出:

https://storage.googleapis.com/testBucketName123/tasksel.png?GoogleAccessId=keyId@iprojectName.iam.gserviceaccount.com&Expires=NaN&Signature=MmI5cQ3NINpOOP%2Fer02R0LxRZJAHplmstvofDXU139VNmZ0MsxpAOTGg6JUHBr8EztiTdR7cNNltoSb0zpM2DCRK5UpDjlrJ6WriSL2uyDsGWgfdx4M1vS0ussRm9BHAT31tqo0U5cdzgJd5rjakPMV06NUw0YYQn1oAEzE6JwThFfUZa%2FexddP763yDOQDKh5sJxW9go3JNmam%2Bk8qq0wJnVP5GNhl3JGlKW7l5AtimUwaI%2BViasX6LNmvwtoyM4%2Fg6Ebx%2BnI9%2Bk2LxWA3c8MUypeEZj0W8AWv%2BAWjf1bzeCEMTOjxV53bo1SA0zmUGjE6GDVd9PnYomwJ88v2h3Q%3D%3D

但是当我用浏览器打开这个URL时得到的输出是:

<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 NaN /testBucketName123/tasksel.png</StringToSign>
</Error>

实际问题是什么?有什么想法吗?

最佳答案

您的签名URL包含expires=NaN
到期时间应该是一个整数。尝试一下

Date.now() + URL_VALID_DURATION

而不是
Date.now() * URL_VALID_DURATION

关于node.js - 使用 Node.js 创建 google 云存储对象的 SignedURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34943772/

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