node.js - 如何使用签名网址将文件上传到谷歌云存储桶

标签 node.js angular google-cloud-platform cors google-cloud-storage

我正在开发一个 Angular 应用程序,用于显示谷歌云存储桶的内容。对于后面,我在nodeJS中使用谷歌云功能

正如他们在上传文件的文档中提到的,我创建了一个函数来生成签名的 url,但是当我使用签名的 url 发送文件时,我在浏览器中收到了 cors 错误

我用 postman 进行了测试,它上传了一个空文件

这是我的 lambda 函数:

// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

exports.generateSignedUrl = (req, res) => {
// generate signed url to use for file upload

const filename = req.query.fileName;
console.log('filename ', filename);

const filetype = req.query.fileType;
console.log('filetype ', filetype);

const bucketName = 'nx-terega-omega';

res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Headers', "Origin, X-Requested-With, 
Content-Type, Accept, Authorization");

if (req.query.fileName !== null && req.query.fileName !== undefined
  && req.query.fileType !== null && req.query.fileType !== undefined) 
{
generateV4UploadSignedUrl(bucketName, filename).then(function (value) 
{
  console.log('File Url response ', value);
  res.status(200).send(JSON.stringify({'url': value}));
}).catch(error => {
  res.status(404).send('Error while generating signed url');
});
} else {
res.status(500).send('Filename not found');
}
};

async function generateV4UploadSignedUrl(bucketName, filename, filetype) {
// [START storage_generate_upload_signed_url_v4]

// These options will allow temporary uploading of the file with outgoing
// Content-Type: application/octet-stream header.
const options = {
version: 'v4',
action: 'write',
expires: Date.now() + 15 * 60 * 1000, // 15 minutes
contentType: filetype,
};

// Get a v4 signed URL for uploading file
const [url] = await storage
.bucket(bucketName)
.file(filename)
.getSignedUrl(options);

console.log('Generated PUT signed URL:');
console.log(url);
console.log('You can use this URL with any user agent, for example:');
console.log("curl -X PUT -H 'Content-Type: application/octet-stream' " +`--upload-file my-file '${url}'`);

return url;
// [END storage_generate_upload_signed_url_v4]
}

当我收到签名的网址时,我将我的文件发送给它,但它返回

No 'Access-Control-Allow-Origin' header is present on the requested resource.

最佳答案

正如 Brandon Yarbrough 回复中提到的,我必须在 Google Cloud 中配置 cors。我的配置中缺少一些内容

[
 {
  "origin": ["http://example.appspot.com"],
  "responseHeader": ["*"],
  "method": ["GET", "HEAD", "DELETE", "PUT"],
  "maxAgeSeconds": 3600
 }
]

您应该在方法中包含PUT,并将*放入responseHeader中,因为Content-Type不够

关于node.js - 如何使用签名网址将文件上传到谷歌云存储桶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55761597/

相关文章:

angular - PrimeNg 自动完成在清除时发送空字符串

python - 如何在 GCP 计算引擎上从 PyCharm 运行 python 脚本?

linux - 检查 Google Cloud Console 中的先前输出,是否可能?

angular - 如何在包含 RouterOutlet 的 HTML 模板中直接使用激活路由的数据?

google-cloud-platform - 如何标记和标记 Google GCP 负载均衡器?

node.js - 了解 Passport 序列化反序列化

ios - 通过heroku解析服务器在单个设备上发送推送通知

javascript - 如何在不知道按钮 id/class 或表单 id/class 的情况下提交表单?

node.js - 使用 IISnode 运行 Keystone.js

Angular 6 - 对所有表列进行排序仅适用于两列而不是所有列