【问题标题】:post request is not working to upload files to S3 pre-signed URL but put works using angular HttpClient发布请求无法将文件上传到 S3 预签名 URL,但使用 Angular HttpClient 放置工作
【发布时间】:2019-12-25 21:29:28
【问题描述】:

我想将文件上传到我使用后端应用程序生成的 S3 预签名 URL。对于前端,我使用的是 Angular HttpClient v8.2.9 当我使用 put request 时,它的代码看起来像这样。

const formData = new FormData();
           formData.append('file', file, file.name);
           this.http.put(preSignedUrl, formData, {
             responseType: 'text'
           }).subscribe(respond => {
                 console.log('__________OK_________');
                 console.log(respond);
               },
               error => {
                 console.log('__________ERROR_________');
                 console.log(error);
               });

但是当我使用发布请求时,我总是收到一个错误提示

我们计算的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。

post 请求的 cod 是这样的。

const formData = new FormData();
           formData.append('file', file, file.name);
           this.http.post(preSignedUrl, formData, {
             responseType: 'text'
           }).subscribe(respond => {
                 console.log('__________OK_________');
                 console.log(respond);
               },
               error => {
                 console.log('__________ERROR_________');
                 console.log(error);
               });
         });

由于生成的预签名 URL 也可以用于我自己的微服务,它能够通过发布请求接收文件,我不能使用亚马逊库​​在前端上传文件,必须是通用的。上面的发布请求示例正在使用我很满意的微服务,并且我想让 S3 也与帖子一起使用。所以我不必根据上传到的位置更改我的请求。

这是我为 S3 生成预​​签名 URL 的 java 后端代码

AmazonS3 s3client = makeClient();
                Date expirationTime = calculateExpirationTime();
                GeneratePresignedUrlRequest generatePresignedUrlRequest =
                        new GeneratePresignedUrlRequest(bucketName, fullPath, HttpMethod.PUT);
                                    generatePresignedUrlRequest.setExpiration(expirationTime);

                return s3client.generatePresignedUrl(generatePresignedUrlRequest).toString();

如果我将 HttpMethod 的后端代码更改为 POST,我也会收到同样的错误,并且将请求放在前端也不起作用。

【问题讨论】:

    标签: angular post amazon-s3 httpclient pre-signed-url


    【解决方案1】:

    我想我又找到了问题所在。如果其他人有这个问题,我只是在这里发布。 如果我们查看 s3 SDK 的文档。要生成预签名 URL,我们不能使用 POST。

     * @param method
     *            The HTTP method (GET, PUT, DELETE, HEAD) to be used in the
     *            request when the pre-signed URL is used.
    

    所以在我的情况下,PUT 是最合适的。

    我还找到了下面的链接 https://www.intelliware.com/submitting-a-multipart-request-multipartform-data-using-put-and-spring-boot/

    上面写着的地方

    spring boot 只期望 multipart/form-data 作为 POST 请求的一部分

    所以我需要做的是,我需要重写spring方法来接受put一个分段上传的请求。

    【讨论】:

      猜你喜欢
      • 2020-10-23
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2020-11-05
      • 2019-06-01
      • 1970-01-01
      相关资源
      最近更新 更多