如何在上传过程中为亚马逊S3存储桶中的某些特定文件提供公共访问权限?

时间:2022-09-25 18:48:20

Currently I am using a S3 bucket to upload some pdfs.

目前我正在使用S3存储桶上传一些pdf。

Now none of those pdfs are directly accessible via url. But I want to make some specific pdfs public during upload.

现在这些pdf都不能通过url直接访问。但是我希望在上传期间公开一些特定的pdf。

I have gone this blog which explains how to make all the resources in a bucket public. But I do not want this, I want only few specific files to be public during upload.

我已经离开了这个博客,它解释了如何公开所有资源。但我不希望这样,我希望在上传过程中只公开几个特定文件。

Currently I am using ruby aws-sdk. I want to know is this possible?

目前我正在使用ruby aws-sdk。我想知道这有可能吗?

1 个解决方案

#1


1  

Since you didn't paste any specific code snippet you use, I assume you atr trying to achieve this using official AWS Ruby SDK

由于您没有粘贴您使用的任何特定代码段,我假设您尝试使用官方AWS Ruby SDK实现此目的

File.open('/source/file/path', 'rb') do |file|
  s3.put_object(bucket: 'bucket-name', key: 'object-key', body: file)
end

By default, any uploaded file is private, therefore not accessible by URL.

默认情况下,任何上传的文件都是私有的,因此URL无法访问。

If you want to make it public, additional param needs to be set: acl: "public-read". By default, all AWS SDKs are using ACL private. So modifying my last example, it will look like:

如果要将其公开,则需要设置其他参数:acl:“public-read”。默认情况下,所有AWS开发工具包都使用ACL私有。所以修改我的上一个例子,它看起来像:

File.open('/source/file/path', 'rb') do |file|
  s3.put_object(acl: "public-read", bucket: 'bucket-name', key: 'object-key', body: file)
end

Now you should be able to access your file by URL.

现在,您应该能够通过URL访问您的文件。

You can read more about canned ACLs on S3 here: http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl

您可以在此处阅读有关S3上的固定ACL的更多信息:http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl

#1


1  

Since you didn't paste any specific code snippet you use, I assume you atr trying to achieve this using official AWS Ruby SDK

由于您没有粘贴您使用的任何特定代码段,我假设您尝试使用官方AWS Ruby SDK实现此目的

File.open('/source/file/path', 'rb') do |file|
  s3.put_object(bucket: 'bucket-name', key: 'object-key', body: file)
end

By default, any uploaded file is private, therefore not accessible by URL.

默认情况下,任何上传的文件都是私有的,因此URL无法访问。

If you want to make it public, additional param needs to be set: acl: "public-read". By default, all AWS SDKs are using ACL private. So modifying my last example, it will look like:

如果要将其公开,则需要设置其他参数:acl:“public-read”。默认情况下,所有AWS开发工具包都使用ACL私有。所以修改我的上一个例子,它看起来像:

File.open('/source/file/path', 'rb') do |file|
  s3.put_object(acl: "public-read", bucket: 'bucket-name', key: 'object-key', body: file)
end

Now you should be able to access your file by URL.

现在,您应该能够通过URL访问您的文件。

You can read more about canned ACLs on S3 here: http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl

您可以在此处阅读有关S3上的固定ACL的更多信息:http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl