Arr*_*ton 15 amazon-s3 amazon-web-services reactjs aws-sdk
我正在从 React 客户端处理 aws s3 照片上传,但遇到以下错误:
TypeError: Cannot read property 'byteLength' of undefined
我假设上传对象存在缺陷,但我相信 s3/cognito 配置可能有问题,因为当我调用s3.listObjects
. 我正在关注这些文档 - https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album-full.html
有什么想法吗?
uploadPhoto() {
const files = document.getElementById("photoUpload").files;
if (!files.length) {
return alert("Please choose a file to upload first.");
}
const file = files[0];
const fileName = file.name;
const albumPhotosKey = encodeURIComponent('screenshots') + "/";
const photoKey = albumPhotosKey + fileName;
// Use S3 ManagedUpload class as it supports multipart uploads
const upload = new AWS.S3.ManagedUpload({
params: {
Bucket: <Bucket Name>,
Key: fileName,
Body: file
}
});
const promise = upload.promise();
promise.then(
function(data) {
alert("Successfully uploaded photo.");
console.log('UPLOAD: ', data)
},
function(err) {
console.log('ERROR: ', err)
// return alert("There was an error uploading your photo: ", err.message);
}
);
}
Run Code Online (Sandbox Code Playgroud)
我已经按照文档中的描述运行了示例并且它有效,因此无法重现您的错误。这里有几件事需要检查:
AWSService.config.update({
region: region,
accessKeyId: accessKeyId,
secretKey: secretAccessKey,
credentials: new AWSService.CognitoIdentityCredentials({
IdentityPoolId: identityPoolId
})
});
Run Code Online (Sandbox Code Playgroud)
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::BUCKET_NAME",
"arn:aws:s3:::BUCKET_NAME/*"
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
15970 次 |
最近记录: |