这里使用新版的SDK2进行开发:
pom依赖:
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.17.64</version>
</dependency>
官方SDK文档:https://docs.aws.amazon.com/zh_cn/sdk-for-java/latest/developer-guide/home.html
S3Config封装
public class S3Config {
private String accessKeyId;
private String secretAccessKey;
private String region;
private String defaultBucket;
public S3Config() {
}
public S3Config(String accessKeyId, String secretAccessKey, String region, String defaultBucket) {
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.region = region;
this.defaultBucket = defaultBucket;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getSecretAccessKey() {
return secretAccessKey;
}
public void setSecretAccessKey(String secretAccessKey) {
this.secretAccessKey = secretAccessKey;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getDefaultBucket() {
return defaultBucket;
}
public void setDefaultBucket(String defaultBucket) {
this.defaultBucket = defaultBucket;
}
}
Util封装:
/**
* AWS S3(simple storage service)工具类
*
* @author mori
* @date 2021/10/21
*/
public class AmazonS3Util {
private static final Logger log = LoggerFactory.getLogger(AmazonS3Util.class);
private static S3Client s3Client;
private static S3Config s3Config;
/**
* 初始化s3 client
*
* @param config config
*/
private static void init(S3Config config) {
try {
log.info("AWS S3 S3Client init...");
s3Config = config;
AwsBasicCredentials awsBasicCredentials = AwsBasicCredentials.create(
config.getAccessKeyId(), config.getSecretAccessKey());
s3Client = S3Client.builder()
.region(Region.of(config.getRegion()))
.credentialsProvider(StaticCredentialsProvider.create(awsBasicCredentials))
.build();
List<Bucket> buckets = listBucket();
String defaultBucket = s3Config.getDefaultBucket();
if (CollectionUtils.isNotEmpty(buckets)) {
Bucket bucket = buckets.stream()
.filter(b -> Objects.equals(defaultBucket, b.name())).findFirst().orElse(null);
if (Objects.nonNull(bucket)) {
log.info("default bucket '{}' already exists.", defaultBucket);
return;
}
}
log.info("default bucket '{}' does not exist, start to create...", defaultBucket);
boolean res = createBucket(defaultBucket);
if (!res) {
throw new RuntimeException("create default bucket failed.");
}
} catch (Exception e) {
log.error("AWS S3Client init failed, error message: {}", e.getMessage());
throw new RuntimeException("AWS S3Client init failed,", e);
}
}
/**
* 构建存储对象请求
*
* @param bucket 对象桶
* @param key 对象key
* @return {@link PutObjectRequest}
*/
private static PutObjectRequest createPutObjectRequest(String bucket, String key) {
return PutObjectRequest.builder()
.bucket(bucket)
.key(key)
.build();
}
/**
* 存储对象到默认设置的桶中
*
* @param key 对象key
* @param requestBody 需要存储的对象
* @return boolean
*/
public static boolean putObject(String key, RequestBody requestBody) {
return putObject(s3Config.getDefaultBucket(), key, requestBody);
}
/**
* 存储对象到默认设置的桶中
*
* @param key 对象key
* @param path 路径
* @return boolean
*/
public static boolean putObject(String key, Path path) {
return putObject(s3Config.getDefaultBucket(), key, path);
}
/**
* 获取对象输入流, 默认桶
*
* @param key 对象key
* @return {@link GetObjectResponse}
*/
public static ResponseInputStream<GetObjectResponse> getObject(String key) {
return getObject(s3Config.getDefaultBucket(), key);
}
/**
* 获取对象字节数组, 默认桶
*
* @param key 对象key
* @return {@link GetObjectResponse}
*/
public static ResponseBytes<GetObjectResponse> getObjectAsBytes(String key) {
return getObjectAsBytes(s3Config.getDefaultBucket(), key);
}
/**
* 删除对象, 默认桶
*
* @param key 对象key
* @return boolean
*/
public static boolean deleteObject(String key) {
return deleteObject(s3Config.getDefaultBucket(), key);
}
/**
* 删除桶中的对象列表,默认桶
*
* @param objectNames 对象名称列表
* @return true表示删除成功
*/
public static boolean deleteObjects(List<String> objectNames) {
return deleteObjects(s3Config.getDefaultBucket(), objectNames);
}
/**
* 当前桶对象列表
*
* @return 对象列表 {@link S3Object}>
*/
public static List<S3Object> listCurrentBucketObjects() {
return listBucketObjects(s3Config.getDefaultBucket());
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
* @return true表示创建成功
*/
public static boolean createBucket(String bucketName) {
try {
log.info("AWS S3 create bucket..., bucketName: '{}'", bucketName);
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket(bucketName)
.build();
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(res -> log.info(res.toString()));
log.info("AWS S3 bucket {} is ready", bucketName);
return true;
} catch (S3Exception e) {
log.error("AWS S3 create bucket failed, bucketName: {}, error detail message: {}",
bucketName, e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 列出所有存储桶
*
* @return 存储桶列表,通过name函数可以获得桶名称
*/
public static List<Bucket> listBucket() {
try {
ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build();
ListBucketsResponse listBucketsResponse = s3Client.listBuckets(listBucketsRequest);
return listBucketsResponse.buckets();
} catch (S3Exception e) {
log.error("AWS S3 list bucket failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return null;
}
}
/**
* 删除存储桶
*
* @param bucketName 存储桶名称
* @return true表示删除成功
*/
public static boolean deleteBucket(String bucketName) {
try {
log.info("AWS S3 delete bucket..., bucketName: '{}'", bucketName);
DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucketName).build();
s3Client.deleteBucket(deleteBucketRequest);
log.info("AWS S3 delete bucket complete, bucketName: '{}'", bucketName);
return true;
} catch (S3Exception e) {
log.error("AWS S3 deleted bucket failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 删除桶中的对象列表
*
* @param bucketName 存储桶名称
* @param objectNames 对象名称列表
* @return true表示删除成功
*/
public static boolean deleteObjects(String bucketName, List<String> objectNames) {
try {
String objects = Arrays.toString(objectNames.toArray());
log.info("AWS S3 delete multiple objects..., bucketName: '{}', keys: '{}'",
bucketName, objects);
ArrayList<ObjectIdentifier> keys = new ArrayList<>();
for (String objectName : objectNames) {
keys.add(ObjectIdentifier.builder().key(objectName).build());
}
DeleteObjectsRequest multiObjectDeleteRequest = DeleteObjectsRequest.builder()
.bucket(bucketName)
.delete(Delete.builder().objects(keys).build())
.build();
s3Client.deleteObjects(multiObjectDeleteRequest);
log.info("AWS S3 delete multiple objects complete, bucketName: '{}', keys: '{}'", bucketName, objects);
return true;
} catch (S3Exception e) {
log.error("AWS S3 deleted multiple objects failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 存储对象
*
* @param bucketName bucket名称
* @param key 对象key
* @param requestBody 需要存储的对象
* @return boolean
*/
public static boolean putObject(String bucketName, String key, RequestBody requestBody) {
try {
log.info("AWS S3 uploading object..., bucketName: '{}', key: '{}'", bucketName, key);
PutObjectRequest putObjectRequest = createPutObjectRequest(bucketName, key);
s3Client.putObject(putObjectRequest, requestBody);
log.info("AWS S3 upload object complete, bucketName: '{}', key: '{}'", bucketName, key);
return true;
} catch (S3Exception e) {
log.error("AWS S3 put object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 存储对象
*
* @param bucketName bucket名称
* @param key 对象key
* @param path 路径
* @return boolean
*/
public static boolean putObject(String bucketName, String key, Path path) {
try {
log.info("AWS S3 uploading object..., bucketName: '{}', key: '{}'", bucketName, key);
PutObjectRequest putObjectRequest = createPutObjectRequest(bucketName, key);
s3Client.putObject(putObjectRequest, path);
log.info("AWS S3 upload object complete, bucketName: '{}', key: '{}'", bucketName, key);
return true;
} catch (S3Exception e) {
log.error("AWS S3 put object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 获取对象输入流
*
* @param bucketName bucket名称
* @param key 对象key
* @return {@link GetObjectResponse}
*/
public static ResponseInputStream<GetObjectResponse> getObject(String bucketName, String key) {
try {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
return s3Client.getObject(getObjectRequest);
} catch (S3Exception e) {
log.error("AWS S3 get object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return null;
}
}
/**
* 获取对象字节数组
*
* @param bucketName bucket名称
* @param key 对象key
* @return {@link GetObjectResponse}
*/
public static ResponseBytes<GetObjectResponse> getObjectAsBytes(String bucketName, String key) {
try {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
return s3Client.getObjectAsBytes(getObjectRequest);
} catch (S3Exception e) {
log.error("AWS S3 get object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return null;
}
}
/**
* 删除对象
*
* @param bucketName bucket名称
* @param key 对象key
* @return boolean
*/
public static boolean deleteObject(String bucketName, String key) {
try {
log.info("AWS S3 delete object..., bucketName: '{}', key: '{}'", bucketName, key);
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
s3Client.deleteObject(deleteObjectRequest);
log.info("AWS S3 delete object complete, bucketName: '{}', key: '{}'", bucketName, key);
return true;
} catch (S3Exception e) {
log.error("AWS S3 delete object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return false;
}
}
/**
* 复制桶中的对象到另一个桶
*
* @param fromBucket 对象存放的原始桶,一个 URL 编码的字符串值(请参阅 URlenCoder.code 方法)
* @param objectKey 对象的key
* @param toBucket 另一个桶
* @return 复制结果
*/
public static String copyBucketObject(String fromBucket, String objectKey, String toBucket) {
try {
log.info("AWS S3 copy object..., fromBucket: '{}', objectKey: '{}', toBucket: '{}'",
fromBucket, objectKey, toBucket);
CopyObjectRequest copyObjectRequest = CopyObjectRequest.builder()
.sourceBucket(fromBucket)
.sourceKey(objectKey)
.destinationBucket(toBucket)
.destinationKey(objectKey)
.build();
CopyObjectResponse res = s3Client.copyObject(copyObjectRequest);
log.info("AWS S3 copy object complete, fromBucket: '{}', objectKey: '{}', toBucket: '{}'",
fromBucket, objectKey, toBucket);
return res.copyObjectResult().toString();
} catch (S3Exception e) {
log.error("AWS S3 copy bucket object failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return "";
}
}
/**
* 列出桶中的对象
*
* @param bucketName 桶名称
* @return {@link List}<{@link S3Object}>
*/
public static List<S3Object> listBucketObjects(String bucketName) {
try {
ListObjectsRequest listObjectsRequest = ListObjectsRequest
.builder()
.bucket(bucketName)
.build();
ListObjectsResponse res = s3Client.listObjects(listObjectsRequest);
return res.contents();
} catch (S3Exception e) {
log.error("AWS S3 list bucket objects failed, error detail message: {}", e.awsErrorDetails().errorMessage());
return null;
}
}
}
configuration:
@Configuration
public class AmazonS3Config {
@Value("${amazon.s3.accessKeyId}")
private String accessKeyId;
@Value("${amazon.s3.secretAccessKey}")
private String secretAccessKey;
@Value("${amazon.s3.region}")
private String region;
@Value("${amazon.s3.defaultBucket}")
private String defaultBucket;
@Bean
public S3Config s3Config() {
return new S3Config(accessKeyId, secretAccessKey, region, defaultBucket);
}
@Bean
public MethodInvokingFactoryBean s3ClientInit(S3Config config) {
MethodInvokingFactoryBean invokingFactoryBean = new MethodInvokingFactoryBean();
invokingFactoryBean.setTargetClass(AmazonS3Util.class);
invokingFactoryBean.setTargetMethod("init");
invokingFactoryBean.setArguments(config);
return invokingFactoryBean;
}
}