我正在使用 javascript amazon sdk 将内容上传到亚马逊。我尝试了这个例子。这是我的代码。
<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: 'keyid', secretAccessKey: 'secretkey'});
// Configure your region
AWS.config.region = 'us-west-2';
</script>
<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: 'bucket_name'}});
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 ? 'ERROR!' : 'UPLOADED.';
});
} else {
results.innerHTML = 'Nothing to upload.';
}
}, false);
</script>
但是我收到了这个错误
但是我收到了这个错误
XMLHttpRequest cannot load https://bucket_name.s3-us-west-2.amazonaws.com/Chrysanthemum.jpg. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://testing.com' is therefore not allowed access.
经过一些研究,我知道这是一个跨域问题。但我不知道如何解决这个问题。此代码直接取自亚马逊文档
最佳答案
这实际上是一个 CORS 问题。你检查过S3的这部分吗documentation .
这个link一定也很有用:)
关于javascript - 使用 javascript sdk 的亚马逊 s3 上传无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215262/