我需要在python中编写代码,从Amazon s3存储桶中删除所需的文件。我可以连接到Amazon s3存储桶,也可以保存文件,但是如何删除文件?
答案 0 :(得分:77)
使用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)
答案 1 :(得分:44)
使用boto3
(当前版本1.4.4)使用S3.Object.delete()
。
import boto3
s3 = boto3.resource('s3')
s3.Object('your-bucket', 'your-key').delete()
答案 2 :(得分:33)
使用Python boto3 SDK(假设为AWS设置了凭据),以下内容将删除存储桶中的指定对象:
import boto3
client = boto3.client('s3')
client.delete_object(Bucket='mybucketname', Key='myfile.whatever')
答案 3 :(得分:15)
欢迎来到2020,这是Python / Django中的答案:
from django.conf import settings
import boto3
s3 = boto3.client('s3')
s3.delete_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=f"media/{item.file.name}")
花了我很长时间才找到答案,就这么简单。
答案 4 :(得分:3)
我很惊讶没有这么简单的方法:key.delete()
:
from boto.s3.connection import S3Connection, Bucket, Key
conn = S3Connection(AWS_ACCESS_KEY, AWS_SECERET_KEY)
bucket = Bucket(conn, S3_BUCKET_NAME)
k = Key(bucket = bucket, name=path_to_file)
k.delete()
答案 5 :(得分:2)
通过哪个界面?使用REST界面,您只需send a delete:
DELETE /ObjectName HTTP/1.1
Host: BucketName.s3.amazonaws.com
Date: date
Content-Length: length
Authorization: signatureValue
<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>
如果您使用的是Python库like boto,则应该公开“删除”功能,例如delete_key()
。
答案 6 :(得分:2)
最简单的方法是:
import boto3
s3 = boto3.resource("s3")
bucket_source = {
'Bucket': "my-bcuket",
'Key': "file_path_in_bucket"
}
s3.meta.client.delete(bucket_source)
答案 7 :(得分:1)
目前我已使用Linux实用程序s3cmd解决了该问题。我在Python中使用它:
delFile = 's3cmd -c /home/project/.s3cfg del s3://images/anon-images/small/' + filename
os.system(delFile)
答案 8 :(得分:1)
如果你想用几行代码以最简单的方式从 s3 存储桶中删除所有文件,请使用它。
import boto3
s3 = boto3.resource('s3', aws_access_key_id='XXX', aws_secret_access_key= 'XXX')
bucket = s3.Bucket('your_bucket_name')
bucket.objects.delete()
答案 9 :(得分:1)
在 S3FileSystem.rm
中使用 s3fs
函数。
您可以一次删除一个或多个文件:
import s3fs
file_system = s3fs.S3FileSystem()
file_system.rm('s3://my-bucket/foo.txt') # single file
files = ['s3://my-bucket/bar.txt', 's3://my-bucket/baz.txt']
file_system.rm(files) # several files
答案 10 :(得分:1)
请尝试以下代码
class DashBoardHostingController: UIHostingController<DashboardView> {
required init?(coder: NSCoder) {
super.init(coder: coder, rootView: DashboardView())
}
}
struct DashboardView: View {
var body: some View {
ScrollView {
VStack(alignment: .leading) {
HStack {
Text("Workouts")
.font(.title)
.fontWeight(.bold)
.padding(.leading)
Spacer()
Button(action: {
let storyBoard: UIStoryboard = UIStoryboard(name: "Version3", bundle: nil)
let subscribeViewController = storyBoard.instantiateViewController(withIdentifier: "skateListVC") as! SkateListTableViewController
UIApplication.topViewController()?.present(subscribeViewController, animated: true, completion: nil)
}) {
Text("Show More")
}
.padding(.trailing)
}
ZStack {
VStack(alignment: .leading) {
WorkoutListView(workouts: [MockWorkout().getMockWorkout()])
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding(20)
.background(Color.white)
.cornerRadius(10)
.padding()
}
}
}
答案 11 :(得分:1)
如果您尝试使用自己的本地主机控制台删除文件,则可以假设已在系统中分配了访问ID和密钥,则可以尝试运行此python脚本
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));
答案 12 :(得分:1)
您可以使用aws cli:https://aws.amazon.com/cli/和一些Unix命令来完成此操作。
此aws cli命令应该起作用:
aws s3 rm s3://<your_bucket_name> --exclude "*" --include "<your_regex>"
如果要包含子文件夹,则应添加标志-递归
或使用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'
说明:
答案 13 :(得分:1)
这对我来说很有用。
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)
答案 14 :(得分:0)
以下内容对我有用(基于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)
答案 15 :(得分:0)
下面是可用于删除存储桶的代码段
import boto3, botocore
from botocore.exceptions import ClientError
s3 = boto3.resource("s3",aws_access_key_id='Your-Access-Key',aws_secret_access_key='Your-Secret-Key')
s3.Object('Bucket-Name', 'file-name as key').delete()
答案 16 :(得分:0)
尝试查找last updated method。 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
}
]
}
)