0

我有一个使用 nodeJs 的网站。在这里,用户可以上传他们的照片。我正在使用芥末来存储这些图片。我的问题是每次用户将图片发送到服务器时,它都会以私人状态保存。因为上传后用户看不到图片。我需要在存储桶上公开它,以便用户可以看到它。我的所有存储桶都可以免费阅读,但每次我上传文件时它都是私有的。如何让每张上传的图片都公开?

这些是我的上传参数

const paramsForUpload = {
      Bucket: bucketName,
      Key: filePath,
      Body: file.data,
    };
    const options = {
      partSize: 10 * 1024 * 1024, // 10 MB
      queueSize: 10,
    };
s3.upload(paramsForUpload,options, (err) =>{
....
})

我的政策

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicRead",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::bucketname"
    },
    {
      "Sid": "AddPerm",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::bucketname"
    }
  ]
}

感谢您的帮助

4

2 回答 2

1

存储桶策略应引用存储桶的内容,并带有尾随/*

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowPublicRead",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::bucketname/*"
    }
  ]
}
于 2020-10-01T10:27:49.580 回答
0

您可以通过添加公开上传

      ACL: 'public-read',

到您的上传参数,例如

    const paramsForUpload = {
      Bucket: bucketName,
      Key: filePath,
      Body: file.data,
      ACL: 'public-read',
    };
于 2020-10-01T09:57:38.727 回答