首页 > 解决方案 > Python Boto3 - 如何删除“文件夹”并重命名另一个?

问题描述

我在 S3 中有这些路径:

s3://mykey/mytest/file1.txt
s3://mykey/mytest/file2.txt
s3://mykey/mytest/file3.txt

s3://mykey/mytest_temp/file4.txt
s3://mykey/mytest_temp/file5.txt
s3://mykey/mytest_temp/file6.txt

想要删除s3://mykey/mytest/(以及其中的所有文件)
然后重命名s3://mykey/mytest_temp/为,s3://mykey/mytest/同时将所有文件保留在其中(file4, file5, file6)。


最终结果应该是 - 只有 1 folder

s3://mykey/mytest/file4.txt
s3://mykey/mytest/file5.txt
s3://mykey/mytest/file6.txt

如何使用Python Boto3

谢谢。

标签: pythonamazon-web-servicesamazon-s3boto3

解决方案


要使用 boto3 纯粹从 Python 执行此操作,您需要执行以下操作:

删除现有的“文件夹”

请注意,这些 API 调用中的每一个都最多可处理 1000 个对象。如果您有超过 1000 个对象,则需要通过再次调用它们来对结果进行分页。

“重命名”对象

Amazon S3 没有“重命名”命令。相反,有必要将每个对象复制到一个新键,然后删除原始对象。


推荐阅读