我需要用python编写代码,该代码将从Amazon s3存储桶中删除所需的文件。我可以连接到Amazon s3存储桶,也可以保存文件,但是如何删除文件?
Answers:
使用boto3
(当前版本1.4.4)使用S3.Object.delete()
。
import boto3
s3 = boto3.resource('s3')
s3.Object('your-bucket', 'your-key').delete()
s3.Object('existing-bucket', 'bogus-key').delete()
然后看看会发生什么。也请尝试s3.Object('bogus-bucket', 'bogus-key').delete()
。)
your-key
这里是否表示your-bucket
S3上的实际文件名?
找到了另一种使用boto的方法:
from boto.s3.connection import S3Connection, Bucket, Key
conn = S3Connection(AWS_ACCESS_KEY, AWS_SECERET_KEY)
b = Bucket(conn, S3_BUCKET_NAME)
k = Key(b)
k.key = 'images/my-images/'+filename
b.delete_key(k)
for x in b.list(): b.delete_key(x.key)
bucket.list()
Bucket
和Key
。如:from boto.s3.connection import S3Connection, Bucket, Key
>>> from boto.s3.connection import S3Connection, Bucket, Key Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: No module named boto.s3.connection
请更新boto3的答案
使用Python boto3 SDK(并假设已为AWS设置凭据),以下操作将删除存储桶中的指定对象:
import boto3
client = boto3.client('s3')
client.delete_object(Bucket='mybucketname', Key='myfile.whatever')
尝试寻找一种更新的方法,因为Boto3可能会不时更改。我用了my_bucket.delete_objects():
import boto3
from boto3.session import Session
session = Session(aws_access_key_id='your_key_id',
aws_secret_access_key='your_secret_key')
# s3_client = session.client('s3')
s3_resource = session.resource('s3')
my_bucket = s3_resource.Bucket("your_bucket_name")
response = my_bucket.delete_objects(
Delete={
'Objects': [
{
'Key': "your_file_name_key" # the_name of_your_file
}
]
}
)
通过哪个接口?使用REST接口,您只需发送一个delete即可:
DELETE /ObjectName HTTP/1.1
Host: BucketName.s3.amazonaws.com
Date: date
Content-Length: length
Authorization: signatureValue
通过SOAP接口:
<DeleteObject xmlns="http://doc.s3.amazonaws.com/2006-03-01">
<Bucket>quotes</Bucket>
<Key>Nelson</Key>
<AWSAccessKeyId> 1D9FVRAYCP1VJEXAMPLE=</AWSAccessKeyId>
<Timestamp>2006-03-01T12:00:00.183Z</Timestamp>
<Signature>Iuyz3d3P0aTou39dzbqaEXAMPLE=</Signature>
</DeleteObject>
如果您使用的是像boto这样的Python库,它应该公开一个“删除”功能,例如delete_key()
。
DELETE
通过REST接口进行调用。因此,是的,尽管名称为“ delete_key”(我同意不清楚),但实际上是在删除键所引用的对象。
目前,我已经使用Linux实用程序s3cmd解决了该问题。我在Python中这样使用它:
delFile = 's3cmd -c /home/project/.s3cfg del s3://images/anon-images/small/' + filename
os.system(delFile)
尝试一下对我有用。
import boto
import sys
from boto.s3.key import Key
import boto.s3.connection
AWS_ACCESS_KEY_ID = '<access_key>'
AWS_SECRET_ACCESS_KEY = '<secret_access_key>'
Bucketname = 'bucket_name'
conn = boto.s3.connect_to_region('us-east-2',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
is_secure=True,
calling_format = boto.s3.connection.OrdinaryCallingFormat(),
)
bucket = conn.get_bucket(Bucketname)
k = Key(bucket)
k.key = 'filename to delete'
bucket.delete_key(k)
您可以使用aws cli来实现:https ://aws.amazon.com/cli/和一些Unix命令。
这个aws cli命令应该可以工作:
aws s3 rm s3://<your_bucket_name> --exclude "*" --include "<your_regex>"
如果要包括子文件夹,则应添加标志--recursive
或使用unix命令:
aws s3 ls s3://<your_bucket_name>/ | awk '{print $4}' | xargs -I% <your_os_shell> -c 'aws s3 rm s3:// <your_bucket_name> /% $1'
说明:
如果您尝试使用自己的本地主机控制台删除文件,则可以尝试运行此python脚本,前提是您已经在系统中分配了访问ID和秘密密钥
import boto3
#my custom sesssion
aws_m=boto3.session.Session(profile_name="your-profile-name-on-local-host")
client=aws_m.client('s3')
#list bucket objects before deleting
response = client.list_objects(
Bucket='your-bucket-name'
)
for x in response.get("Contents", None):
print(x.get("Key",None));
#delete bucket objects
response = client.delete_object(
Bucket='your-bucket-name',
Key='mydocs.txt'
)
#list bucket objects after deleting
response = client.list_objects(
Bucket='your-bucket-name'
)
for x in response.get("Contents", None):
print(x.get("Key",None));
以下代码对我有用(基于Django模型的示例,但是您几乎可以单独使用该delete
方法的代码)。
import boto3
from boto3.session import Session
from django.conf import settings
class Video(models.Model):
title=models.CharField(max_length=500)
description=models.TextField(default="")
creation_date=models.DateTimeField(default=timezone.now)
videofile=models.FileField(upload_to='videos/', null=True, verbose_name="")
tags = TaggableManager()
actions = ['delete']
def __str__(self):
return self.title + ": " + str(self.videofile)
def delete(self, *args, **kwargs):
session = Session (settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
s3_resource = session.resource('s3')
s3_bucket = s3_resource.Bucket(settings.AWS_STORAGE_BUCKET_NAME)
file_path = "media/" + str(self.videofile)
response = s3_bucket.delete_objects(
Delete={
'Objects': [
{
'Key': file_path
}
]
})
super(Video, self).delete(*args, **kwargs)
请尝试此代码
import boto3
s3 = boto3.client('s3')
s3.delete_object(Bucket="s3bucketname", Key="s3filepath")