okhttp和压缩文件实战
一、发起请求处理
import okhttp3.*; import org.junit.jupiter.api.*; import java.io.ioexception; import java.util.concurrent.completablefuture; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.concurrent.timeunit; import java.util.list; import java.util.arraylist; import java.util.stream.collectors; import java.util.logging.level; import java.util.logging.logger; import java.util.map; public class apiservicecaller { private static final executorservice executor = executors.newfixedthreadpool(10, runnable -> { thread thread = new thread(runnable); thread.setname("apiservicecaller-thread"); thread.setdaemon(true); return thread; }); private static final logger logger = logger.getlogger(apiservicecaller.class.getname()); private static final okhttpclient client = new okhttpclient.builder() .connecttimeout(5, timeunit.seconds) .readtimeout(5, timeunit.seconds) .connectionpool(new connectionpool(10, 5, timeunit.minutes)) .retryonconnectionfailure(true) .build(); // 异步调用外部系统 api 的方法 public completablefuture<string> callexternalapi(string url, map<string, string> params, string method) { return completablefuture.supplyasync(() -> { try { request request = buildrequest(url, params, method); return executerequest(request); } catch (exception e) { logger.log(level.severe, "构建请求或执行请求时出错", e); throw new runtimeexception("调用 api 时出错: " + url, e); } }, executor); } // 构建 get 请求 private request buildgetrequest(string url, map<string, string> params) { httpurl.builder httpbuilder = httpurl.parse(url).newbuilder(); if (params != null && !params.isempty()) { params.foreach(httpbuilder::addqueryparameter); } return new request.builder().url(httpbuilder.build()).get().build(); } // 构建 post 请求 private request buildpostrequest(string url, map<string, string> params) throws ioexception { requestbody body = requestbody.create( mediatype.parse("application/json"), new com.fasterxml.jackson.databind.objectmapper().writevalueasstring(params) ); return new request.builder().url(url).post(body).build(); } // 通用请求构建方法 private request buildrequest(string url, map<string, string> params, string method) throws ioexception { if ("get".equalsignorecase(method)) { return buildgetrequest(url, params); } else if ("post".equalsignorecase(method)) { return buildpostrequest(url, params); } else { throw new illegalargumentexception("不支持的方法: " + method); } } // 执行请求并处理响应 private string executerequest(request request) throws ioexception { try (response response = client.newcall(request).execute()) { if (response.issuccessful() && response.body() != null) { string responsebody = response.body().string(); logger.info("收到响应: " + responsebody); return responsebody; } else { logger.warning("收到非正常响应码: " + response.code()); throw new runtimeexception("调用 api 失败,响应码: " + response.code()); } } } // 处理多个不同 url 和参数的 api 调用的方法 public list<completablefuture<string>> callmultipleapis(list<apirequest> apirequests) { logger.info("正在调用多个 api..."); return apirequests.stream() .map(request -> callexternalapi(request.geturl(), request.getparams(), request.getmethod())) .collect(collectors.tolist()); } // 高效处理 completablefuture 结果的方法 public void processapiresponses(list<completablefuture<string>> futures) { completablefuture<void> allof = completablefuture.allof(futures.toarray(new completablefuture[0])); allof.thenaccept(v -> futures.foreach(future -> { future.handle((response, throwable) -> { if (throwable != null) { logger.log(level.severe, "处理 future 出错", throwable); system.err.println("处理 future 出错: " + throwable.getmessage()); } else { logger.info("处理响应: " + response); system.out.println(response); } return null; }); })); } // 主函数,调用 api public static void main(string[] args) { apiservicecaller apiservicecaller = new apiservicecaller(); list<apirequest> apirequests = new arraylist<>(); apirequests.add(new apirequest("http://example.com/api1", map.of("param1", "value1"), "get")); apirequests.add(new apirequest("http://example.com/api2", map.of("key", "value"), "post")); apirequests.add(new apirequest("http://example.com/api3", map.of("param3", "value3"), "get")); logger.info("开始调用 api..."); list<completablefuture<string>> apicalls = apiservicecaller.callmultipleapis(apirequests); apiservicecaller.processapiresponses(apicalls); } // apiservicecaller 的单元测试 public static class apiservicecallertest { @test public void testcallexternalapi_getrequest() { apiservicecaller caller = new apiservicecaller(); completablefuture<string> responsefuture = caller.callexternalapi("http://example.com/api1", map.of("param", "value"), "get"); assertions.assertdoesnotthrow(() -> { string response = responsefuture.get(10, timeunit.seconds); assertions.assertnotnull(response); }); } @test public void testcallexternalapi_postrequest() { apiservicecaller caller = new apiservicecaller(); completablefuture<string> responsefuture = caller.callexternalapi("http://example.com/api1", map.of("key", "value"), "post"); assertions.assertdoesnotthrow(() -> { string response = responsefuture.get(10, timeunit.seconds); assertions.assertnotnull(response); }); } @test public void testcallmultipleapis() { apiservicecaller caller = new apiservicecaller(); list<apirequest> apirequests = new arraylist<>(); apirequests.add(new apirequest("http://example.com/api1", map.of("param1", "value1"), "get")); apirequests.add(new apirequest("http://example.com/api2", map.of("key", "value"), "post")); list<completablefuture<string>> responsefutures = caller.callmultipleapis(apirequests); assertions.assertequals(2, responsefutures.size()); responsefutures.foreach(future -> assertions.assertdoesnotthrow(() -> { string response = future.get(10, timeunit.seconds); assertions.assertnotnull(response); })); } } // 用于保存 api 请求详情的类 public static class apirequest { private final string url; private final map<string, string> params; private final string method; public apirequest(string url, map<string, string> params, string method) { this.url = url; this.params = params; this.method = method; } public string geturl() { return url; } public map<string, string> getparams() { return params; } public string getmethod() { return method; } } } // 确保执行器的优雅关闭 runtime.getruntime().addshutdownhook(new thread(() -> { try { logger.info("正在关闭执行器..."); executor.shutdown(); if (!executor.awaittermination(5, timeunit.seconds)) { logger.warning("执行器未在指定时间内终止。"); executor.shutdownnow(); } } catch (interruptedexception e) { logger.log(level.severe, "关闭过程中断", e); executor.shutdownnow(); } }));
二、压缩文件
import com.amazonaws.services.s3.amazons3; import com.amazonaws.services.s3.amazons3clientbuilder; import com.amazonaws.services.s3.model.s3object; import com.amazonaws.services.s3.model.s3objectinputstream; import java.io.*; import java.util.list; import java.util.concurrent.completablefuture; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.stream.collectors; import java.util.zip.zipentry; import java.util.zip.zipoutputstream; import java.util.concurrent.timeunit; public class s3downloadandcompress { private final amazons3 s3client; private final executorservice executorservice; public s3downloadandcompress(int threadpoolsize) { system.out.println("初始化 s3 客户端和执行器服务..."); this.s3client = amazons3clientbuilder.standard().build(); this.executorservice = executors.newfixedthreadpool(threadpoolsize); } public bytearrayoutputstream getcompressedfilestream(list<string> filekeys, string bucketname) { system.out.println("开始下载和压缩过程..."); bytearrayoutputstream baos = new bytearrayoutputstream(); try (zipoutputstream zipout = new zipoutputstream(baos)) { list<completablefuture<void>> futures = filekeys.stream() .map(filekey -> completablefuture.runasync(() -> { system.out.println("开始下载和压缩文件: " + filekey); downloadandcompressfile(s3client, bucketname, filekey, zipout); system.out.println("完成下载和压缩文件: " + filekey); }, executorservice)) .collect(collectors.tolist()); completablefuture<void> alldownloads = completablefuture.allof(futures.toarray(new completablefuture[0])); alldownloads.join(); system.out.println("所有文件已成功下载和压缩。"); } catch (ioexception e) { system.err.println("下载和压缩过程中出错: " + e.getmessage()); e.printstacktrace(); } finally { system.out.println("关闭执行器服务..."); executorservice.shutdown(); try { if (!executorservice.awaittermination(60, timeunit.seconds)) { system.out.println("执行器服务未能在60秒内终止,正在强制关闭..."); executorservice.shutdownnow(); } } catch (interruptedexception e) { system.out.println("等待执行器服务终止时被中断,强制关闭..."); executorservice.shutdownnow(); } } if (baos.size() == 0) { system.out.println("压缩文件流为空。"); } return baos; } public void savecompressedfiletopath(bytearrayoutputstream compressedstream, string targetpath) { if (compressedstream == null || compressedstream.size() == 0) { system.out.println("压缩文件流为空,无法保存。"); return; } try (fileoutputstream fos = new fileoutputstream(targetpath)) { compressedstream.writeto(fos); system.out.println("压缩文件已保存到: " + targetpath); } catch (ioexception e) { system.err.println("保存压缩文件时出错: " + e.getmessage()); e.printstacktrace(); } } private void downloadandcompressfile(amazons3 s3client, string bucketname, string filekey, zipoutputstream zipout) { synchronized (zipout) { try (s3object s3object = s3client.getobject(bucketname, filekey); s3objectinputstream s3is = s3object.getobjectcontent()) { system.out.println("从桶中下载文件: " + filekey + " 桶名称: " + bucketname); zipentry zipentry = new zipentry(filekey); zipout.putnextentry(zipentry); byte[] buffer = new byte[4096]; int length; while ((length = s3is.read(buffer)) >= 0) { zipout.write(buffer, 0, length); } zipout.closeentry(); system.out.println("文件 " + filekey + " 已添加到 zip 中。"); } catch (ioexception e) { system.err.println("下载或压缩文件时出错: " + filekey + " - " + e.getmessage()); e.printstacktrace(); } } } public static void main(string[] args) { system.out.println("启动 s3downloadandcompress..."); int threadpoolsize = 10; // 这个可以根据需要进行配置 s3downloadandcompress downloader = new s3downloadandcompress(threadpoolsize); list<string> filekeys = list.of("file1.txt", "file2.txt", "file3.txt"); string bucketname = "your-bucket-name"; string targetpath = "compressed_files.zip"; bytearrayoutputstream compressedstream = downloader.getcompressedfilestream(filekeys, bucketname); downloader.savecompressedfiletopath(compressedstream, targetpath); system.out.println("s3downloadandcompress 完成。"); } }
import com.amazonaws.services.s3.amazons3; import com.amazonaws.services.s3.amazons3clientbuilder; import com.amazonaws.services.s3.model.generatepresignedurlrequest; import com.amazonaws.services.s3.model.s3object; import com.amazonaws.services.s3.model.s3objectinputstream; import com.amazonaws.services.s3.transfer.transfermanager; import com.amazonaws.services.s3.transfer.transfermanagerbuilder; import com.amazonaws.services.s3.transfer.download; import com.amazonaws.httpmethod; import java.io.*; import java.net.httpurlconnection; import java.net.url; import java.util.date; import java.util.list; import java.util.concurrent.completablefuture; import java.util.concurrent.executorservice; import java.util.concurrent.executors; import java.util.stream.collectors; import java.util.zip.zipentry; import java.util.zip.zipoutputstream; import java.util.concurrent.timeunit; public class s3downloadandcompress { private final amazons3 s3client; private final executorservice executorservice; private final transfermanager transfermanager; private final string defaultfilename = "default_filename.txt"; // 初始化 amazon s3 客户端和线程池 public s3downloadandcompress(int threadpoolsize) { system.out.println("初始化 s3 客户端和执行器服务..."); this.s3client = amazons3clientbuilder.standard().build(); this.executorservice = executors.newfixedthreadpool(threadpoolsize); this.transfermanager = transfermanagerbuilder.standard().withs3client(s3client).build(); system.out.println("s3 客户端和执行器服务初始化完成。"); } // 获取文件列表,压缩成 zip 文件,并返回压缩后的文件流 public bytearrayoutputstream getcompressedfilestream(list<string> filekeys, string bucketname) { system.out.println("开始下载和压缩过程..."); bytearrayoutputstream baos = new bytearrayoutputstream(); try (zipoutputstream zipout = new zipoutputstream(baos)) { list<completablefuture<void>> futures = filekeys.stream() .map(filekey -> completablefuture.runasync(() -> { system.out.println("开始下载和压缩文件: " + filekey); downloadandcompressfile(bucketname, filekey, zipout); system.out.println("完成下载和压缩文件: " + filekey); }, executorservice)) .collect(collectors.tolist()); completablefuture<void> alldownloads = completablefuture.allof(futures.toarray(new completablefuture[0])); alldownloads.join(); system.out.println("所有文件已成功下载和压缩。"); } catch (ioexception e) { system.err.println("下载和压缩过程中出错: " + e.getmessage()); e.printstacktrace(); } finally { shutdownexecutorservice(); } system.out.println("压缩过程完成,返回压缩文件流。"); return baos; } // 将压缩后的文件流保存到指定路径 public void savecompressedfiletopath(bytearrayoutputstream compressedstream, string targetpath) { if (compressedstream == null || compressedstream.size() == 0) { throw new illegalargumentexception("压缩文件流为空,无法保存。"); } system.out.println("开始将压缩文件保存到路径: " + targetpath); try (fileoutputstream fos = new fileoutputstream(targetpath)) { compressedstream.writeto(fos); system.out.println("压缩文件已保存到: " + targetpath); } catch (ioexception e) { system.err.println("保存压缩文件时出错: " + e.getmessage()); e.printstacktrace(); } } // 从 s3 下载指定文件并保存到目标路径 public void downloadfiletopath(string bucketname, string filekey, string targetpath) { system.out.println("开始从 s3 下载文件: " + filekey + " 到路径: " + targetpath); try { string resolvedfilekey = resolvefilekey(bucketname, filekey); file targetfile = new file(targetpath); download download = transfermanager.download(bucketname, resolvedfilekey, targetfile); download.waitforcompletion(); system.out.println("文件已成功下载到: " + targetpath); } catch (exception e) { system.err.println("下载文件时出错: " + e.getmessage()); e.printstacktrace(); } } // 生成指定文件的临时访问链接 public url generatepresignedurl(string bucketname, string filekey, int expirationminutes) { system.out.println("生成临时链接,文件: " + filekey + " 有效期: " + expirationminutes + " 分钟"); try { string resolvedfilekey = resolvefilekey(bucketname, filekey); date expiration = new date(system.currenttimemillis() + expirationminutes * 60 * 1000); generatepresignedurlrequest request = new generatepresignedurlrequest(bucketname, resolvedfilekey) .withmethod(httpmethod.get) .withexpiration(expiration); url url = s3client.generatepresignedurl(request); system.out.println("生成的临时链接: " + url.tostring()); return url; } catch (exception e) { system.err.println("生成临时链接时出错: " + e.getmessage()); e.printstacktrace(); return null; } } // 使用临时链接下载文件并保存到指定路径 public void downloadfilefrompresignedurl(url presignedurl, string targetpath) { system.out.println("使用临时链接下载文件到路径: " + targetpath); try (bufferedinputstream in = new bufferedinputstream(presignedurl.openstream()); fileoutputstream fileoutputstream = new fileoutputstream(targetpath)) { byte[] databuffer = new byte[8192]; int bytesread; while ((bytesread = in.read(databuffer, 0, 8192)) != -1) { fileoutputstream.write(databuffer, 0, bytesread); } system.out.println("文件已通过临时链接成功下载到: " + targetpath); } catch (ioexception e) { system.err.println("通过临时链接下载文件时出错: " + e.getmessage()); e.printstacktrace(); } } // 使用临时链接获取文件的输入流 public inputstream getfilestreamfrompresignedurl(url presignedurl) { system.out.println("通过临时链接获取文件流: " + presignedurl); try { httpurlconnection connection = (httpurlconnection) presignedurl.openconnection(); connection.setrequestmethod("get"); inputstream inputstream = connection.getinputstream(); system.out.println("成功获取文件流。"); return inputstream; } catch (ioexception e) { system.err.println("通过临时链接获取文件流时出错: " + e.getmessage()); e.printstacktrace(); return null; } } // 解析文件键名,如果文件不存在则返回默认文件名 private string resolvefilekey(string bucketname, string filekey) { system.out.println("解析文件键名: " + filekey); if (s3client.doesobjectexist(bucketname, filekey)) { system.out.println("文件存在: " + filekey); return filekey; } else { system.out.println("文件不存在,使用默认文件名: " + defaultfilename); return defaultfilename; } } // 从 s3 下载文件并将其压缩到 zipoutputstream 中 private void downloadandcompressfile(string bucketname, string filekey, zipoutputstream zipout) { system.out.println("从 s3 下载并压缩文件: " + filekey); synchronized (zipout) { try (s3object s3object = s3client.getobject(bucketname, filekey); s3objectinputstream s3is = s3object.getobjectcontent()) { system.out.println("从桶中下载文件: " + filekey + " 桶名称: " + bucketname); zipentry zipentry = new zipentry(filekey); zipout.putnextentry(zipentry); byte[] buffer = new byte[8192]; int length; while ((length = s3is.read(buffer)) >= 0) { zipout.write(buffer, 0, length); } zipout.closeentry(); system.out.println("文件 " + filekey + " 已添加到 zip 中。"); } catch (ioexception e) { system.err.println("下载或压缩文件时出错: " + filekey + " - " + e.getmessage()); e.printstacktrace(); } } } // 关闭执行器服务 private void shutdownexecutorservice() { system.out.println("关闭执行器服务..."); try { executorservice.shutdown(); if (!executorservice.awaittermination(60, timeunit.seconds)) { system.out.println("执行器服务未能在60秒内终止,正在强制关闭..."); executorservice.shutdownnow(); system.out.println("已调用 shutdownnow() 强制关闭执行器服务。"); } } catch (interruptedexception e) { system.out.println("等待执行器服务终止时被中断,强制关闭..."); executorservice.shutdownnow(); thread.currentthread().interrupt(); } system.out.println("执行器服务已关闭。"); } public static void main(string[] args) { system.out.println("启动 s3downloadandcompress..."); int threadpoolsize = 10; // 这个可以根据需要进行配置 s3downloadandcompress downloader = new s3downloadandcompress(threadpoolsize); list<string> filekeys = list.of("file1.txt", "file2.txt", "file3.txt"); string bucketname = "your-bucket-name"; string targetpath = "compressed_files.zip"; // 下载并压缩文件并保存到目标路径 system.out.println("开始下载并压缩文件..."); downloader.downloadandcompressfiletopath(filekeys, bucketname, targetpath); system.out.println("下载并压缩文件完成。"); // 直接下载到指定路径 system.out.println("开始直接下载文件..."); downloader.downloadfiletopath(bucketname, "file1.txt", "downloaded_file1.txt"); system.out.println("直接下载文件完成。"); // 生成临时链接 system.out.println("开始生成临时链接..."); url presignedurl = downloader.generatepresignedurl(bucketname, "file2.txt", 60); if (presignedurl != null) { system.out.println("访问临时链接: " + presignedurl); // 通过临时链接下载到本地 system.out.println("通过临时链接下载文件..."); downloader.downloadfilefrompresignedurl(presignedurl, "downloaded_from_presigned_url.txt"); system.out.println("通过临时链接下载文件完成。"); // 获取文件流 system.out.println("获取文件流..."); inputstream filestream = downloader.getfilestreamfrompresignedurl(presignedurl); if (filestream != null) { system.out.println("成功获取文件流。"); } } system.out.println("s3downloadandcompress 完成。"); } }
三、文件存储
1. 配置
# bucket 1 configuration aws.buckets.bucket1.accesskey=accesskey1 aws.buckets.bucket1.secretkey=secretkey1 aws.buckets.bucket1.endpoint=http://endpoint1 aws.buckets.bucket1.region=us-east-1 # bucket 2 configuration aws.buckets.bucket2.accesskey=accesskey2 aws.buckets.bucket2.secretkey=secretkey2 aws.buckets.bucket2.endpoint=http://endpoint2 aws.buckets.bucket2.region=us-west-1
2. 实体类
package com.example.s3config; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.stereotype.component; @component @configurationproperties public class bucketconfig { private string accesskey; private string secretkey; private string endpoint; private string region; // getters and setters public string getaccesskey() { return accesskey; } public void setaccesskey(string accesskey) { this.accesskey = accesskey; } public string getsecretkey() { return secretkey; } public void setsecretkey(string secretkey) { this.secretkey = secretkey; } public string getendpoint() { return endpoint; } public void setendpoint(string endpoint) { this.endpoint = endpoint; } public string getregion() { return region; } public void setregion(string region) { this.region = region; } }
3. 配置类
package com.example.s3config; import org.springframework.boot.context.properties.configurationproperties; import org.springframework.stereotype.component; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.util.hashmap; import java.util.map; @component @configurationproperties(prefix = "aws.buckets") public class bucketsconfig { private static final logger logger = loggerfactory.getlogger(bucketsconfig.class); private map<string, bucketconfig> bucketconfigs = new hashmap<>(); public map<string, bucketconfig> getbucketconfigs() { return bucketconfigs; } public void setbucketconfigs(map<string, bucketconfig> bucketconfigs) { this.bucketconfigs = bucketconfigs; // log to confirm if configurations are loaded correctly logger.info("bucket configurations loaded: {}", bucketconfigs.keyset()); } public bucketconfig getbucketconfig(string bucketname) { bucketconfig bucketconfig = bucketconfigs.get(bucketname); if (bucketconfig == null) { throw new illegalargumentexception("invalid bucket name: " + bucketname); } return bucketconfig; } }
4. 初始化类
package com.example.s3config; import com.amazonaws.auth.awsstaticcredentialsprovider; import com.amazonaws.auth.basicawscredentials; import com.amazonaws.services.s3.amazons3; import com.amazonaws.services.s3.amazons3clientbuilder; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.util.map; import java.util.concurrent.concurrenthashmap; @component public class amazons3config { private static final logger logger = loggerfactory.getlogger(amazons3config.class); private final bucketsconfig bucketsconfig; private final map<string, amazons3> amazons3clientscache = new concurrenthashmap<>(); @autowired public amazons3config(bucketsconfig bucketsconfig) { this.bucketsconfig = bucketsconfig; logger.info("amazons3config initialized with bucketsconfig"); } public amazons3 getamazons3client(string bucketname) { // check if client is already in cache if (amazons3clientscache.containskey(bucketname)) { logger.debug("returning cached amazons3 client for bucket: {}", bucketname); return amazons3clientscache.get(bucketname); } // get bucket configuration bucketconfig bucketconfig = bucketsconfig.getbucketconfig(bucketname); // ensure all required configurations are present if (bucketconfig.getaccesskey() == null || bucketconfig.getsecretkey() == null || bucketconfig.getendpoint() == null || bucketconfig.getregion() == null) { throw new illegalargumentexception("incomplete bucket configuration for: " + bucketname); } // initialize amazons3 client basicawscredentials awscreds = new basicawscredentials(bucketconfig.getaccesskey(), bucketconfig.getsecretkey()); amazons3 amazons3 = amazons3clientbuilder.standard() .withcredentials(new awsstaticcredentialsprovider(awscreds)) .withendpointconfiguration( new amazons3clientbuilder.endpointconfiguration(bucketconfig.getendpoint(), bucketconfig.getregion())) .withpathstyleaccessenabled(true) .build(); // cache the client for future use amazons3clientscache.put(bucketname, amazons3); logger.info("amazons3 client created and cached for bucket: {}", bucketname); return amazons3; } }
5. 获取对象
package com.example.s3config; import com.amazonaws.services.s3.amazons3; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.slf4j.logger; import org.slf4j.loggerfactory; import java.io.file; @service public class s3service { private static final logger logger = loggerfactory.getlogger(s3service.class); private final amazons3config amazons3config; @autowired public s3service(amazons3config amazons3config) { this.amazons3config = amazons3config; logger.info("s3service initialized with amazons3config"); } public void uploadfile(string bucketname, string key, file file) { amazons3 amazons3 = amazons3config.getamazons3client(bucketname); amazons3.putobject(bucketname, key, file); logger.info("file uploaded to bucket: {}, key: {}", bucketname, key); } // other operations }
6. 主程序
package com.example.s3config; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.boot.context.properties.enableconfigurationproperties; import org.springframework.boot.commandlinerunner; import org.springframework.context.annotation.bean; @springbootapplication @enableconfigurationproperties(bucketsconfig.class) public class yourapplication { public static void main(string[] args) { springapplication.run(yourapplication.class, args); } @bean commandlinerunner validatebucketsconfig(bucketsconfig bucketsconfig) { return args -> { system.out.println("validating bucket configurations: " + bucketsconfig.getbucketconfigs().keyset()); }; } }
7. 测试类
package com.example.s3config; import org.junit.jupiter.api.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.boot.test.mock.mockito.mockbean; import org.springframework.test.context.testpropertysource; import static org.junit.jupiter.api.assertions.*; @springboottest @testpropertysource("classpath:application.properties") public class bucketsconfigtest { @autowired private bucketsconfig bucketsconfig; @test public void testbucketsconfigloaded() { assertnotnull(bucketsconfig, "bucketsconfig should not be null"); assertfalse(bucketsconfig.getbucketconfigs().isempty(), "bucket configurations should not be empty"); asserttrue(bucketsconfig.getbucketconfigs().containskey("bucket1"), "bucket1 should be present in the configurations"); asserttrue(bucketsconfig.getbucketconfigs().containskey("bucket2"), "bucket2 should be present in the configurations"); } @test public void testgetbucketconfig() { bucketconfig bucket1 = bucketsconfig.getbucketconfig("bucket1"); assertnotnull(bucket1, "bucketconfig for bucket1 should not be null"); assertequals("accesskey1", bucket1.getaccesskey()); assertequals("secretkey1", bucket1.getsecretkey()); assertequals("http://endpoint1", bucket1.getendpoint()); assertequals("us-east-1", bucket1.getregion()); } @test public void testinvalidbucket() { exception exception = assertthrows(illegalargumentexception.class, () -> { bucketsconfig.getbucketconfig("invalidbucket"); }); assertequals("invalid bucket name: invalidbucket", exception.getmessage()); } }
package com.example.s3config; import com.amazonaws.services.s3.amazons3; import org.junit.jupiter.api.test; import org.springframework.beans.factory.annotation.autowired; import org.springframework.boot.test.context.springboottest; import org.springframework.boot.test.mock.mockito.mockbean; import org.springframework.test.context.testpropertysource; import static org.junit.jupiter.api.assertions.*; import static org.mockito.mockito.*; @springboottest @testpropertysource("classpath:application.properties") public class amazons3configtest { @autowired private amazons3config amazons3config; @mockbean private bucketsconfig bucketsconfig; @test public void testgetamazons3client() { // mock the bucketconfig bucketconfig bucketconfig = new bucketconfig(); bucketconfig.setaccesskey("accesskey1"); bucketconfig.setsecretkey("secretkey1"); bucketconfig.setendpoint("http://endpoint1"); bucketconfig.setregion("us-east-1"); when(bucketsconfig.getbucketconfig("bucket1")).thenreturn(bucketconfig); amazons3 s3client = amazons3config.getamazons3client("bucket1"); assertnotnull(s3client, "amazons3 client should not be null"); // verify that the client is cached amazons3 cachedclient = amazons3config.getamazons3client("bucket1"); assertsame(s3client, cachedclient, "cached client should be the same instance"); } @test public void testgetamazons3clientinvalidbucket() { when(bucketsconfig.getbucketconfig("invalidbucket")) .thenthrow(new illegalargumentexception("invalid bucket name: invalidbucket")); exception exception = assertthrows(illegalargumentexception.class, () -> { amazons3config.getamazons3client("invalidbucket"); }); assertequals("invalid bucket name: invalidbucket", exception.getmessage()); } }
package com.example.s3config; import com.amazonaws.services.s3.amazons3; import org.junit.jupiter.api.test; import org.mockito.injectmocks; import org.mockito.mock; import org.springframework.boot.test.context.springboottest; import java.io.file; import static org.mockito.mockito.*; import static org.junit.jupiter.api.assertions.*; @springboottest public class s3servicetest { @mock private amazons3config amazons3config; @mock private amazons3 amazons3; @injectmocks private s3service s3service; @test public void testuploadfile() { string bucketname = "bucket1"; string key = "testfile.txt"; file file = new file("testfile.txt"); when(amazons3config.getamazons3client(bucketname)).thenreturn(amazons3); s3service.uploadfile(bucketname, key, file); verify(amazons3config, times(1)).getamazons3client(bucketname); verify(amazons3, times(1)).putobject(bucketname, key, file); } @test public void testuploadfilewithinvalidbucket() { string bucketname = "invalidbucket"; string key = "testfile.txt"; file file = new file("testfile.txt"); when(amazons3config.getamazons3client(bucketname)) .thenthrow(new illegalargumentexception("invalid bucket name: " + bucketname)); exception exception = assertthrows(illegalargumentexception.class, () -> { s3service.uploadfile(bucketname, key, file); }); assertequals("invalid bucket name: " + bucketname, exception.getmessage()); } }
8.依赖
确保在 pom.xml
中添加以下依赖:
<!-- aws sdk --> <dependency> <groupid>com.amazonaws</groupid> <artifactid>aws-java-sdk-s3</artifactid> <version>1.12.100</version> </dependency> <!-- spring boot starter --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> </dependency> <!-- spring boot configuration processor --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional>true</optional> </dependency> <!-- testing --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <!-- mockito --> <dependency> <groupid>org.mockito</groupid> <artifactid>mockito-core</artifactid> <version>3.9.0</version> <scope>test</scope> </dependency>
到此这篇关于springboot中okhttp和压缩文件的使用的文章就介绍到这了,更多相关springboot使用okhttp内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论