AWS S3 和无服务器 - 使用 PUT 预签名时拒绝访问 URL

AWS S3 & Serverless - Access Denied when using PUT presigned URL

我正在尝试生成预签名 URL 以将对象上传到 S3 存储桶,但每当我对预签名 URL 执行 PUT 请求时,我都会收到 'Access Denied' 错误。这是我用来生成 URL 的代码(在节点 lambda 函数内运行):

app.post('/upload', async (req, res) => {
    const filename = `${uuid()}.jpg`;

    const url = s3.getSignedUrl('putObject', {
        Bucket: BucketName,
        Key: filename,
        Expires: 3600,
        ContentType: 'image/jpeg',
        ACL: 'public-read',
    });

    res.status(200).json({ url, filename });
});

下面是我的 serverless.yml 文件的权限:

service: my-service

provider:
  name: aws
  runtime: nodejs12.x
  profile: my-profile
  region: eu-west-2
  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - 's3:PutObject'
        - 's3:PutObjectAcl'
      Resource:
        - !GetAtt galleryBucket.Arn

我将 PutObjectPutObjectAcl 权限应用于 galleryBucket 而不是其中的对象。

您必须将 PutObject 权限应用于存储桶内的对象,而不是存储桶本身

我更新了对此的权限并且 PUT 请求成功:

service: my-service

provider:
  name: aws
  runtime: nodejs12.x
  profile: my-profile
  region: eu-west-2
  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - 's3:PutObject'
        - 's3:PutObjectAcl'
      Resource:
        Fn::Join: ['', ['arn:aws:s3:::', !Ref galleryBucket, '/*']]

注意资源标识符末尾的/*