springboot中引入相关依赖
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>regions</artifactId>
<version>2.22.13</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>eksauth</artifactId>
<version>2.22.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3 -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.22.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.22.12</version>
<scope>compile</scope>
</dependency>
然后java代码,一些相关注意的事项都在代码里有标出。
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.util.List;
import java.util.ListIterator;
import java.time.Duration;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
import software.amazon.awssdk.services.s3.model.ObjectCannedACL;
import software.amazon.awssdk.services.s3.model.S3Exception;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;
/**
* @author 忆林pp
* @className: Ceph_upload_test
* @description: 测试对接ceph数据库
* @date 2024/1/10 10:44
* @version:1.0
*/
public class Ceph_upload_test {
public static void main(String[] args) throws Exception{
/**
* 使用客户端构建器创建 S3 客户端:
*/
//这里的这两个密钥要去自己的对象网关那里看,关于用户信息,如果没有创建一个即可
String accessKey="W3VQYUV72ZPFRWI0DIJL";
String secretKey="CQoJOXUHWZNM4qsv5G9JkrR8TIZFDYvkRdmm06N3";
AwsBasicCredentials credentials = AwsBasicCredentials.create(accessKey, secretKey);
S3Client client = S3Client.builder()
//你后端部署的对象网关的地址,这里我填的是内网地址,端口为对象网关默认的端口
.endpointOverride(new URI("http://192.168.78.140:7480"))
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.serviceConfiguration(srvcConf -> {
srvcConf.pathStyleAccessEnabled();
})
.region(Region.US_EAST_1) // this is not used, but the AWS SDK requires it
.build();
/**
* 列出拥有的存储桶
*/
ListBucketsResponse lbResponse = client.listBuckets();
for (Bucket bucket : lbResponse.buckets()) {
System.out.println(bucket.name() + "\t" + bucket.creationDate());
}
// 输出示范
// mahbuckat1 2021-09-20T14:12:57.231Z
// mahbuckat2 2021-09-20T14:12:59.402Z
// mahbuckat3 2021-09-20T14:13:02.288Z
/**
* 创建存储桶
*/
client.createBucket(req -> {
req.bucket("my-new-bucket");
});
/**
* 列出存储桶的内容
*/
ListObjectsResponse loResponse = client.listObjects(req -> {
req.bucket("s3cmd-demo");
});
for (S3Object object : loResponse.contents()) {
System.out.println(
object.key() + "\t" +
object.size() + "\t" +
object.lastModified()
);
}
// 输出示例
// myphoto1.jpg 251262 2021-09-20T17:47:07.317Z
// myphoto2.jpg 262518 2021-09-20T17:49:46.872Z
/**
* 删除存储桶
* 注意:桶必须是空的!否则就行不通了!
*/
client.deleteBucket(req -> {
req.bucket("my-new-bucket");
});
/**
* 创建对象
* 这将创建一个包含字符串的文件hello.txt"Hello World!"
* 大致为生产环境的文件上传!!!!!
*/
ByteBuffer input = ByteBuffer.wrap("Hello World!".getBytes());
client.putObject(
req -> {
req.bucket("s3cmd-demo").key("hello.txt");
},
RequestBody.fromByteBuffer(input)
);
/**
* 更改对象的 ACL
* 文件的限定权限
*/
client.putObjectAcl(req -> {
req.bucket("s3cmd-demo").key("hello.txt").acl(ObjectCannedACL.PUBLIC_READ);
});
client.putObjectAcl(req -> {
req.bucket("s3cmd-demo").key("secret_plans.txt").acl(ObjectCannedACL.PRIVATE);
});
/**
* 下载对象(到文件)
* 这个要指定到指定文件名进行覆盖。(这个文件应该是不存在的,它这里会自动创建,如果存在会报文件存在异常
* 这个地方的下载文件不建议用在生产环境的对象下载,因为这里的机制我是这样理解的:(仅供参考!!!)
* :这里首先你的ceph桶必须得存在,其次相对应得对象也必须得存在,
* 其次你的本地下载地址中应该没有重名文件,然后这段语句他执行得时候会将
* 你的ceph桶对象得数据直接创建一个空文件然后复写进你这个对应得空文件中!
* 因为这里要指定到对应的文件路径,所以我觉得这里很蠢,在真实使用中非常不灵活!!!(个人想法,不喜勿喷!!!)
*/
client.getObject(
req -> {
req.bucket("s3cmd-demo").key("6.txt");
},
Paths.get("E:\\download\\6.txt")
);
/**
* 删除对象
*/
client.deleteObject(req -> {
req.bucket("s3cmd-demo").key("goodbye.txt");
});
/**
* 生成对象下载 URL(已签名和未签名)
* 我的预想是这里将这个生成的对应的文件连接返回给客户端,然后这里在前端给一个重定向,将这个下载任务甩锅给
* 客户端的游览器,由客户端请求ceph资源,这样的话,后端就不用做下载管理了,下载任务直接对接ceph的集群,减轻后端负担!!!
*/
S3Presigner presigner = S3Presigner.builder()
//这里同样得需要对应得后端对象网关得地址,http或者https都可以
.endpointOverride(new URI("http://192.168.78.140:7480"))
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.region(Region.US_EAST_1) // this is not used, but the AWS SDK requires it
.build();
PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(preReq -> {
preReq.getObjectRequest(req -> {
// req.bucket("s3cmd-demo").key("secret_plans.txt");
req.bucket("s3cmd-demo").key("旺仔小乔 - 梨花香.mp3");
}).signatureDuration(
Duration.ofMinutes(20)
);
});
System.out.println(presignedRequest.url());
// 输出示例
// https://endpoint.com/my-bucket/secret_plans.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20210921T151408Z&X-Amz-SignedHeaders=host&X-Amz-Expires=1200&X-Amz-Credential=XXXXXXXXXXXX%2F20210921%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Signature=yyyyyyyyyyyyyyyyyyyyyy
}
}
相关资源参考: