1. 关于AWS S3的使用相关知识总结可以参考:AWS S3 学习小结。
2. 以下主要是想总结一下关于S3文件、文件夹如何删除,示例版本是源码里面的java2。
注:要知道,S3不存在子文件夹的概念,而是所有文件都在根目录。
假设有/home/files/如下的文件结构:
并且,有这样一个工具类。
package com.bas.util;
import org.apache.commons.lang3.StringUtils;
import software.amazon.awssdk.auth.credentials.*;
import software.amazon.awssdk.core.pagination.sync.SdkIterable;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
import software.amazon.awssdk.services.sts.model.Credentials;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;
public class S3Utils {
private S3Utils() {
}
public static S3Client builder(String regionStr) {
Region region = Region.US_EAST_1;//default region
if (StringUtils.isNotEmpty(regionStr)) {
region = Region.of(regionStr);
}
return S3Client.builder().region(region).build();
}
public static List<String> listKeysInDirectory(S3Client s3, String bucket, String prefix) {
String delimiter = "/";
if (!prefix.endsWith(delimiter)) {
prefix += delimiter;
}
// Build the list objects request
ListObjectsV2Request listReq = ListObjectsV2Request.builder()
.bucket(bucket)
.prefix(prefix)
.delimiter(delimiter)
.maxKeys(1)
.build();
ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
List<String> keyList = new ArrayList<>();
final String flolder = prefix;
listRes.contents().stream()
.forEach(content -> {
if (!flolder.equals(content.key())) {
keyList.add(content.key());
}
});
return keyList;
}
}
1) 当我们想要删除一个目录下的所有文件,也就是上图的两个txt文件。
public static void deleteS3Folder(S3Client s3, String bucketName, String folderPath) {
ArrayList<ObjectIdentifier> to_delete = new ArrayList<ObjectIdentifier>();
List<String> object_keys = listKeysInDirectory(s3, bucketName, folderPath);
if (null == object_keys || object_keys.size() == 0) {
return;
}
for (String k : object_keys) {
to_delete.add(ObjectIdentifier.builder().key(k).build());
}
try {
DeleteObjectsRequest dor = DeleteObjectsRequest.builder()
.bucket(bucketName)
.delete(Delete.builder().objects(to_delete).build())
.build();
DeleteObjectsResponse response=s3.deleteObjects(dor);
while (!response.sdkHttpResponse().isSuccessful()){
Thread.sleep(100);
}
} catch (S3Exception | InterruptedException e) {
}
System.out.println("delete folder successfully--->"+folderPath);
}
2) 当我们想要删除这个目录的所有文件,也就是图片上所有txt和所有子目录文件。
public static void deleteS3FolderRecursively(S3Client s3, String bucket, String prefix) {
ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
.bucket(bucket).build();
ListObjectsResponse objectsResponse = s3.listObjects(listObjectsRequest);
while (true) {
ArrayList<ObjectIdentifier> objects = new ArrayList<>();
for (Iterator<?> iterator = objectsResponse.contents().iterator(); iterator.hasNext(); ) {
S3Object s3Object = (S3Object)iterator.next();
objects.add(ObjectIdentifier.builder().key(s3Object.key()).build());
}
s3.deleteObjects(
DeleteObjectsRequest.builder().bucket(bucket).delete(Delete.builder().objects(objects).build()).build()
);
if (objectsResponse.isTruncated()) {
objectsResponse = s3.listObjects(listObjectsRequest);
continue;
}
break;
};
}
附:
1. 递归删除目录文件参考:How to delete files recursively from an S3 bucket
BAStriver 发布了65 篇原创文章 · 获赞 15 · 访问量 4万+ 私信 关注