javascript - Amazon S3 file upload: POST 405 Method Not Allowed using ember-uploader -
i'm using ember-uploader upload file amazon s3. component simple:
import ember 'ember'; import emberuploader '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]); } } });
i've written simple node express script generates pre-signed 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(); }); });
when try upload file, component generates post request https://bucket-name.s3.amazonaws.com/file.file-format
the response 405 method not allowed
it's unusual doing put request makes request work fine. however, ember-uploader appears post requests s3 uploads.
my s3 bucket policy is:
{ "version": "2012-10-17", "statement": [ { "sid": "stmtxxxxxxx", "effect": "allow", "principal": "*", "action": "s3:*", "resource": [ "arn:aws:s3:::ingagedtest2", "arn:aws:s3:::ingagedtest2/*" ] } ] }
my s3 bucket cors configuration is:
<corsconfiguration> <corsrule> <allowedorigin>*</allowedorigin> <allowedmethod>get</allowedmethod> <allowedmethod>post</allowedmethod> <allowedheader>*</allowedheader> </corsrule> </corsconfiguration>
i'm testing on http://localhost:3000 server. pointers in right direction appreciated! cannot figure out missing.
Comments
Post a Comment