SpringBoot整合aws的方法是什么
发表于:2025-11-13 作者:千家信息网编辑
千家信息网最后更新 2025年11月13日,这篇文章主要讲解了"SpringBoot整合aws的方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBoot整合aws的方法是什么
千家信息网最后更新 2025年11月13日SpringBoot整合aws的方法是什么
这篇文章主要讲解了"SpringBoot整合aws的方法是什么",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"SpringBoot整合aws的方法是什么"吧!
引入依赖
software.amazon.awssdk s3 com.amazonaws aws-java-sdk-s3 com.amazonaws aws-java-sdk-sqs software.amazon.awssdk sns com.amazonaws aws-java-sdk-cloudfront
创建client
private S3Client createClient() { AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey)); S3Client s3 = S3Client.builder() .region(Region.CN_NORTHWEST_1) .credentialsProvider(credentialsProvider) .endpointOverride(URI.create(minIoUrl)) .build(); return s3; }aws工具类
public class MinioOperate { private String minIoAccessKey; private String minIoSecretKey; private String minIoUrl; public MinioOperate() { } public MinioOperate(String minIoAccessKey, String minIoSecretKey, String minIoUrl) { this.minIoAccessKey = minIoAccessKey; this.minIoSecretKey = minIoSecretKey; this.minIoUrl = minIoUrl; }#创建aws的客户端 private S3Client createClient() { AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey)); S3Client s3 = S3Client.builder() .region(Region.CN_NORTHWEST_1) .credentialsProvider(credentialsProvider) .endpointOverride(URI.create(minIoUrl)) .build(); return s3; }// public String generatePresignedUrl(String bucketName, String objectKey, String acl) {// URL url = null;// try {// AWSStaticCredentialsProvider credentialsProvider =// new AWSStaticCredentialsProvider(// new BasicAWSCredentials(minIoAccessKey, minIoSecretKey));//// AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();// builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(minIoUrl, Regions.CN_NORTHWEST_1.getName()));//// AmazonS3 s3Client = builder// .withPathStyleAccessEnabled(true)// .withCredentials(credentialsProvider)// .build();//// // Set the presigned URL to expire after one hour.// Date expiration = new Date();// long expTimeMillis = expiration.getTime();// expTimeMillis += 1000 * 60 * 60 * 4;// expiration.setTime(expTimeMillis);//// // Generate the presigned URL.// GeneratePresignedUrlRequest generatePresignedUrlRequest =// new GeneratePresignedUrlRequest(bucketName, objectKey)// .withMethod(HttpMethod.GET)// .withExpiration(expiration);//// // set acl// if (!StringUtils.isEmpty(acl)) {// generatePresignedUrlRequest.withMethod(HttpMethod.PUT);// generatePresignedUrlRequest.addRequestParameter(Headers.S3_CANNED_ACL, acl);// }//// url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);//// } catch (AmazonServiceException e) {// // The call was transmitted successfully, but Amazon S3 couldn't process// // it, so it returned an error response.// e.printStackTrace();// } catch (SdkClientException e) {// // Amazon S3 couldn't be contacted for a response, or the client// // couldn't parse the response from Amazon S3.// e.printStackTrace();// }//// if (StringUtils.isEmpty(url)) {// return null;// }// return url.toString();// } #获取所有的对象(根据桶和前缀) public List ListObjects(String bucket, String prefix) { S3Client s3Client = this.createClient(); List contents = null; try { ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix).build(); ListObjectsV2Response listObjectsV2Response = s3Client.listObjectsV2(request); contents = listObjectsV2Response.contents(); } finally { this.closeClient(s3Client); } return contents; } #上传对象 public void putObject(String bucket, String key, String content) { S3Client s3Client = this.createClient(); try { ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)); PutObjectResponse putObjectResponse = s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key) .build(), RequestBody.fromByteBuffer(byteBuffer)); } finally { this.closeClient(s3); } }#上传文件 public void putFile(String filePath, String key, String bucket) { S3Client s3Client = this.createClient(); File tempFile = new File(filePath); try { PutObjectResponse putObjectResponse = s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(key) .build(), RequestBody.fromFile(tempFile)); } catch (Exception e) { e.printStackTrace(); } finally { this.closeClient(s3Client); } }#获取对象大小 @Override public long getS3ObjectLength(String bucket, String key) { S3Client s3Client = this.createClient(); long size = 0; try { List s3Objects = this.ListObjects(s3, bucket, key); size = s3Objects.get(0).size(); }catch (Exception e){ e.printStackTrace(); }finally { this.closeClient(s3); } return size; } # 获取对象 public String getObject(String bucket, String key) { S3Client s3Client = this.createClient(); try { ResponseBytes responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toBytes()); # ResponseTransformer.toBytes() 将响应转换为二进制流 return this.decode(responseResponseBytes.asByteBuffer()); } finally { this.closeClient(s3); } }# 获取对象的流 @Override public InputStream getInputStream(String bucket, String key) { S3Client s3Client = this.createClient(); try { ResponseBytes responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toBytes()); return responseResponseBytes.asInputStream(); } finally { this.closeClient(s3); } }#删除对象 public void deleteObject(String bucket, String key) { S3Client s3Client = this.createClient(); try { DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build(); DeleteObjectResponse deleteObjectResponse = s3.deleteObject(deleteObjectRequest); } catch (Exception e) { e.printStackTrace(); } finally { this.closeClient(s3); } }#批量删除对象 public void deleteObjects(List buckets, String key) { S3Client s3Client = this.createClient(); try { String prefix = key.substring(0, key.lastIndexOf(File.separator)); for (String bucket : buckets) { ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder().bucket(bucket).prefix(prefix).build(); ListObjectsResponse listObjectsResponse = s3.listObjects(listObjectsRequest); List contents = listObjectsResponse.contents(); for (S3Object content : contents) { String objectKey = content.key(); this.deleteObject(s3, bucket, objectKey); } this.deleteObject(s3, bucket, prefix); } } finally { this.closeClient(s3); } }} public String decode(ByteBuffer byteBuffer) { Charset charset = StandardCharsets.UTF_8; return charset.decode(byteBuffer).toString(); } 其中 minIoAccessKey,minIoSecretKey, minIoUrl;分别对应账号、密码、请求地址。需要对应自己的相关信息。
感谢各位的阅读,以上就是"SpringBoot整合aws的方法是什么"的内容了,经过本文的学习后,相信大家对SpringBoot整合aws的方法是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
对象
方法
整合
学习
内容
二进制
信息
前缀
地址
大小
客户
客户端
密码
就是
工具
思路
情况
文件
文章
更多
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
期货软件开发价格
怎么将函数写进数据库
梁平区全过程软件开发服务代理商
网络安全公司取名
网络安全技术研究 基本问题
数据库如何删除多个记录
添加课程表的数据库
网络安全27号文件
英雄联盟有微信服务器吗
伊春市委网络安全委员会
sql导入数据库显示外部表
服务器能24小时上qq吗
石家庄股权投资管理软件开发
电信ip的代理服务器能查到吗
广州电脑服务器生产线
公安天网数据库
2042门户未找到服务器
中文数据库认识
安装数据库时外部组件出现异常
git 上传服务器
特斯拉为啥有数据库
昆达软件开发
软件开发容易脱发
光谷租房软件开发
超高清数据库
网络安全技术与应用课件
erp实施顾问和软件开发
上海app软件开发哪家便宜
迪博数据库的信息怎么下
华为光纤猫网络安全密钥