问题描述
我正在尝试使用boto3删除s3存储桶中的特定对象.
I am trying to access a specific object in my s3 bucket using boto3 for deletion.
下面的代码来自boto3文档. https://boto3.readthedocs.io/en/Latest/guide/migrations3.html#accessing-a-bucket
the code below is from the boto3 documentation.https://boto3.readthedocs.io/en/latest/guide/migrations3.html#accessing-a-bucket
# Boto 3
for key in bucket.objects.all():
key.delete()
很棒,但是我宁愿有一个字典引用,然后遍历对象.在扩展方面并不是最大的.
great but i would much rather have a dictionary reference then iterating though objects. That isn't the greatest at scaling.
是否有一种使用其键来抓取对象的方法?
Is there a way to grab an object using its key?
我尝试了此操作,但没有成功.查看对象集合管理器.
I attempted this but it didnt work. Looking through the objects collection manager.
s3 = boto3.resource('s3')
bucket = s3.Bucket(AWS_UPLOAD_BUCKET)
key = bucket.objects.get(key = venueobjects3key)
推荐答案
使用以下代码,我认为它将为您提供帮助
use the below code I think it will help you
S3 = boto3.client(
's3',
region_name = 'us-west-2',
aws_access_key_id = AWS_ACCESS_KEY_ID,
aws_secret_access_key = AWS_SECRET_ACCESS_KEY
)
#Create a file object using the bucket and object key.
fileobj = S3.get_object(
Bucket=<Bucket_Name>,
Key=<Key_Name>
)
# open the file object and read it into the variable filedata.
fileData = fileobj['Body'].read()
print(fileData)
这篇关于使用boto3访问s3存储桶中的特定密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!