2015-04-16 200 views
0

我正嘗試使用javascript將文檔/圖像上傳到S3存儲桶。我在做手術時遇到了CORS問題。用javascript:cors問題將文檔上傳到amazon s3存儲桶

<script src="https://sdk.amazonaws.com/js/aws-sdk-2.0.0-rc13.min.js"> </script> 
<script type="text/javascript"> 
    // See the Configuring section to configure credentials in the SDK 
    // AWS.config.credentials = ...; 
    AWS.config.update({ accessKeyId: 'mykeyid', secretAccessKey: 'mysecretkey' }); 

    // Configure your region 
    AWS.config.region = 'us-west-2'; 

<input type="file" id="file-chooser" /> 
<button id="upload-button">Upload to S3</button> 
<div id="results"></div> 

<script type="text/javascript"> 
    var bucket = new AWS.S3({ params: { Bucket: 'ard1'} }); 

    var fileChooser = document.getElementById('file-chooser'); 
    var button = document.getElementById('upload-button'); 
    var results = document.getElementById('results'); 
    button.addEventListener('click', function() { 
     var file = fileChooser.files[0]; 
     if (file) { 
      results.innerHTML = ''; 
      var cal_key = file.name; 
      var params = { Key: cal_key, ContentType: file.type, Body: file }; 
      bucket.putObject(params, function (err, data) { 

       results.innerHTML = err ? (err+data) : 'UPLOADED.'; 
      }); 
     } else { 
      results.innerHTML = 'Nothing to upload.'; 
     } 
    }, false); 

對亞馬遜鬥我Cors設定爲

<?xml version="1.0" encoding="UTF-8"?> 
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> 
<CORSRule> 
    <AllowedOrigin>*</AllowedOrigin> 
    <AllowedMethod>HEAD</AllowedMethod> 
    <AllowedMethod>GET</AllowedMethod> 
    <AllowedMethod>PUT</AllowedMethod> 
    <AllowedMethod>POST</AllowedMethod> 
    <AllowedMethod>DELETE</AllowedMethod> 
    <ExposeHeader>ETag</ExposeHeader> 
    <ExposeHeader>x-amz-meta-custom-header</ExposeHeader> 
    <AllowedHeader>*</AllowedHeader> 
</CORSRule> 
</CORSConfiguration> 

它提供了以下錯誤:

XMLHttpRequest cannot load https://ard1.s3-us-west-2.amazonaws.com/Hydrangeas.jpg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:59213' is therefore not allowed access. 

回答

1

嘗試加入這個..

<AllowedOrigin>http://*</AllowedOrigin> 
<AllowedOrigin>https://*</AllowedOrigin> 

而且,好像在桶中的對象沒有更新到最新的CORS文件...試圖找到一種方法來更新現有的所有對象的CORS信息。