简体   繁体   English

TypeError:无法读取未定义的属性“byteLength”-AWS S3 上传

[英]TypeError: Cannot read property 'byteLength' of undefined - AWS S3 Upload

I'm working on an aws s3 photo upload from a react client and I'm experiencing the following error:我正在处理来自 react 客户端的 aws s3 照片上传,但遇到以下错误:

TypeError: Cannot read property 'byteLength' of undefined

I'm assuming there's a flaw in the upload object, but I believe there might be something wrong with the s3/cognito configuration because I receive the same error when I invoked s3.listObjects .我假设上传 object 存在缺陷,但我相信 s3/cognito 配置可能有问题,因为我在调用s3.listObjects时收到相同的错误。 I'm following these docs - https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album-full.html我正在关注这些文档 - https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album-full.html

Any thoughts?有什么想法吗?

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);
          }
        );
      }

I got this error in a React Native app.我在 React Native 应用程序中遇到了这个错误。 I was able to fix it by turning off my Dev Tools network inspector.我可以通过关闭我的 Dev Tools 网络检查器来修复它。

I've run the example as described in the docs and it works, so can't reproduce your err.我已经按照文档中的描述运行了该示例并且它有效,因此无法重现您的错误。 Here's a couple of things to check:这里有几件事要检查:

  • Try passing your accesskey and secret into your config object (although I don't recommend this for security reasons, it might indicate where the issue is):尝试将您的访问密钥和秘密传递到您的配置 object 中(尽管出于安全原因我不推荐这样做,但它可能表明问题出在哪里):
AWSService.config.update({
    region: region,
    accessKeyId: accessKeyId,
    secretKey: secretAccessKey,
    credentials: new AWSService.CognitoIdentityCredentials({
    IdentityPoolId: identityPoolId
    })
});

  • Confirm your your Cognito Identity Pool ID has an IAM policy attached with the following:确认您的 Cognito 身份池 ID 具有附加以下内容的 IAM 策略:
    "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/*"
            ]
        }
    ]
}

Just in case.以防万一。 Check that your credentials are configured and passed right.检查您的凭据是否已配置并正确传递。 And that you have the privileges for the resource in IAM.并且您拥有 IAM 中资源的权限。

I had the error above because i forget to pass the access credentials.我有上面的错误,因为我忘记传递访问凭据。 It went away once i put them in.一旦我把它们放进去,它就消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件上传到 AWS S3/Amazon 错误:无法读取未定义的属性(读取“byteLength”) - Upload file to AWS S3/Amazon error: Cannot read properties of undefined (reading 'byteLength') TypeError:尝试上传图像时无法读取未定义的属性“路径” - TypeError: Cannot read property 'path' of undefined when trying to upload image TypeError:无法读取未定义的属性“名称”,但可以使用另一个属性 - TypeError: Cannot read property 'name' of undefined, but it's OK with another property TypeError:无法读取未定义的属性“s_path”(React Native) - TypeError: Cannot read property 's_path' of undefined (React Native) 类型错误:在使用 spotify 的 api 时无法读取未定义的属性 - TypeError: Cannot read property of undefined while using spotify's api TypeError:无法读取未定义的属性“未定义” - TypeError: Cannot read property 'undefined' of undefined 类型错误:无法将 undefined 或 null 转换为对象 React JS(S3 分段文件上传) - TypeError: Cannot convert undefined or null to object React JS (S3 multipart file upload) 未捕获的TypeError:无法读取未定义的属性&#39;onDismiss&#39; - Uncaught TypeError: Cannot read property 'onDismiss' of undefined 未被捕获的TypeError:无法读取未定义的属性“ form” - Uncaught TypeError: Cannot read property 'form' of undefined TypeError:无法读取未定义的属性setState - TypeError: cannot read property setState of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM