我正在尝试使用Python / Boto3下载AWS S3内容。
第三方正在上传数据,我需要下载它。 他们提供了这样的凭证:
MYUser
SOMEKEY
SOMEOTHERKEY
使用流行的Windows 10应用程序CyberDuck,将我的“用户名”添加到应用程序的路径设置third-party/MYUser/myfolder
我在这里给的什么都不是我的水桶。
my_bucket = s3.Bucket('third-party/MYUser')
ParamValidationError: Parameter validation failed:
Invalid bucket name 'third-party/MYUser': Bucket name must match the regex "^[a-zA-Z0-9.\-_]{1,255}$"
my_bucket = s3.Bucket('third-party')
ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied
my_bucket = s3.Bucket('MYStuff')
NoSuchBucket: An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist
从我读到的内容来看,third-party
是AWS S3存储桶名称,但找不到有关如何访问其他用户存储桶的子目录的说明。
我看到Bucket()
有一些 user 参数。我在其他地方读过有关角色和访问控制列表的信息。但是我找不到一个简单的例子。
如何在给定用户名的情况下访问AWS S3上其他用户的存储桶?
答案 0 :(得分:3)
Amazon S3实际上没有目录。相反,对象的Key
(文件名)包含对象的完整路径。
例如,考虑以下对象:
s3://my-bucket/invoices/foo.txt
my-bucket
invoices/foo.txt
因此,您可以通过以下方式访问对象:
import boto3
s3_resource = boto3.resource('s3')
object = s3.Object('my-bucket','invoices/foo.txt')
为使S3与希望拥有文件夹和目录的系统和人员兼容,它维护CommonPrefixes
的列表,该列表实际上与目录相同。它们是从斜杠(/
)之间的名称派生的。因此,CyberDuck可以使用户能够浏览目录。
但是,第三方可能只为您分配了足够的权限来访问您自己的目录,而没有分配给根目录。在这种情况下,您将需要直接导航到目录,而无需单击层次结构。
使用备用凭据集的一种好方法是将它们存储为单独的配置文件:
aws configure --profile third-party
然后将提示您输入凭据。
然后,您可以使用如下凭证:
aws s3 ls s3://third-party/MyUser --profile third-party
aws s3 cp s3://third-party/MyUser/folder/foo.txt .
最后的--profile
可让您选择要使用的凭据。
boto3等效项是:
session = boto3.Session(profile_name='third-party')
s3_resource = session.resource('s3')
object = s3.Object('THEIR-bucket','MYinvoices/foo.txt')