S3.getSignedUrl to accept multiple content-type

五迷三道 提交于 2019-12-12 16:10:10

问题


I'm using the react-s3-uploader node package, which takes in a signingUrlfor obtaining a signedUrl for storing an object into S3.

Currently I've configured a lambda function (with an API Gateway endpoint) to generate this signedUrl. After some tinkering, I've got it to work, but noticed that I have to define in my lambda function the content-type, which looks like this:

var AWS = require('aws-sdk');
const S3 = new AWS.S3()
AWS.config.update({
  region: 'us-west-2'
})

exports.handler = function(event, context) {
  console.log('context, ', context)
  console.log('event, ', event)
  var params = {
    Bucket: 'video-bucket',
    Key: 'videoname.mp4',
    Expires: 120,
    ACL: 'public-read',
    ContentType:'video/mp4'
  };
  S3.getSignedUrl('putObject', params, function (err, url) {
    console.log('The URL is', url);
    context.done(null, {signedUrl: url})
  });  
}

The issue is that I want this signed url to be able to accept multiple types of video files, and I've tried setting ContentType to video/*, which doesn't work. Also, because this lambda endpoint isn't what actually takes the upload, I can't pass in the filetype to this function beforehand.


回答1:


You'll have to find a way to discover the file type and pass it to the Lambda function as an argument. There isn't an alternative, here, with a pre-signed PUT.

The request signing process for PUT has no provision for wildcards or multiple/alternative values.




回答2:


In case anyone else is looking for a working answer, I eventually found out that react-s3-uploader does pass the content-type and filename over to the getSignin url (except I had forgotten to pass the query through in API Gateway earlier), so I was able to extract it as event.params.querystring.contentType in lambda.

Then in the params, I simply set {ContentType: event.params.querystring.contentType} and now it accepts all file formats.



来源:https://stackoverflow.com/questions/43192068/s3-getsignedurl-to-accept-multiple-content-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!