简体   繁体   English

从AWS S3存储桶下载文件时,如何避免“网络错误:网络故障”?

[英]How can I avoid “Networking Error: Network Failure” when downloading a file from AWS S3 bucket?

I am using AWS S3 to access a file in my HTML5 application. 我使用AWS S3访问HTML5应用程序中的文件。 Following code works fine on my laptop but it fails on the mobile device (iphone). 以下代码在我的笔记本电脑上工作正常,但在移动设备(iphone)上失败。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

 AWS.config.region = 'us-west-2';
 var bucket = new AWS.S3({params: {Bucket: 'mybucket'}});
 bucket.getObject({Key: myfile}, function (error, data) {

 if (error != null) {
    alert("Failed to retrieve " + myfile + " :" + error);
 } else {
     //alert("Loaded " + data.ContentLength + " bytes");
     localStorage[myfile] = data.Body.toString();
     // do something with data.body

 }}); 

The error I get is: "http //local host Failed to retrieve foo.json :NetworkingError: Network Failure" 我得到的错误是:“http //本地主机无法检索foo.json:NetworkingError:网络故障”

I have the followings CORS configuration for this bucket 我有这个桶的以下CORS配置

<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
</CORSRule>

I was able to get this resolved by aws tech support. 我能够通过aws技术支持解决这个问题。 Issue got resolved with the following CORSconfig: 使用以下CORSconfig解决了问题:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>GET</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM