简体   繁体   English

使用AWS PreSignedURL将图像上传到Amazon S3

[英]uploading an image to Amazon S3 using AWS PreSignedURL

I am generating presigned URL with HttpVerb as PUT to upload file to my bucket. 我正在使用HttpVerb作为PUT生成预签名的URL,以将文件上传到我的存储桶。 Strange thing is that when I am using AWSDK to upload file using preseigned URL then it is working fine. 奇怪的是,当我使用AWSDK通过预先指定的URL上传文件时,它工作正常。 But when I am using this URL in the form action method then I am getting the below error. 但是,当我在表单操作方法中使用此URL时,出现以下错误。 "The request signature we calculated does not match the signature you provided. Check your key and signing method." “我们计算出的请求签名与您提供的签名不匹配。请检查您的密钥和签名方法。”

Below is the HTML code to upload the file 以下是上传文件的HTML代码

    <html> 
  <head>
    <title>S3 POST Form</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>

  <body> 
    <form action="https://mvrskt-certifications.s3-ap-southeast-1.amazonaws.com/Temp/ARMA.jpg?AWSAccessKeyId=AKIAIGPD33Q3DT22IFWQ&Expires=1451482258&Signature=nYx%2F2kDmna5XqEWx5CPTg4OIYDU%3D" method="post" enctype="multipart/form-data">
      <input type="hidden" name="key" value="mvrskt-certifications/Temp/ARMA.jpg">
      <input type="hidden" name="Content-Type" value="image/jpeg">
      <!-- Include any additional input fields here -->

      File to upload to S3: 
      <input name="file" type="file"> 
      <br> 
      <input type="submit" value="Upload File to S3"> 
    </form> 
  </body>
</html>

Below is the C#.NET code which I am using to generate the PreSignedURL 以下是我用来生成PreSignedURL的C#.NET代码

GetPreSignedUrlRequest request = new GetPreSignedUrlRequest
                {
                    BucketName = bucketName,
                    Key        = objectKey,
                    Verb       = HttpVerb.PUT,
                    Expires    = DateTime.Now.AddMinutes(15)
                };

            string url = null;
           request.ContentType = "image/jpeg";
            url = s3Client.GetPreSignedURL(request);

I have set CORS policy on my bucket as mentioned below 我已在我的存储桶上设置了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>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>Authorization</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

I am new to AWS S3 and getting totally confuse on using this.... pl assist on how to upload this file using form post OR AJAX 我是AWS S3的新手,在使用它时完全感到困惑。...请协助使用表单发布或AJAX上载此文件

Morever do we need to generate the PreSignedURL for each image which I am going to upload? 另外,我们需要为要上传的每个图像生成PreSignedURL吗?

I can see that we can use signed policy also to upload the images but do I need to generate the signed policy for each image which I am going to upload 我可以看到我们也可以使用签名策略来上传图像,但是我是否需要为要上传的每个图像生成签名策略?

A pre-signed URL is specific to the key (path/file in the bucket), the HTTP verb ( PUT ), and other attributes of the request you're going to make. 预签名URL特定于密钥(存储桶中的路径/文件),HTTP动词( PUT )以及您要发出的请求的其他属性。

A form POST is not an HTTP PUT request, so that is the simple explanation of why you can't use a pre-signed URL for a PUT request when you're making a POST . 表单POST不是HTTP PUT请求,因此这是为什么在进行POST时为什么不能对PUT请求使用预签名URL的简单说明。

Not only are the verbs not interchangeable, a browser-based POST upload is an entirely different operation than PUT , and requires the signed policy statement to indicate the acceptable values for the various form fields. 这些动词不仅不可互换,而且基于浏览器的POST上传操作与PUT完全不同,并且要求签名策略声明指示各种表单字段的可接受值。 You can't upload from a form without that. 没有它,您将无法从表单上载。

Yes, you should generate the policy and signature for each file you want to upload, otherwise you expose yourself to a malicious user overwriting a file other than the one you intended for them to be able to write. 是的,您应该为要上传的每个文件生成策略和签名,否则,您将自己暴露给恶意用户,该恶意用户将覆盖文件(而不是您希望其能够写入的文件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM