英文:
Delete files in the subfolder(recursively) from an S3 bucket
问题
我在S3中有以下文件夹结构。
s3://test/my-folder/test1.txt
s3://test/my-folder/test2.txt
s3://test/test3.txt
我想要在golang中递归删除某个文件夹下的所有文件,比如上面的my-folder
(不想删除s3://test/test3.txt
),就像在s3cmd中执行以下命令的结果一样:
s3cmd --recursive s3://test/my-folder/
有没有办法实现这个功能?
英文:
I have the following folder structure in S3.
s3://test/my-folder/test1.txt
s3://test/my-folder/test2.txt
s3://test/test3.txt
I want to recursively remove all files under a certain folder like my-folder
above in golang(Don't want to delete s3://test/test3.txt
) like the result of this command in s3cmd :
s3cmd --recursive s3://test/my-folder/
Is there any way to do that?
答案1
得分: 3
代码应该:
- 使用
Prefix: aws.String("test/my-folder/")
调用ListObjectsV2()
来获取该“文件夹”中的对象列表 - 循环遍历结果,并对每个对象调用
DeleteObject()
,或者创建一个要删除的对象列表,并将它们传递给DeleteObjects()
参考:
- 使用 Go V2 SDK 的 Amazon S3 示例 - AWS SDK 代码示例
- s3 包 - github.com/aws/aws-sdk-go-v2/service/s3 - Go Packages
英文:
The code should:
- Call
ListObjectsV2()
withPrefix: aws.String("test/my-folder/")
to obtain a list of the objects in that 'folder' - Loop through the results and call
DeleteObject()
on each object, or create a list of the objects to delete and pass them toDeleteObjects()
See:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论