用邮递员呼叫S3预签名URL

编程入门 行业动态 更新时间:2024-10-23 02:03:50
本文介绍了用邮递员呼叫S3预签名URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用预签名URL进行上传,如docs中所述(

在word文件的右侧,如果您单击下拉列表,则可以浏览到您的文件并将其附加:

如果有帮助,我正在使用用python编写的lambda生成一个预签名的URL,以便用户可以上传附件.代码如下:

signedURL = self.s3.generate_presigned_post(Bucket ="my-s3-bucket",密钥= putkey,字段= {"acl":公共读取","Content-Type":"multipart/form-data"},ExpiresIn = 15条件= [{"acl":公开阅读"},["content-length-range",1,5120000]])

希望这会有所帮助.

I am attempting to use a pre-signed URL to upload as described in the docs (docs.aws.amazon/AmazonS3/latest/dev/PresignedUrlUploadObject.html) I can retrieve the pre-signed URL but when I attempt to do a PUT in Postman, I receive the following error:

<Code>SignatureDoesNotMatch</Code> <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>

Obviously, the way my put call is structured doesn't match with the way AWS is calculating the signature. I can't find a lot of information on what this put call requires.

I've attempted to modify the header for Content-Type to multipart/form-data and application/octet-stream. I've also tried to untick the headers section in postman and rely on the body type for both form-data and binary settings where I select the file. The form-data setting results in the following added to the call:

Content-Disposition: form-data; name="thefiletosend.txt"; filename="thefiletosend.txt

In addition, I noticed that postman is including what it calls "temporary headers" as follows:

Host: s3.amazonaws Content-Type: text/plain User-Agent: PostmanRuntime/7.13.0 Accept: / Cache-Control: no-cache Postman-Token: e11d1ef0-8156-4ca7-9317-9f4d22daf6c5,2135bc0e-1285-4438-bb8e-b21d31dc36db Host: s3.amazonaws accept-encoding: gzip, deflate content-length: 14 Connection: keep-alive cache-control: no-cache

The Content-Type header may be one of the issues, but I'm not certain how to exclude these "temporary headers" in postman.

I am generating the pre-signed URL in a lambda as follows:

public string FunctionHandler(Input input, ILambdaContext context) { _logger = context.Logger; _key = input.key; _bucketname = input.bucketname; string signedURL = _s3Client.GetPreSignedURL(new GetPreSignedUrlRequest() { Verb = HttpVerb.PUT , Protocol = Protocol.HTTPS, BucketName = _bucketname, Key = _key, Expires = DateTime.Now.AddMinutes(5) }); returnObj returnVal = new returnObj() { url = signedURL }; return JsonConvert.SerializeObject(returnVal); }

解决方案

I was able to get this working in Postman using a POST request. Here are the details of what worked for me. When I call my lambda to get a presigned URL here is the json that comes back (after I masked sensitive and app-specific information):

{ "attachmentName": "MySecondAttachment.docx", "url": "my-s3-bucket.s3.amazonaws/", "fields": { "acl": "public-read", "Content-Type": "multipart/form-data", "key": "attachment-upload/R271645/65397746_MySecondAttachment.docx", "x-amz-algorithm": "AWS4-HMAC-SHA256", "x-amz-credential": "WWWWWWWW/20200318/us-east-1/s3/aws4_request", "x-amz-date": "20200318T133309Z", "x-amz-security-token": "XXXXXXXX", "policy": "YYYYYYYY", "x-amz-signature": "ZZZZZZZZ" } }

In Postman, create a POST request, and use "form-data" to enter in all the fields you got back, with exactly the same field names you got back in the signedURL shown above. Do not set the content type, however. Then add one more key named "file":

To the right of the word file if you click the drop-down you can browse to your file and attach it:

In case it helps, I’m using a lambda written in python to generate a presigned URL so a user can upload an attachment. The code looks like this:

signedURL = self.s3.generate_presigned_post( Bucket= "my-s3-bucket", Key=putkey, Fields = {"acl": "public-read", "Content-Type": "multipart/form-data"}, ExpiresIn = 15, Conditions = [ {"acl": "public-read"}, ["content-length-range", 1, 5120000] ] )

Hope this helps.

更多推荐

用邮递员呼叫S3预签名URL

本文发布于:2023-10-29 08:04:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:邮递员   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!