通过Boto连接到S3中的“文件夹”

3
我正在尝试使用boto和Python将文件上传到特定位置。我正在使用类似于以下的方法进行访问:
from boto.s3.connection import S3Connection
from boto.s3.key import Key


conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket('the_bucket_name')
for key in bucket: 
    print key.name

这是一个技巧。我已经获得了访问存储桶中“文件夹”内的凭证。根据此链接 Amazon S3 boto - how to create a folder?,我了解到实际上在s3中并没有文件夹,而是像“foo/bar/my_key.txt”这样的键。当我尝试执行get_bucket()时,我会收到boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden的错误提示,因为我实际上没有基本存储桶的凭证,而是有一组存储桶密钥的子集:my_bucket/the_area_I_have_permission/*。有没有人知道如何在连接步骤中传递我可以访问的特定“区域”,或者我可以使用什么替代方法来访问my_bucket/the_area_I_have_permission/*
2个回答

2
这有帮助吗:
bucket = conn.get_bucket('the_bucket_name',validate=False)

key = bucket.get_key("ur_key_name")
if key is not None:
    print  key.get_contents_as_string()

keys = bucket.list(prefix='the_area_I_have_permission')
for key in keys:
    print key.name

谢谢回复。不对。第一个出现了TypeError: get_key() got an unexpected keyword argument 'prefix',第二个出现了boto.exception.S3ResponseError: S3ResponseError: 403 Forbidden。 - Brad
哦,混淆了两个不同的API调用。这个应该是正确的调用。关于403错误,您能否使用其他客户端(可能是UI或Firefox的S3Explorer插件)使用这些secretid/keys访问存储桶? - Archit Jain
我仍然在第一个请求中收到403错误。我正在尝试连接到“bucket-name”,然后将路径传递给我要访问的文件,例如:“/some/path/file.json”。这样正确吗?我已经尝试过带和不带前导斜杠的方式。 - Brad
就403而言,是的。我可以正常连接S3上传器,但如果我尝试浏览除了我被授权凭证的特定文件夹之外的任何地方,我会收到错误提示。 - Brad

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接