AWS S3 Javascript in the browser error

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

I'm trying out the AWS Javascript SDK, I wrote a simple code based on the examples to get the list of files in a bucket. But I keep getting NetworkingError: Network Failure, and can't find any references to this error in the docs.

I also get the same error when I try a getObject.

My code:

AWS.config.update({     accessKeyId : 'myaccesskey',     secretAccessKey : 'mysecretkey' }); AWS.config.region = 'us-west-2';  function list(){     var bucket = new AWS.S3({params: {Bucket: 'myBucket'}});       bucket.listObjects(function (err, data) {         if (err) {             alert(err);         } else {             document.getElementById('status').innerHTML = 'Loaded ' + data.Contents.length + ' items from S3';             for (var i = 0; i < data.Contents.length; i++) {             document.getElementById('objects').innerHTML +=                    '<li>' + data.Contents[i].Key + '</li>';              }         }       }); } 

I configured CORS to accept GET from all locations.

<CORSConfiguration>   <CORSRule>    <AllowedOrigin>*</AllowedOrigin>    <AllowedMethod>GET</AllowedMethod>  </CORSRule> </CORSConfiguration> 

What am I missing here?

回答1:

The list of regions and endpoints can be found here:

http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

Am using your same code. Please check the below version with your code ;

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.0-rc4.min.js"></script> 

Again, please double check the region. If you still receive a Network Failure error, please give me the script you're using and the bucket name so I can check on my end.

Looking forward to your reply.



回答2:

The SDK is attempting to access the bucket in the region you've configured ('us-west-2'). A networking error with S3 usually means that your bucket does not exist in the region you've configured (it is trying to connect to myBucket.s3-us-west-2.amazonaws.com).

Is that where your bucket is? If you did not select a region when you created your bucket, it is probably in the "US Standard" region, which maps to 'us-east-1' in the SDK.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!