AWS初心者です。下記のサイトを参考にしながら設定をしたのですがうまくアップロードされません。cors設定が原因のような気がしますが。。すみませんがご助力の方宜しくお願いします。
エラー内容
Access to XMLHttpRequest at 'https://foliojs.s3.amazonaws.com/file1617874791820audio/mpeg' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
PUT https://foliojs.s3.amazonaws.com/file1617874791820audio/mpeg net::ERR_FAILED
javascript
1コード 2 <script src="https://sdk.amazonaws.com/js/aws-sdk-2.881.0.min.js"></script> 3 4var s3_client = function() { 5 AWS.config.region = "us-east-1"; 6 AWS.config.credentials = new AWS.CognitoIdentityCredentials({ 7 IdentityPoolId: "us-east-1:bc" //仮設定 8 }); 9 AWS.config.credentials.get(function(err) { 10 if (!err) { 11 console.log("Cognito Identify Id: " + AWS.config.credentials.identityId); 12 } 13 }); 14 return new AWS.S3({ 15 params: { 16 Bucket: "foliojs" 17 } 18 }); 19 }; 20 21 22 var file = sliceData 23 var timestamp = new Date().getTime(); 24 var filename = "file" + timestamp + "audio/mpeg"; 25 s3_client().putObject({ 26 Key: filename, 27 ContentType: 'audio/mpeg', 28 Body: file, 29 ACL: "public-read" 30 }, 31 function(err, data) { 32 // if failed, alert 33 if (data !== null) { 34 alert("アップロード成功!"); 35 } else { 36 alert("アップロード失敗."); 37 } 38 });
cors
1コード 2[ 3 { 4 "AllowedHeaders": [ 5 "*" 6 ], 7 "AllowedMethods": [ 8 "PUT", 9 "POST", 10 "DELETE" 11 ], 12 "AllowedOrigins": [ 13 "http://www.example1.com" 14 ], 15 "ExposeHeaders": [] 16 }, 17 { 18 "AllowedHeaders": [ 19 "*" 20 ], 21 "AllowedMethods": [ 22 "PUT", 23 "POST", 24 "DELETE" 25 ], 26 "AllowedOrigins": [ 27 "http://www.example2.com" 28 ], 29 "ExposeHeaders": [] 30 }, 31 { 32 "AllowedHeaders": [], 33 "AllowedMethods": [ 34 "GET" 35 ], 36 "AllowedOrigins": [ 37 "*" 38 ], 39 "ExposeHeaders": [] 40 } 41]
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/09 09:00