如何从亚马逊s3存储桶中删除文件?
我需要用 python 编写代码,从 Amazon s3 存储桶中删除所需的文件。我可以连接到 Amazon s3 存储桶,也可以保存文件,但如何删除文件?
I need to write code in python that will delete the required file from an Amazon s3 bucket. I am able to connect to the Amazon s3 bucket, and also to save files, but how can I delete a file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(22)
使用
boto3
(当前版本 1.4.4)使用S3.Object.delete()
。Using
boto3
(currently version 1.4.4) useS3.Object.delete()
.使用 Python boto3 SDK(以及假设为 AWS 设置了凭证),以下命令将删除存储桶中的指定对象:
Using the Python boto3 SDK (and assuming credentials are setup for AWS), the following will delete a specified object in a bucket:
使用 boto 找到了另一种方法:
found one more way to do it using the boto:
欢迎来到 2020 年,这是 Python/Django 的答案:
花了我太长时间才找到答案,就这么简单。
Welcome to 2020 here is the answer in Python/Django:
Took me far too long to find the answer and it was as simple as this.
请尝试这个代码
please try this code
尝试查找 更新方法,因为 Boto3 可能会不时发生变化。我使用了my_bucket.delete_objects():
Try to look for an updated method, since Boto3 might change from time to time. I used my_bucket.delete_objects():
我很惊讶没有这么简单的方法:
key.delete()
:I'm surprised there isn't this easy way :
key.delete()
:以下是可用于删除存储桶的代码片段,
Below is code snippet you can use to delete the bucket,
使用
S3FileSystem.rm
s3fs
中的 函数。您可以一次删除单个文件或多个文件:
Use the
S3FileSystem.rm
function ins3fs
.You can delete a single file or several at once:
如果您想通过几行代码以最简单的方式从 s3 存储桶中删除所有文件,请使用此命令。
if you want to delete all files from s3 bucket in simplest way with couple of lines of code use this.
通过哪个接口?使用 REST 界面,您只需发送删除:
通过 SOAP 接口:
如果您使用的是 Python 库像 boto 一样,它应该公开“删除”功能,例如
delete_key()
。Via which interface? Using the REST interface, you just send a delete:
Via the SOAP interface:
If you're using a Python library like boto, it should expose a "delete" feature, like
delete_key()
.2021 更新-我在这方面遇到了困难,但它就像做一样简单。
确保在 boto3 资源中添加凭据
2021 update- I had a hard time on this but it was as simple as doing.
make sure to add the credentials in your boto3 resource
目前,我已经使用 Linux 实用程序 s3cmd 解决了该问题。我在Python中这样使用它:
For now I have resolved the issue by using the Linux utility s3cmd. I used it like this in Python:
最简单的方法是:
Simplest way to do this is:
对我有用,试试吧。
It's worked for me try it.
如果您尝试使用自己的本地主机控制台删除文件,那么您可以尝试运行此 python 脚本,假设您已经在系统中分配了访问 ID 和密钥
if you are trying to delete file using your own local host console then you can try running this python script assuming that you have have already assigned your access id and secret key in the system
我是这样做的
Here is how i did this
cloudpathlib
将boto3
包装在pathlib
接口,因此可以轻松执行诸如这与使用本地文件一样。首先,请确保使用
~/.aws/credentials
文件或环境变量集进行正确身份验证。请cloudpathlib 文档查看更多选项。然后,像在 pathlib 中一样使用
unlink
方法。对于目录,cloudpathlib 还提供了 rmtree 方法。cloudpathlib
wrapsboto3
in apathlib
interface so it becomes as easy to do tasks like this as working with local files.First, be sure to be authenticated properly with an
~/.aws/credentials
file or environment variables set. See more options in the cloudpathlib docs.Then, using the
unlink
method like you would in pathlib. For directories, cloudpathlib also provides anrmtree
method.从 S3 中的文件夹中删除文件
Delete files from folder in S3
使用凭证的 S3 连接
删除现有密钥
S3 connection with credential
Delete existing key
您可以使用 aws cli 来完成此操作: https://aws.amazon.com/cli/ 和一些unix命令。
这个 aws cli 命令应该可以工作:
如果您想包含子文件夹,您应该添加标志 --recursive
或使用 unix 命令:
解释:
you can do it using aws cli : https://aws.amazon.com/cli/ and some unix command.
this aws cli commands should work:
if you want to include sub-folders you should add the flag --recursive
or with unix commands:
explanation:
以下内容对我有用(基于 Django 模型的示例,但您几乎可以单独使用
delete
方法的代码)。The following worked for me (based on example for a Django model, but you can pretty much use the code of the
delete
method on its own).