删除文件夹及其内容 AWS S3 java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42442259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete a folder and its content AWS S3 java
提问by Munish Dhiman
Is it possible to delete a folder(In S3 bucket) and all its content with a single api request using java sdk for aws. For browser console we can delete and folder and its content with a single click and I hope that same behavior should be available using the APIs also.
是否可以使用 java sdk for aws 通过单个 api 请求删除文件夹(在 S3 存储桶中)及其所有内容。对于浏览器控制台,我们可以通过单击删除文件夹及其内容,我希望使用 API 也应该可以使用相同的行为。
回答by Opster Elasticsearch Ninja
There is no such thing as folders in S3; There are simply files with slashes in the filenames.
S3中没有文件夹之类的东西;文件名中只有带斜杠的文件。
Browser console will visualize these slashes as folders, but they're not real.
浏览器控制台会将这些斜杠可视化为文件夹,但它们不是真实的。
You can delete all files with the same prefix, but first you need to look them up with list_objects(), then you can batch delete them.
您可以删除所有具有相同前缀的文件,但首先需要使用list_objects() 查找它们,然后才能批量删除它们。
For code snippet using Java sdk please refer below doc
有关使用 Java sdk 的代码片段,请参阅以下文档
http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html
http://docs.aws.amazon.com/AmazonS3/latest/dev/DeletingMultipleObjectsUsingJava.html
回答by Nikolas
You can specify keyPrefix in ListObjectsRequest.
您可以在 ListObjectsRequest 中指定 keyPrefix。
For example, consider a bucket that contains the following keys:
例如,考虑一个包含以下键的存储桶:
- foo/bar/baz
- foo/bar/bash
- foo/bar/bang
- foo/boo
- 富/酒吧/巴兹
- 富/酒吧/bash
- 富/酒吧/砰
- 富/嘘
And you want to delete files from foo/bar/baz.
并且您想从foo/bar/baz 中删除文件。
if (s3Client.doesBucketExist(bucketName)) {
ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix("foo/bar/baz");
ObjectListing objectListing = s3Client.listObjects(listObjectsRequest);
while (true) {
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
s3Client.deleteObject(bucketName, objectSummary.getKey());
}
if (objectListing.isTruncated()) {
objectListing = s3Client.listNextBatchOfObjects(objectListing);
} else {
break;
}
}
}
回答by KESHAV KUMAR
There is no option of giving a folder name or more specifically prefix in java sdk to delete files. But there is an option of giving array of keys you want to delete. Click for details. By using this, I have written a small method to delete all files corresponding to a prefix.
在 java sdk 中没有提供文件夹名称或更具体的前缀来删除文件的选项。但是有一个选项可以提供要删除的键数组。 点击了解详情。通过使用它,我编写了一个小方法来删除与前缀对应的所有文件。
private AmazonS3 s3client = <Your s3 client>;
private String bucketName = <your bucket name, can be signed or unsigned>;
public void deleteDirectory(String prefix) {
ObjectListing objectList = this.s3client.listObjects( this.bucketName, prefix );
List<S3ObjectSummary> objectSummeryList = objectList.getObjectSummaries();
String[] keysList = new String[ objectSummeryList.size() ];
int count = 0;
for( S3ObjectSummary summery : objectSummeryList ) {
keysList[count++] = summery.getKey();
}
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest( bucketName ).withKeys( keysList );
this.s3client.deleteObjects(deleteObjectsRequest);
}
回答by Vaibhav Rai
You can try this
你可以试试这个
void deleteS3Folder(String bucketName, String folderPath) {
for (S3ObjectSummary file : s3.listObjects(bucketName, folderPath).getObjectSummaries()){
s3.deleteObject(bucketName, file.getKey());
}
}