首页 > 解决方案 > aws-amplify S3 存储上传文件,但将它们设置为“私有”,尽管有明确的公共访问配置

问题描述

我正在尝试将文件上传到具有公共访问权限的 AWS S3 存储,但尽管在代码上明确配置了公共访问权限,但文件仍被上传为私有文件。我在Angular 应用程序上使用aws-amplify包。

这是我正在使用的代码:

  public onSubmit() {
    this.form.disable();
    const contentType: string = this.imgFile.extension === 'png' ? 'image/png' : 'image/jpeg';
    // Image upload to AWS S3 storage.
    Storage.put(this.imgFile.name, this.imgFile.content, {
      progressCallback: (progress: any) => {
        console.log(`Uploaded: ${progress.loaded}/${progress.total}`);
      },
      contentType: contentType,
      ACL: 'public-read',
      visibility: 'public',
      level: 'public',
    }).then((result: any) => {
      this.img = S3_URL + replaceAll(result.key, ' ', '+');
      // GraphQL API mutation service.
      this.bannerService.createBanner(
        this.img,
        this.form.value.channel,
        this.form.value.trailer,
        this.backgroundColor,
        this.buttonColor,
        this.textColor,
      );
      // Re-enable the form.
      this.form.enable({
        onlySelf: true,
        emitEvent: true,
      });
      // Clean all form fields.
      this.formElement.nativeElement.reset();
    }).catch((err) => {
      console.log('error =>', err);
    });
  }

知道为什么 S3 会忽略我指示的公共访问权限并将文件设为私有吗?提前致谢!

标签: angularamazon-web-servicesamazon-s3aws-amplifyaws-amplify-cli

解决方案


检查您的存储桶属性设置;无论您的许可是什么,它都可能被配置为阻止公共访问。此外,请检查您的帐户设置是否阻止对 S3 的公共访问 https://aws.amazon.com/s3/features/block-public-access/


推荐阅读