当我尝试在nodejs中使用aws-sdk-s3-client上传文件时。它抛出一个验证错误:
错误:已解决的凭据对象在SignatureV4.validateResolvedCredentials(/API/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:163:19)无效,位于SignatureV4.signRequest(/api/node_modules/@aws-sdk/signature-v4/dist-cjs/SignatureV4.js:88:14)
下面是我的s3 Client配置文件的样子:
s3_util.js
const dotenv = require("dotenv");
dotenv.config();
const AWS = require("@aws-sdk/client-s3");
const bucketRegion = process.env.BUCKET_REGION;
const accessKey = process.env.ACCESS_KEY;
const secretAccesskey = process.env.SECRET_ACCESS_KEY;
const s3 = new AWS.S3Client({
credentials: {
accessKeyId: accessKey,
accessKey: secretAccesskey,
},
region: bucketRegion,
});
module.exports = s3;
上传文件到s3
const { PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3");
const s3 = require("./s3_util");
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
const uploadFileToS3 = async (bucketName, file, uploadPath) => {
console.log(file, "file");
const commandProps = {
Bucket: bucketName,
Key: uploadPath + file.originalname,
Body: file.buffer,
ContentType: file.mimetype,
};
const uploadFileCommand = new PutObjectCommand(commandProps);
let data;
try {
data = await s3.send(uploadFileCommand);
} catch (error) {
console.log(error);
// const { requestId, cfId, extendedRequestId } = error.$$metadata;
// console.log({ requestId, cfId, extendedRequestId });
}
return data;
};
我想知道为什么它会抛出一个错误,以及解决这个错误的方法是什么。
1条答案
按热度按时间7eumitmz1#
确保导入的dotenv模块
这是一个简单的代码单元,因为我正在使用数字海洋桶,它看起来像这样,它的工作。