Node.js 谷歌云存储返回错误身份
在我的本地机器上,我一直在用不同的帐户处理两个不同的项目,等等,这些项目都不相关,所以 假设这两个项目有以下电子邮件: 首先-project@gmail.com 第二-project@gmail.com 第二个项目(第二个)-project@gmail.com)我使用nodejs制作了一个API,其中有一个端点可以上传文件,所以我尝试使用postman将文件上传到butchet,nodejs端点上的配置具有第二个端点的项目id-project@gmail.com帐户,还具有存在于第二个-project@gmail.com,以下是我上传文件的代码:Node.js 谷歌云存储返回错误身份,node.js,google-cloud-platform,google-cloud-storage,Node.js,Google Cloud Platform,Google Cloud Storage,在我的本地机器上,我一直在用不同的帐户处理两个不同的项目,等等,这些项目都不相关,所以 假设这两个项目有以下电子邮件: 首先-project@gmail.com 第二-project@gmail.com 第二个项目(第二个)-project@gmail.com)我使用nodejs制作了一个API,其中有一个端点可以上传文件,所以我尝试使用postman将文件上传到butchet,nodejs端点上的配置具有第二个端点的项目id-project@gmail.com帐户,还具有存在于第二个-proj
const GCStorage = require('@google-cloud/storage');
const Multer = require('multer');
const CLOUD_BUCKET = 'mybucketname';
const storage = new GCStorage.Storage({
projectId: '1234657890'
});
const bucket = storage.bucket(CLOUD_BUCKET);
// Returns the public, anonymously accessable URL to a given Cloud Storage
// object.
// The object's ACL has to be set to public read.
// [START public_url]
function getPublicUrl(filename) {
return `https://storage.googleapis.com/${CLOUD_BUCKET}/${filename}`;
}
// [END public_url]
// Express middleware that will automatically pass uploads to Cloud Storage.
// req.file is processed and will have two new properties:
// * ``cloudStorageObject`` the object name in cloud storage.
// * ``cloudStoragePublicUrl`` the public url to the object.
// [START process]
function sendUploadToGCS(req, res, next) {
if (!req.file) {
return next();
}
const gcsname = Date.now() + req.file.originalname;
const file = bucket.file(gcsname);
const stream = file.createWriteStream({
metadata: {
contentType: req.file.mimetype
},
resumable: false
});
stream.on('error', err => {
req.file.cloudStorageError = err;
next(err);
});
stream.on('finish', () => {
req.file.cloudStorageObject = gcsname;
file.makePublic().then(() => {
req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
next();
});
});
stream.end(req.file.buffer);
}
// [END process]
// Multer handles parsing multipart/form-data requests.
// This instance is configured to store images in memory.
// This makes it straightforward to upload to Cloud Storage.
// [START multer]
const multer = Multer({
storage: Multer.MemoryStorage,
limits: {
fileSize: 5 * 1024 * 1024 // no larger than 5mb
}
});
// [END multer]
module.exports = {
getPublicUrl,
sendUploadToGCS,
multer
};
但我得到了以下错误:
{
domain: 'global',
reason: 'forbidden',
message: 'first-project@gmail.com does not have storage.objects.create access to mybucketname/1578598638283DeveloperInPixelsLogo1.png.'
}
正如您所看到的,该邮件包含第一个用户的电子邮件地址-project@gmail.com但是这是不同的项目和帐户,所以我无法理解这里发生了什么,请提供任何帮助您正在创建的
存储对象是;它可能正在从您的第一个项目加载服务帐户的凭据
如果要始终为第二个项目的服务帐户使用凭据,则应使用或使用存储
,而不是尝试从默认源加载凭据。正在创建的存储
对象是;它可能正在从您的第一个项目加载服务帐户的凭据
如果要始终为第二个项目的服务帐户使用凭据,则应选择或使用存储
,而不是尝试从默认源加载凭据