【问题标题】:S3 file upload with presigned url returns status (canceled)带有预签名 url 的 S3 文件上传返回状态(已取消)
【发布时间】:2021-05-09 16:05:34
【问题描述】:

我正在尝试使用 Angular 客户端将文件上传到 Amazon S3。我使用 NodeJs 应用程序服务器生成了预签名 URL。将文件上传到预签名 URL,但无法从客户端上传文件,获取(取消)。

我尝试上传以下格式的文件:bufferbase64formData 和原始 File

如果我尝试使用邮递员将二进制形式上传到生成的 URL,这将有效

使用 NodeJs

生成 Presigned URL
      const s3 = new AWS.S3({
         accessKeyId: AWS_ACCESS_KEY,
         secretAccessKey: AWS_SECRET_ACCESS_KEY,
         region: 'eu-central-1',
      });
      const signedUrlExpireSeconds = 60 * 5;
      const presignedS3Url = s3.getSignedUrl('putObject', {
         Bucket: process.env.bucket,
         Key: './test3Public.pdf',
         Expires: signedUrlExpireSeconds,
         ACL: 'public-read',
         ContentType: 'application/pdf',
      });

HTML

<input
      type="file"
      (change)="onFileSelected($event)"
      accept="application/pdf, .docx"
   />

组件.ts

onFileSelected(event: any) {
      this.fileToUpload = <File>event.target.files[0];
     
      this.fileUpload
         .generatePresignedURL(this.fileToUpload.name)
         .pipe(first())
         .subscribe(
            (data) => {
               this.fileUpload
                  .uploadfileAWSS3(this.fileToUpload, data.presignedS3Url)
                  .pipe(first())
                  .subscribe(
                     (data) => {
                        console.log('uploaded', data);
                     },
                     (error) => {
                        console.log('error', error);
                     }
                  );
            },
            (error) => {
               console.log('error', error);
            }
         );
   }

我发送文件的格式:

Angular 11 服务

 uploadfileAWSS3(file, fileuploadurl) {
      const req = new HttpRequest('PUT', fileuploadurl, file);
      return this.http.request(req);
   }

您能帮我解决上传被取消的问题吗?

【问题讨论】:

  • 是否在 S3 上设置了 CORS 配置?
  • 是的,如果不是邮递员也应该抛出错误。
  • 好的..邮递员不会抛出 cors 错误,它只是浏览器。如果您正确设置了 CORS.. 我想不出它在邮递员中有效但在角度代码中无效的其他原因
  • 我添加了我正在使用的代码的额外信息
  • 我在代码中找不到问题,但我只是尝试了类似的代码来从 angular 上传 pdf,让我添加一些细节来看看它是否有帮助。

标签: angular amazon-web-services amazon-s3 client pre-signed-url


【解决方案1】:

HTML:

    <input type="file" (change)="onFileSelected($event)" accept="application/pdf, .docx"
    />

角度代码:

  onFileSelected(event) {
    const fileToUpload = <File>event.target.files[0];
    const bucketParms = {
      Bucket: "sample-temp-bucket",
      Key: fileToUpload.name,
      ContentType: "application/pdf",
      ACL: "public-read",
      Expires: 3600,
    };
    s3.getSignedUrl("putObject", bucketParms, (error, url) => {
      if (error) console.log("error", error);
      if (url) {
        this.http.put(url, fileToUpload).subscribe((response) => {
          console.log("response receved is ", response);
        });
      }
    });
  }

CORS:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST"
        ],
        "AllowedOrigins": [
            "http://localhost:4200"
        ],
        "ExposeHeaders": []
    }
]

【讨论】:

  • 这个方法对你有用吗?如果可以,您能分享一下您的 CORS 配置吗?
  • 是的,它的工作代码。刚刚用 CORS 更新
  • 我在“AllowedOrigins”上缺少 http://:[“localhost:4200”],非常感谢
  • 太棒了!很高兴我能帮上忙。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 1970-01-01
  • 2020-04-22
  • 2020-09-30
  • 2018-10-20
相关资源
最近更新 更多