从S3存储桶中递归删除子文件夹中的文件。

huangapple go评论87阅读模式
英文:

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()

参考:

英文:

The code should:

  • Call ListObjectsV2() with Prefix: 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 to DeleteObjects()

See:

huangapple
  • 本文由 发表于 2022年11月1日 04:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/74268746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定