Amazon S3 的 java sdk简单使用
在开发过程中基本都需要使用到对象存储技术。
对于一些国内的项目来说,可以使用阿里云,七牛云,京东云等开发平台进行开发对接存储对象。
而对于海外的项目,可能现有的国内技术无法使用,这时候就遇到了amazon这个平台了。在此只整理了其对接以及对象存储和验签预览对象的功能。
以下使用的是AWS SDK for Java2.x的开发工具包
详细使用请看:https://github.com/aws/aws-sdk-java-v2/#using-the-sdk
一、项目依赖导入
maven依赖管理
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.100</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
选择需要的模块进行依赖
<dependencies>
<!--amazon s3-->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>kms</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3control</artifactId>
</dependency>
</dependencies>
整个SDK包(不建议使用此方式导入,有大量的不需要的模块):
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-sdk-java</artifactId>
<version>2.17.100</version>
</dependency>
二、配置添加
nacos:
aws:
s3:
accessKey: xxxxxxxxxx
secretKey: xxxxxxxxxx
region: us-east-2 (选择s3控制台上的资源区域)
bucket: xxx
配置类读取nacos的配置:
/**
* @author Survivor
* @create 2021/12/20 17:49
*/
@Configuration
public class AwsConfiguration {
@Value("${aws.s3.accessKey}")
private String accessKey;
@Value("${aws.s3.secretKey}")
private String secretKey;
@Value("${aws.s3.region}")
private String region;
@Value("${aws.s3.bucket}")
private String bucket;
@Bean
public void initAwConfiguration() {
AwsUtils.setAccessKey(accessKey);
AwsUtils.setSecretKey(secretKey);
AwsUtils.setRegion(region);
AwsUtils.setBucket(bucket);
}
}
获取配置:
/**
* @author Survivor
* @create 2021/12/20 17:48
*/
@Slf4j
public class AwsUtils {
private static String accessKey;
private static String secretKey;
private static String region;
// bucket
private static String bucket;
public static void setAccessKey(String accessKey) {
AwsUtils.accessKey = accessKey;
}
public static void setSecretKey(String secretKey) {
AwsUtils.secretKey = secretKey;
}
public static void setBucket(String bucket) {
AwsUtils.bucket = bucket;
}
public static void setRegion(String region) {
AwsUtils.region = region;
}
}
三、使用aws的s3 SDK
- 获取s3对象
/**
* 获取S3Client对象
*
* @return s3
*/
private static S3Client getAmazonS3() {
return S3Client.builder()
.credentialsProvider(getAwsCredentialsProviderChain())
.region(Region.of(region))
.build();
}
// 获取aws供应商凭据
private static AwsCredentialsProviderChain getAwsCredentialsProviderChain(){
return AwsCredentialsProviderChain
.builder()
.addCredentialsProvider(new AwsCredentialsProvider() {
@Override
public AwsCredentials resolveCredentials() {
return AwsBasicCredentials.create(accessKey, secretKey);
}
}).build();
}
- 对象存储
private static String putS3Object(S3Client s3, String bucketName, String objectKey, String objectPath) {
try {
String mimetype = Mimetypes.getInstance().getMimetype(new File(objectPath));
PutObjectRequest putOb = PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.contentType(mimetype)
.build();
PutObjectResponse response = s3.putObject(putOb, RequestBody.fromBytes(getObjectFile(objectPath)));
return response.eTag();
} catch (S3Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
private static byte[] getObjectFile(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
putS3Object()方法返回的字符串信息,就是上传后的存储key,如果你的存储的bucket不是私有的,可以直接拼接域名,直接访问bucket上的对象。但如果你的bucket是私有的,又想要访问其对象的话,就需要用到下面的验签获取:
对象获取
private static S3Presigner getS3Presigner() {
return S3Presigner.builder()
.region(Region.of(region))
.credentialsProvider(getAwsCredentialsProviderChain())
.build();
}
/**
* 预览 有效时间为1小时
*
* @param key
* @return
*/
private static String preview(String bucketName, String key) {
GetObjectRequest getObjectRequest =
GetObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();
GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofMinutes(60))
.getObjectRequest(getObjectRequest)
.build();
PresignedGetObjectRequest presignedGetObjectRequest =
getS3Presigner().presignGetObject(getObjectPresignRequest);
return presignedGetObjectRequest.url().toString();
}
得到的是一串加上了验签参数的url路径,得到这个路径后就可以直接访问了。文章来源:https://www.toymoban.com/news/detail-780728.html
这边只整理了小部分功能,其余的功能有需要的可以参考官方代码示例:https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/s3/src/main/java/com/example/s3文章来源地址https://www.toymoban.com/news/detail-780728.html
到了这里,关于Amazon S3 的 java sdk简单使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!