我正在使用AWS javascript浏览器版本将文件上传到AWS S3服务器。我收到以下错误:
XMLHttpRequest无法加载
https://s3-ap-southeast-2.amazonaws.com/cortex.myauscript.upload/uploadcontent-upload-development/TestFileName.txt。
对预检请求的响应未通过访问控制检查:否
请求中存在“ Access-Control-Allow-Origin”标头
资源。因此,不允许访问原始“空”。响应
HTTP状态码为403。
我搜索了此错误,但可以确认该存储桶上的CORS设置是否正确设置。所以我有点困惑。
下面是我的代码:
var bucketName = 'cortex.myauscript.upload';
AWS.config.region = 'ap-southeast-2';
AWS.config.update({
accessKeyId : 'test',
secretAccessKey : 'test'
});
var bucket = new AWS.S3({
params: {
Bucket: bucketName
}
});
var fileChooser = document.getElementById('file-chooser');
var button = document.getElementById('upload-button');
var results = document.getElementById('results');
$("#upload-button").click(function() {
var file = fileChooser.files[0];
if(file) {
var params = {
Key: 'uploadcontent-upload-development/TestFileName.txt',
ContentType: file.type,
Body: file
};
bucket.upload(params).on('httpUploadProgress', function(evt){
console.log("Uploaded :: " + parseInt((evt.loaded * 100) / evt.total)+'%');
}).send(function(err, data) {
alert("File uploaded successfully.");
});
}
});
我也有点纳闷,错误的密钥/安全密钥会产生相同的错误。我对此不太确定。
谢谢。
最佳答案
看来您是从file://
而不是http://
运行您的应用程序,这会产生无法授权的null
来源。