简体   繁体   English

无法访问 S3 存储桶的子文件夹 – Python,Boto3

[英]Cannot Access Subfolder of S3 bucket – Python, Boto3

I have been given access to a subfolder of an S3 bucket, and want to access all files inside using Python and boto3.我已获得访问 S3 存储桶的子文件夹的权限,并希望使用 Python 和 boto3 访问其中的所有文件。 I am new to S3 and have read the docs to death, but haven't been able to figure out how to successfully access just one subfolder.我是 S3 的新手,并且已经阅读了文档,但无法弄清楚如何仅成功访问一个子文件夹。 I understand that s3 does not use unix-like directory structure, but I don't have access to the root bucket.我知道 s3 不使用类似 unix 的目录结构,但我无权访问根存储桶。

How can I configure boto3 to just connect to this subfolder?如何将 boto3 配置为仅连接到此子文件夹?

I have successfully used this AWS CLI command to download the entire subfolder to my machine:我已成功使用此 AWS CLI 命令将整个子文件夹下载到我的机器上:

aws s3 cp --recursive s3://s3-bucket-name/SUB_FOLDER/ /Local/Path/Where/Files/Download/To --profile my-profile

This code:这段代码:

AWS_BUCKET='s3-bucket-name'
s3 = boto3.client("s3", region_name='us-east-1', aws_access_key_id=AWS_KEY_ID, aws_secret_access_key=AWS_SECRET)
response = s3.list_objects(Bucket=AWS_BUCKET)

Returns this error:返回此错误:

botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

I have also tried specifying the 'prefix' option in the call to list_objects, but this produces the same error.我还尝试在调用 list_objects 时指定“前缀”选项,但这会产生相同的错误。

You want to aws configure and save have your credentials and region then using boto3 is simple and easy.您想要aws configure并保存您的凭据和区域,然后使用 boto3 简单易行。

Use boto3.resource and get the client like this:使用 boto3.resource 并像这样获取客户端:

s3_resource = boto3.resource('s3')
s3_client   = s3_resource.meta.client
s3_client.list_objects(Bucket=AWS_BUCKET)

You should be good to go.你应该对 go 好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM