所以我一直在關注stackoverflow和AWS SDK檔案的其他問答,但我仍然無法使用以下代碼洗掉S3檔案,它只是給出了以下錯誤Error TypeError: Cannot read properties of undefined (reading 'byteLength')
我的代碼(s3Client.js):
import { S3Client } from "@aws-sdk/client-s3";
const REGION = `${process.env.S3_UPLOAD_REGION}`;
const creds = {
accessKeyId: process.env.S3_UPLOAD_KEY,
secretAccessKey: process.env.S3_UPLOAD_SECRET
};
// Create an Amazon S3 service client object.
const s3Client = new S3Client({
region: REGION,
credentials: creds
});
export { s3Client };
我的 nextJS 組件:
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
import { s3Client } from "../lib/s3Client.js"
const bucketParams = { Bucket: "my bucket name...", Key: "my key..." };
const run = async () => {
try {
const data = await s3Client.send(new DeleteObjectCommand(bucketParams));
console.log("Success. Object deleted.", data);
return data; // For unit tests.
} catch (err) {
console.log("Error", err);
}
};
我從其他人那里了解到這似乎是一個憑證問題,但我仍然無法解決問題所在。
編輯:這個問題的解決方案 -
- 檢查 .env,具體取決于您使用的是 React 還是 NextJS,您需要擁有“REACT_PUBLIC”或“NEXT_PUBLIC”才能通過 .env 公開環境物件
process.env。 - 檢查您在 S3 存盤桶中的權限并設定“AllowedOrigins”以包含您的本地主機。我有我的設定,
"AllowedOrigins": "*"但這還不夠。所以我已經包括在內http://localhost:3000并且它起作用了。
uj5u.com熱心網友回復:
因此,您似乎正在為此使用密鑰憑據。要除錯您的問題,您應該做的第一件事是檢查代碼和 SDK 外部的憑據并確保它們沒問題。
為此,請通過設定環境變數在 CLI 上設定憑據。
export AWS_ACCESS_KEY_ID=<Your Key here>
export AWS_SECRET_ACCESS_KEY=<SECRET HERE>
export AWS_DEFAULT_REGION=<AWS REGION>
參考:https ://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
在此之后運行此命令以驗證憑據設定是否正確aws sts get-caller-identity
如果他們向您顯示帳戶和用戶詳細資訊。然后使用 CLI 洗掉該檔案。如果可行,則確認問題不在于憑據和代碼。如果沒有,它將為您指出問題所在的正確方向。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/443934.html
