AutoClosable
This commit is contained in:
parent
a08f4adf2d
commit
48a3dfb313
@ -27,9 +27,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@ -69,8 +67,8 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
return null;
|
||||
}
|
||||
String fullPath = buildPath(path);
|
||||
OSS ossClient = getOssClient();
|
||||
try {
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(inputStream.available());
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getBucketName(), fullPath, inputStream);
|
||||
@ -84,8 +82,8 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public boolean deleteFile(String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
try {
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
ossClient.deleteObject(config.getBucketName(), buildPath(path));
|
||||
return true;
|
||||
} catch (ClientException e) {
|
||||
@ -100,33 +98,37 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public String getUrlForDownload(Date expireDate, String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
URL url = ossClient.generatePresignedUrl(config.getBucketName(), buildPath(path), expireDate, HttpMethod.GET);
|
||||
return url.toString();
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
URL url = ossClient.generatePresignedUrl(config.getBucketName(), buildPath(path), expireDate, HttpMethod.GET);
|
||||
return url.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForUpload(Date expireDate, String contentType, String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getBucketName(), buildPath(path), HttpMethod.PUT);
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
request.setContentType(contentType);
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getBucketName(), buildPath(path), HttpMethod.PUT);
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
request.setContentType(contentType);
|
||||
}
|
||||
request.setExpiration(expireDate);
|
||||
URL url = ossClient.generatePresignedUrl(request);
|
||||
return url.toString();
|
||||
}
|
||||
request.setExpiration(expireDate);
|
||||
URL url = ossClient.generatePresignedUrl(request);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageFileObject> listDir(String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request(config.getBucketName());
|
||||
listObjectsV2Request.setPrefix(buildPath(path) + "/");
|
||||
listObjectsV2Request.setMaxKeys(1000);
|
||||
boolean isTruncated = true;
|
||||
String continuationToken = null;
|
||||
List<OSSObjectSummary> objectList = new ArrayList<>();
|
||||
try {
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
while (isTruncated) {
|
||||
if (continuationToken != null) {
|
||||
listObjectsV2Request.setContinuationToken(continuationToken);
|
||||
@ -156,27 +158,31 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
@Override
|
||||
public boolean deleteDir(String... path) {
|
||||
List<StorageFileObject> objectList = listDir(buildPath(path));
|
||||
OSS ossClient = getOssClient();
|
||||
if (objectList.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
int idx = 0;
|
||||
int batchSize = 999;
|
||||
while (objectList.size() > idx) {
|
||||
if (objectList.size() - idx < batchSize) {
|
||||
batchSize = objectList.size() - idx;
|
||||
}
|
||||
List<StorageFileObject> subList = objectList.subList(idx, idx + batchSize);
|
||||
idx += batchSize;
|
||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
|
||||
request.setKeys(subList.stream().map(StorageFileObject::getFullPath).collect(Collectors.toList()));
|
||||
try {
|
||||
ossClient.deleteObjects(request);
|
||||
} catch (OSSException e) {
|
||||
return false;
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
int idx = 0;
|
||||
int batchSize = 999;
|
||||
while (objectList.size() > idx) {
|
||||
if (objectList.size() - idx < batchSize) {
|
||||
batchSize = objectList.size() - idx;
|
||||
}
|
||||
List<StorageFileObject> subList = objectList.subList(idx, idx + batchSize);
|
||||
idx += batchSize;
|
||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName());
|
||||
request.setKeys(subList.stream().map(StorageFileObject::getFullPath).collect(Collectors.toList()));
|
||||
try {
|
||||
ossClient.deleteObjects(request);
|
||||
} catch (OSSException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (OSSException e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private CannedAccessControlList convertAcl(StorageAcl acl) {
|
||||
@ -196,8 +202,8 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public boolean setAcl(StorageAcl acl, String... path) {
|
||||
OSS ossClient = getOssClient();
|
||||
try {
|
||||
try (OSSWrapper wrapper = getOssClient()) {
|
||||
OSS ossClient = wrapper.getOSSClient();
|
||||
ossClient.setObjectAcl(config.getBucketName(), buildPath(path), convertAcl(acl));
|
||||
return true;
|
||||
} catch (OSSException e) {
|
||||
@ -205,9 +211,9 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private OSS getOssClient() {
|
||||
private OSSWrapper getOssClient() {
|
||||
OSS ossClient = new OSSClientBuilder().build(config.getEndpoint(), config.getAccessKeyId(), config.getAccessKeySecret());
|
||||
return ossClient;
|
||||
return new OSSWrapper(ossClient);
|
||||
}
|
||||
|
||||
private String buildPath(String ...paths) {
|
||||
@ -221,4 +227,23 @@ final public class AliOssAdapter extends AStorageAdapter {
|
||||
private String getRelativePath(String path) {
|
||||
return StorageUtil.getRelativePath(path, config.getPrefix());
|
||||
}
|
||||
|
||||
public static class OSSWrapper implements AutoCloseable {
|
||||
private final OSS ossClient;
|
||||
|
||||
public OSSWrapper(OSS ossClient) {
|
||||
this.ossClient = ossClient;
|
||||
}
|
||||
|
||||
// 提供对原始对象的方法访问
|
||||
public OSS getOSSClient() {
|
||||
return ossClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// 在此处实现资源关闭逻辑(如调用 OSS 的关闭方法)
|
||||
ossClient.shutdown(); // 假设 OSS 提供 shutdown 方法关闭资源
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,9 +4,7 @@ import com.amazonaws.ClientConfiguration;
|
||||
import com.amazonaws.HttpMethod;
|
||||
import com.amazonaws.Protocol;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||
import com.amazonaws.services.s3.S3ClientOptions;
|
||||
import com.amazonaws.services.s3.model.*;
|
||||
import com.ycwl.basic.storage.entity.AwsOssStorageConfig;
|
||||
@ -22,7 +20,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -63,8 +60,8 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
return null;
|
||||
}
|
||||
String fullPath = buildPath(path);
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
try {
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
ObjectMetadata metadata = new ObjectMetadata();
|
||||
metadata.setContentLength(inputStream.available());
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(config.getBucketName(), fullPath, inputStream, metadata);
|
||||
@ -78,8 +75,8 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public boolean deleteFile(String... path) {
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
try {
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
s3Client.deleteObject(config.getBucketName(), buildPath(path));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
@ -94,27 +91,30 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public String getUrlForDownload(Date expireDate, String... path) {
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
URL url = s3Client.generatePresignedUrl(config.getBucketName(), buildPath(path), expireDate);
|
||||
return url.toString();
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
URL url = s3Client.generatePresignedUrl(config.getBucketName(), buildPath(path), expireDate);
|
||||
return url.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUrlForUpload(Date expireDate, String contentType, String... path) {
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getBucketName(), buildPath(path));
|
||||
request.setMethod(HttpMethod.PUT);
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
request.setContentType(contentType);
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(config.getBucketName(), buildPath(path));
|
||||
request.setMethod(HttpMethod.PUT);
|
||||
if (StringUtils.isNotBlank(contentType)) {
|
||||
request.setContentType(contentType);
|
||||
}
|
||||
request.setExpiration(expireDate);
|
||||
URL url = s3Client.generatePresignedUrl(request);
|
||||
return url.toString();
|
||||
}
|
||||
request.setExpiration(expireDate);
|
||||
URL url = s3Client.generatePresignedUrl(request);
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<StorageFileObject> listDir(String... path) {
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
|
||||
.withBucketName(config.getBucketName())
|
||||
.withPrefix(buildPath(path) + "/")
|
||||
@ -122,7 +122,8 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
boolean isTruncated = true;
|
||||
String continuationToken = null;
|
||||
List<S3ObjectSummary> objectList = new ArrayList<>();
|
||||
try {
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
while (isTruncated) {
|
||||
if (continuationToken != null) {
|
||||
listObjectsV2Request.setContinuationToken(continuationToken);
|
||||
@ -152,27 +153,31 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
@Override
|
||||
public boolean deleteDir(String... path) {
|
||||
List<StorageFileObject> objectList = listDir(buildPath(path));
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
if (objectList.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
int idx = 0;
|
||||
int batchSize = 999;
|
||||
while (objectList.size() > idx) {
|
||||
if (objectList.size() - idx < batchSize) {
|
||||
batchSize = objectList.size() - idx;
|
||||
}
|
||||
List<StorageFileObject> subList = objectList.subList(idx, idx + batchSize);
|
||||
idx += batchSize;
|
||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName())
|
||||
.withKeys(subList.stream().map(StorageFileObject::getFullPath).toArray(String[]::new));
|
||||
try {
|
||||
s3Client.deleteObjects(request);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
int idx = 0;
|
||||
int batchSize = 999;
|
||||
while (objectList.size() > idx) {
|
||||
if (objectList.size() - idx < batchSize) {
|
||||
batchSize = objectList.size() - idx;
|
||||
}
|
||||
List<StorageFileObject> subList = objectList.subList(idx, idx + batchSize);
|
||||
idx += batchSize;
|
||||
DeleteObjectsRequest request = new DeleteObjectsRequest(config.getBucketName())
|
||||
.withKeys(subList.stream().map(StorageFileObject::getFullPath).toArray(String[]::new));
|
||||
try {
|
||||
s3Client.deleteObjects(request);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private CannedAccessControlList convertAcl(StorageAcl acl) {
|
||||
@ -192,8 +197,8 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
|
||||
@Override
|
||||
public boolean setAcl(StorageAcl acl, String... path) {
|
||||
AmazonS3 s3Client = getS3Client();
|
||||
try {
|
||||
try (S3Wrapper wrapper = getS3Client()) {
|
||||
AmazonS3Client s3Client = wrapper.getS3Client();
|
||||
s3Client.setObjectAcl(config.getBucketName(), buildPath(path), convertAcl(acl));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
@ -201,7 +206,7 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private AmazonS3Client getS3Client() {
|
||||
private S3Wrapper getS3Client() {
|
||||
BasicAWSCredentials basicAwsCred = new BasicAWSCredentials(config.getAccessKeyId(), config.getAccessKeySecret());
|
||||
ClientConfiguration clientConfiguration = new ClientConfiguration();
|
||||
clientConfiguration.setProtocol(Protocol.HTTPS);
|
||||
@ -209,7 +214,7 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
S3ClientOptions options = S3ClientOptions.builder().setPathStyleAccess(true).setPayloadSigningEnabled(true).disableChunkedEncoding().build();
|
||||
s3.setS3ClientOptions(options);
|
||||
s3.setEndpoint(config.getEndpoint());
|
||||
return s3;
|
||||
return new S3Wrapper(s3);
|
||||
}
|
||||
|
||||
private String buildPath(String... paths) {
|
||||
@ -223,4 +228,22 @@ public class AwsOssAdapter extends AStorageAdapter {
|
||||
private String getRelativePath(String path) {
|
||||
return StorageUtil.getRelativePath(path, config.getPrefix());
|
||||
}
|
||||
|
||||
public static class S3Wrapper implements AutoCloseable {
|
||||
private final AmazonS3Client s3Client;
|
||||
|
||||
public S3Wrapper(AmazonS3Client s3Client) {
|
||||
this.s3Client = s3Client;
|
||||
}
|
||||
|
||||
// 提供对原始对象的方法访问
|
||||
public AmazonS3Client getS3Client() {
|
||||
return s3Client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
s3Client.shutdown();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user