<dependency> <groupid>software.amazon.awssdk</groupid> <artifactid>s3</artifactid> </dependency> <dependency> <groupid>com.amazonaws</groupid> <artifactid>aws-java-sdk-s3</artifactid> </dependency> <dependency> <groupid>com.amazonaws</groupid> <artifactid>aws-java-sdk-sqs</artifactid> </dependency> <dependency> <groupid>software.amazon.awssdk</groupid> <artifactid>sns</artifactid> </dependency> <dependency> <groupid>com.amazonaws</groupid> <artifactid>aws-java-sdk-cloudfront</artifactid> </dependency>
private s3client createclient() { awscredentialsprovider credentialsprovider = staticcredentialsprovider.create(awsbasiccredentials.create(minioaccesskey, miniocretkey)); s3client s3 = s3client.builder() .region(region.cn_northwest_1) .credentialsprovider(credentialsprovider) .endpointoverride(uri.create(miniourl)) .build(); return s3; }
public class miniooperate { private string minioaccesskey; private string miniocretkey; private string miniourl; public miniooperate() { } public miniooperate(string minioaccesskey, string miniocretkey, string miniourl) { this.minioaccesskey = minioaccesskey; this.miniocretkey = miniocretkey; this.miniourl = miniourl; }#创建aws的客户端 private s3client createclient() { awscredentialsprovider credentialsprovider = staticcredentialsprovider.create(awsbasiccredentials.create(minioaccesskey, miniocretkey)); 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, miniocretkey));//// amazons3clientbuilder builder = amazons3clientbuilder.standard();// builder.withendpointconfiguration(new awsclientbuilder.endpointconfiguration(miniourl, regions.cn_northwest_1.getname()));//// amazons3 s3client = builder// .withpathstyleaccesnabled(true)// .withcredentials(credentialsprovider)// .build();//// // t the presigned url to expire after one hour.// date expiration = new date();// long exptimemillis = expiration.gettime();// exptimemillis += 1000 * 60 * 60 * 4;// expiration.ttime(exptimemillis);//// // generate the presigned url.// generatepresignedurlrequest generatepresignedurlrequest =// new generatepresignedurlrequest(bucketname, objectkey)// .withmethod(httpmethod.get)// .withexpiration(expiration);//// // t acl// if (!stringutils.impty(acl)) {// generatepresignedurlrequest.withmethod(httpmethod.put);// generatepresignedurlrequest.addrequestparameter(headers.s3_canned_acl, acl);// }//// url = s3client.generatepresignedurl(generatepresignedurlrequest);//// } catch (amazonrviceexception e) {// // the call was transmitted successfully, but amazon s3 couldn't process// // it, so it returned an error respon.// e.printstacktrace();// } catch (sdkclientexception e) {// // amazon s3 couldn't be contacted for a respon, or the client// // couldn't par the respon from amazon s3.// e.printstacktrace();// }//// if (stringutils.impty(url)) {// return null;// }// return url.tostring();// } #获取所有的对象(根据桶和前缀) public list<s3object> listobjects(string bucket, string prefix) { s3client s3client = this.createclient(); list<s3object> contents = null; tr细胞全能性的概念y { listobjectsv2request request = listobjectsv2request.builder().bucket(bucket).prefix(prefix).build(); listobjectsv2respon listobjectsv2respon = s3client.listobjectsv2(request); contents = listobjectsv2respon.contents(); } finally { this.cloclient(s3client); } return contents; } #上传对象 public void putobject(string bucket, string key, string content) { s3client s3client = this.createclient(); try { bytebuffer bytebuffer = bytebuffer.wrap(content.getbytes(standardcharts.utf_8)); putobjectrespon putobjectrespon = s3.putobject(putobjectrequest.builder().bucket(bucket).key(key) .build(), requestbody.frombytebuffer(bytebuffer)); } finally { this.cloclient(s3); } }#上传文件 public void putfile(string filepath, string key, string bucket) { s3client s3client = this.createclient(); file tempfile = new file(filepath); try { putobjectrespon putobjectrespon = s3client.putobject法国梧桐(putobjectrequest.builder().bucket(bucket).key(key) .build(), requestbody.fromfile(tempfile)); } catch (exception e) { e.printstacktrace(); } finally { this.cloclient(s3client); } }#获取对象大小 @override public long gets3objectlength(string bucket, string key) { s3client s3client = this.createclient(); long size = 0; try { list<s3object> s3objects = this.listobjects(s3, bucket, key); size = s3objects.get(0).size(); }catch (exception e){ e.printstacktrace(); }finally { this.cloclient(s3); } return size; } # 获取对象 public string getobject(string bucket, string key) { s3client s3client = this.createclient(); try { responbytes<getobjectrespon> responresponbytes = s3.getobject(getobjectrequest.builder().bucket(bucket).key(key).build(), respontransformer.tobytes()); # respontransformer.tobytes()将响应转换为二进制流 return this.decode(responresponbytes.asbytebuffer()); } finally { this.cloclient(s3); } }# 获取对象的流 @override public inputstream getinputstream(string bucket, string key) { s3client s3client = this.createclient(); try { responbytes<getobjectrespon> responresponbytes = s3.getobject(getobjectrequest.builder().bucket(bucket).key(key).build(), respontransflivedormer.tobytes()); return responresponbytes.asinputstream(); } finally { this.cloclient(s3); } }#删除对象 public void deleteobject(string bucket, string key) { s3client s3client = this.createclient(); try { deleteobjectrequest deleteobjectrequest = deleteobjectrequest.build桃花绽放er().bucket(bucket).key(key).build(); deleteobjectrespon deleteobjectrespon = s3.deleteobject(deleteobjectrequest); } catch (exception e) { e.printstacktrace(); } finally { 四级难吗 this.cloclient(s3); } }#批量删除对象 public void deleteobjects(list<string> buckets, string key) { s3client s3client = this.createclient(); try { string prefix = key.substring(0, key.lastindexof(file.parator)); for (string bucket : buckets) { listobjectsrequest listobjectsrequest = listobjectsrequest.builder().bucket(bucket).prefix(prefix).build(); listobjectsrespon listobjectsrespon = s3.listobjects(listobjectsrequest); list<s3object> contents = listobjectsrespon.contents(); for (s3object content : contents) { string objectkey = content.key(); this.deleteobject(s3, bucket, objectkey); } this.deleteobject(s3, bucket, prefix); } } finally { this.cloclient(s3); } }} public string decode(bytebuffer bytebuffer) { chart chart = standardcharts.utf_8; return chart.decode(bytebuffer).tostring(); }
其中 minioaccesskey,miniocretkey, miniourl;分别对应账号、密码、请求地址。需要对应自己的相关信息。
到此这篇关于springboot整合aws的文章就介绍到这了,更多相关springboot整合aws内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-03 23:50:16,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/15353ba66dd40a4fb5d7d542dc9b0a7c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:SpringBoot整合aws的示例代码.doc
本文 PDF 下载地址:SpringBoot整合aws的示例代码.pdf
留言与评论(共有 0 条评论) |