我在 Nextjs 应用程序中使用 AWS SDK V3 将文件上传到 s3。我以前没有遇到任何问题,但是当我今天更改用户的密钥时,我不断收到错误:TypeError:无法获取。这是我的应用程序中导致错误的代码:
const appId = process.env.NEXT_PUBLIC_AWS_ACCESS_KEY_ID;
const secretId = process.env.NEXT_PUBLIC_AWS_SECRET_ACCESS_KEY;
const S3_BUCKET = process.env.NEXT_PUBLIC_BUCKET_NAME;
const REGION = process.env.NEXT_PUBLIC_REGION;
const creds = {
accessKeyId: appId,
secretAccessKey: secretId,
};
const client = new S3Client({
params: { Bucket: S3_BUCKET },
region: REGION,
credentials: creds,
});
const handleUploadFiles = async (e) => {
if (e.target.files[0]) {
for (var i = 0; i < e.target.files.length; i++) {
setDocCounter((current) => current + 1);
const fileName = e.target.files[i].name;
const fileType = fileName.slice(fileName.lastIndexOf(".") + 1);
const file = e.target.files[i];
const fileId = uuidv4() + "." + fileType;
console.log(fileId);
const command = new PutObjectCommand({
// ACL: "public-read",
Body: file,
Bucket: S3_BUCKET,
Key: fileId,
});
try {
const response = await client.send(command);
console.log(response);
} catch (err) {
console.log(err);
}
}
}
setSnackbarMessage("Invoices successfully uploaded");
setUploadSnackbarOpen(true);
};
我已在 IAM 中检查了我的用户,它具有完整的 s3 权限,并且我已在 AWS CLI 中验证该用户已创建并且具有正确的访问密钥和秘密访问密钥。
这是控制台错误:
TypeError: Failed to fetch
at FetchHttpHandler.handle (fetch-http-handler.js:56:1)
at async eval (flexibleChecksumsResponseMiddleware.js:17:1)
at async eval (deserializerMiddleware.js:2:24)
任何帮助将不胜感激
更新
我已验证凭据并将其复制/粘贴到
aws configure
CLI 提示符中。然后我运行以下 CLI 命令,文档已成功上传到 s3 存储桶:
aws s3api put-object --bucket <myBucketName> --key <myKey> --body <myFilePath> --acl bucket-owner-full-control
我仍然没有更接近找到问题的根源,现在控制台日志中有更多详细信息:
TypeError: Failed to fetch
at FetchHttpHandler.handle (fetch-http-handler.js:56:1)
at eval (PutObjectCommand.js:56:1)
at eval (flexibleChecksumsResponseMiddleware.js:17:1)
at eval (deserializerMiddleware.js:2:24)
at eval (check-content-length-header.js:18:1)
at eval (awsAuthMiddleware.js:10:1)
at async eval (retryMiddleware.js:24:44)
at async eval (flexibleChecksumsMiddleware.js:54:1)
at async eval (loggerMiddleware.js:3:1)