首页 > 解决方案 > 在反应中使用流的谷歌存储文件上传问题

问题描述

每个人。我将从反应前端上传图像,图像文件作为缓冲区发送。在后端,代码如下所示。

const { Storage } = require("@google-cloud/storage");
const path = require("path");
const send = require("../../send.js");

const serviceKey = path.join(__dirname, "./keys.json");

const storage = new Storage({
  keyFilename: serviceKey,
  projectId: "myprojectID",
});
const bucket = storage.bucket("bucketName");

const uploadImage = async (buffer) => {
  const file = bucket.file("newImage.png");
  await file.save(buffer);
  await file.makePublic();
  const publicUrl = `http://storage.googleapis.com/${bucket.name}/${file.name}`;
  return publicUrl;
};

看起来文件正在正确上传到谷歌存储桶,但是当这段代码运行时,我收到了这个警告

*!已请求 Google API!

存储的大小相同,但是当我单击存储上的该链接时,图像未显示,如果我单击“https://storage.googleapis.com/storage/v1/b/{“myprojectID” }/o/newImage.png/acl?” 它在浏览器上显示此 401 错误。

{
  "error": {
    "code": 401,
    "message": "Anonymous caller does not have storage.objects.getIamPolicy access to the Google Cloud Storage object.",
    "errors": [
      {
        "message": "Anonymous caller does not have storage.objects.getIamPolicy access to the Google Cloud Storage object.",
        "domain": "global",
        "reason": "required",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}

谁能帮我?提前致谢。

标签: node.jsreactjsgoogle-cloud-storagegoogle-authentication

解决方案


推荐阅读