我正在使用ember-uploader将文件上传到Amazon S3。该组件非常简单:
import Ember from 'ember';
import EmberUploader from 'ember-uploader';
export default EmberUploader.FileField.extend({
signingUrl: '/sign',
filesDidChange (files) {
const uploader = EmberUploader.S3Uploader.create({
signingUrl: this.get('signingUrl'),
});
uploader.on('didUpload', response => {
console.log('Upload successful!');
});
if (!Ember.isEmpty(files)) {
uploader.upload(files[0]);
}
}
});
然后我编写了一个简单的节点表达脚本,用于生成预先签名的URL:
var aws = require('aws-sdk');
var config = new aws.Config({
accessKeyId: 'ACCESS-KEY-ID',
secretAccessKey: 'SECRET-ACCESS-KEY',
region: 'us-east-1',
});
aws.config = config;
var bucketName = 'BUCKET-NAME';
var express = require('express');
var app = express();
app.use(fileUpload());
app.use(function(req, res, next) {
req.headers['if-none-match'] = 'no-match-for-this';
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.get('/sign', function (req, res) {
const s3 = new aws.S3();
const fileName = req.query['name'];
const fileType = req.query['type'];
const s3Params = {
Bucket: bucketName,
Key: fileName,
Expires: 60,
ContentType: fileType,
ACL: 'public-read'
};
s3.getSignedUrl('putObject', s3Params, (err, data) => {
if(err){
console.log(err);
return res.end();
}
console.log('data =>', data);
const returnData = {
signedData: data,
endpoint: `https://${bucketName}.s3.amazonaws.com/${fileName}`,
};
res.write(JSON.stringify(returnData));
res.end();
});
});
当我尝试上传文件时,该组件会生成 POST请求到https://BUCKET-NAME.s3.amazonaws.com/FILE.FILE-FORMAT
响应为405方法不允许
执行PUT请求会使此请求正常工作,这是不寻常的。但是,ember-uploader似乎只对s3上传做了POST请求。
我的S3存储桶策略是:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "StmtXXXXXXX",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::ingagedtest2",
"arn:aws:s3:::ingagedtest2/*"
]
}
]
}
我的S3 Bucket CORS配置是:
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
我正在http://localhost:3000服务器上对此进行测试。任何指向正确方向的人都会非常感激!我无法弄清楚我错过了什么。