2014-09-20 106 views
0

我已經編寫代碼,用於直接從瀏覽器使用角度js將圖像上傳到亞馬遜S3。爲此我使用庫aws-sdk.js。我有一個函數「上傳」,我在其中分配證書和參數,以及文件。雖然上傳,我得到下面的錯誤直接從瀏覽器上傳圖像到亞馬遜s3 - 使用角度js

*No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access*. 

我CORS配置

<?xml version="1.0" encoding="UTF-8"?> 
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> 
    <CORSRule> 
     <AllowedOrigin>http://*</AllowedOrigin> 
     <AllowedOrigin>https://*</AllowedOrigin> 
     <AllowedMethod>PUT</AllowedMethod> 
     <MaxAgeSeconds>3000</MaxAgeSeconds> 
     <ExposeHeader>x-amz-server-side-encryption</ExposeHeader> 
     <ExposeHeader>x-amz-request-id</ExposeHeader> 
     <ExposeHeader>x-amz-id-2</ExposeHeader> 
     <AllowedHeader>*</AllowedHeader> 
    </CORSRule> 
</CORSConfiguration> 

我的控制器功能

function _upload($files) { 
    $scope.file = $files[0]; 
    $scope.creds = { 
     accessKeyId: '...', 
     secretAccessKey: '...', 
     region: 'us-west-2', 
     bucket: 'sabari-test' 
    }; 


    var bucket = new AWS.S3({ 
     region: 'us-west-2', 
     credentials: new AWS.Credentials($scope.creds.accessKeyId, $scope.creds.secretAccessKey) 
    }); 

    if ($scope.file) { 
     // Perform File Size Check First 
     var fileSize = Math.round(parseInt($scope.file.size)); 
     if (fileSize > $scope.sizeLimit) { 
      console.log('Sorry, your attachment is too big.'); 
      return false; 
     } 
     // Prepend Unique String To Prevent Overwrites 
     var uniqueFileName = 'hai' + '-' + $scope.file.name; 

     var params = { 
      Bucket: $scope.creds.bucket, 
      Key: uniqueFileName, 
      ContentType: $scope.file.type, 
      Body: $scope.file, 
      ServerSideEncryption: 'AES256' 
     }; 

     bucket.putObject(params, function(err, data) { 
      if (err) { 
       console.log(err.message); 
       return false; 
      } else { 
       // Upload Successfully Finished 
       console.log('File Uploaded Successfully'); 
      } 
     }) 
    } else { 
     // No File Selected 
     console.log('Please select a file to upload'); 
    } 
} 

文件在我的水桶 - 公開 - 跨域.xml

<cross-domain-policy> 
<allow-access-from domain="*" secure="false"/> 
</cross-domain-policy> 

我需要做些什麼來擺脫第一個錯誤?謝謝。

回答

0

爲了擺脫錯誤的,我必須安裝CORS ..

NPM安裝CORS

除此之外,我在我的區域是錯誤的設置爲美國 - 東 - 1這是我們早期的西 - 2