[英]Direct upload to S3 with JavaScript not working
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>s3</title>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.1.34.min.js"></script>
<script type="text/javascript">
// See the Configuring section to configure credentials in the SDK
AWS.config.credentials = new AWS.Credentials({
accessKeyId: 'MY ACCESS KEY',
secretAccessKey: 'MY SECRET KEY'
});
// Configure your region
AWS.config.region = 'us-west-2';
</script>
</head>
<body>
<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: 'MY 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 params = {Key: file.name, ContentType: file.type, Body: file};
bucket.upload(params, function (err, data) {
results.innerHTML = err ? 'ERROR!' : 'UPLOADED.';
});
} else {
results.innerHTML = 'Nothing to upload.';
}
}, false);
</script>
</body>
</html>
The above is the code I'm using, but when I select a file and upload it, I get these errors on the console: 上面是我正在使用的代码,但是当我选择一个文件并将其上传时,在控制台上会出现以下错误:
MY_BUCKET_NAME.s3-us-west-2.amazonaws.com/burger.jpg:1 PUT https://MY_BUCKET_NAME.s3-us-west-2.amazonaws.com/burger.jpg 403 (Forbidden) MY_BUCKET_NAME.s3-us-west-2.amazonaws.com/burger.jpg:1放置https://MY_BUCKET_NAME.s3-us-west-2.amazonaws.com/burger.jpg 403(禁止)
The weird thing is... I've correctly used S3 before, and I generally notice that there is some kind of "nested structure" that gets created in the bucket, and above, the 403 happens to MY_BUCKET_NAME.s3-us-west-2.amazonaws.com/burger.jpg, which isn't in a nested structure that I'm used to. 奇怪的是...我之前已经正确使用过S3,而且我通常会注意到在存储桶中创建了某种“嵌套结构”,而在上面,403发生在MY_BUCKET_NAME.s3-us-west -2.amazonaws.com/burger.jpg,它不是我惯用的嵌套结构。
I don't know what's wrong. 我不知道怎么了
This is my CORS config: 这是我的CORS配置:
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<ExposeHeader>ETag</ExposeHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我的秘密访问密钥缺少4个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.