阅读量:2
AmazonS3部署
因为涉及到做的需求的头像的处理,所以需要去找头像的来源,没想到又是我们的老熟人,AmazonS3,巧了已经是第二次用了,上次我是用的别人的工具类去干的,这一次我这边自己编辑具体工具类型。
对应的依赖
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.11.358</version> </dependency>
具体的工具类
import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.regions.RegionUtils; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.*; import lombok.extern.slf4j.Slf4j; import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j public class AmazonS3FileUtils { private static String bucketName = null; private static String endPoint = null; private static String region = null; private static String accessKey = null; private static String secretKey = null; public AmazonS3FileUtils(String bucketName1, String endPoint1, String region1, String accessKey1, String secretKey1) { log.info("AmazonS3FileUtils"); bucketName = bucketName1; endPoint = endPoint1; region = region1; accessKey = accessKey1; secretKey = secretKey1; } private static AmazonS3 getClient() { try { log.error("getClient start"); // 新建一个凭证 log.error("accessKey:"+accessKey); log.error("secretKey:"+secretKey); AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.HTTP); AmazonS3 conn = new AmazonS3Client(credentials, clientConfig); log.error("conn:"+conn.toString()); log.error("endPoint:"+endPoint); conn.setEndpoint(endPoint); //if(!StringUtils.isNull(region)) { //} return conn; } catch (Exception e) { log.error("getClient失败", e.getMessage()); log.error(e.toString()); return null; } } public static boolean upload(InputStream inputStream, String fileName) { AmazonS3 conn = getClient(); try { Bucket bucket = new Bucket(bucketName); ObjectMetadata om1 = new ObjectMetadata(); om1.setContentLength(inputStream.available()); PutObjectResult pb = conn.putObject(bucket.getName(), fileName, inputStream, om1); List<Map<String, Object>> list = list(fileName); if (list.size() > 0) { return true; } return false; } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); return false; } } public static List<Map<String, Object>> list(String fileName) { AmazonS3 conn = getClient(); try { List<Bucket> buckets = conn.listBuckets(); List<Map<String, Object>> files = new ArrayList<Map<String, Object>>(); // 列出 bucket 的内容 ObjectListing objects; if (fileName != null && !"".equals(fileName)) { objects = conn.listObjects(bucketName, fileName); } else { objects = conn.listObjects(bucketName); } do { for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) { Map map = new HashMap(); map.put("fileName", objectSummary.getKey()); map.put("fileSize", objectSummary.getSize()); // map.put("createTime",StringUtils.fromDate(objectSummary.getLastModified())); files.add(map); // System.out.println(objectSummary.getKey() + "\t" + // objectSummary.getSize()); } objects = conn.listNextBatchOfObjects(objects); } while (objects.isTruncated()); return files; } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); return null; } } public static URL download(String fileName) { AmazonS3 conn = getClient(); try { Bucket bucket = new Bucket(bucketName); // 生成对象的下载 URLs (带签名和不带签名),java仅支持带签名的 GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket.getName(), fileName); System.out.println(conn.generatePresignedUrl(request)); return conn.generatePresignedUrl(request); } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); return null; } } public static byte[] downloadInputStream(String fileName) { try { byte[] bytes = downLoadFromUrl(download(fileName).toString()); return bytes; } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); return null; } } public static boolean delete(String fileName) { AmazonS3 conn = getClient(); try { Bucket bucket = new Bucket(bucketName); conn.deleteObject(bucket.getName(), fileName); return true; } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); return false; } } public static void downloadFile(String fileName, OutputStream ouputStream) { AmazonS3 conn = getClient(); InputStream input = null; try { Bucket bucket = new Bucket(bucketName); GetObjectRequest gor = new GetObjectRequest(bucket.getName(), fileName); S3Object object = conn.getObject(gor); input = object.getObjectContent(); byte[] data = new byte[input.available()]; System.out.println(data); int len = 0; while ((len = input.read(data)) != -1) { ouputStream.write(data, 0, len); } System.out.println("下载文件成功"); } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } finally { if (ouputStream != null) { try { ouputStream.close(); } catch (IOException e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } } if (input != null) { try { input.close(); } catch (IOException e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } } } } /** * * @param fileName * @param response * @param oldFileName */ public static void amazonS3Downloading(String fileName, HttpServletResponse response, String oldFileName) { AmazonS3 conn = getClient(); Bucket bucket = new Bucket(bucketName); GetObjectRequest gor = new GetObjectRequest(bucket.getName(), fileName); S3Object object = conn.getObject(gor); if (object != null) { System.out.println("Content-Type: " + object.getObjectMetadata().getContentType()); InputStream input = null; // FileOutputStream fileOutputStream = null; OutputStream out = null; byte[] data = null; try { //获取文件流 //信息头,相当于新建的名字 response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(oldFileName, "UTF-8")); input = object.getObjectContent(); data = new byte[input.available()]; int len = 0; out = response.getOutputStream(); //fileOutputStream = new FileOutputStream(targetFilePath); while ((len = input.read(data)) != -1) { out.write(data, 0, len); } } catch (IOException e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } finally { //关闭输入输出流 if (out != null) { try { out.close(); } catch (IOException e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } } if (input != null) { try { input.close(); } catch (IOException e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } } } } } /** * 从网络Url中下载文件 * * @param urlStr * @throws IOException */ public static byte[] downLoadFromUrl(String urlStr) throws IOException { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时间为3秒 conn.setConnectTimeout(30 * 1000); conn.setDoInput(true); conn.setDoOutput(true); //防止屏蔽程序抓取而返回403错误 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); //连接 conn.connect(); //得到输入流 InputStream inputStream = conn.getInputStream(); byte[] getData = null; try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] d = new byte[1024]; int len = 0; while ((len = inputStream.read(d)) != -1) { outputStream.write(d, 0, len); } outputStream.flush(); //获取自己数组 getData = outputStream.toByteArray(); } catch (Exception e) { log.error("AmazonS3FileUtils失败", e.getMessage()); } finally { if (inputStream != null) { inputStream.close(); } } return getData; } /** * 从输入流中获取字节数组 * * @param inputStream * @return * @throws IOException */ public static byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } }
这是具体的工具类型,可以直接使用
其实本来和前端商议的也是直接用他们之前的转成base64的格式,但是出了点问题,我发现图片的大小好大啊,转成base64我这边没有办法测试看他转成图片后的样子,所以我只能换成附件下载的样子,直接给前端下载一个图片,
@Override public void getLeaderListUrl(HttpServletResponse res,String leaderNo) { List<String> leaderListUrlList = cadreLedgerMapper.getLeaderListUrl(leaderNo); String filename = ""; if(null != leaderListUrlList&&leaderListUrlList.size()>0){ filename = leaderListUrlList.get(0); InputStream inputStream = null; //这是蛮重要的部分,就是这里穿进去AmazonS3需要的参数,然后这样在调用AmazonS3FileUtils方法的时候才可以直接使用, AmazonS3FileUtils amazonS3FileUtils = new AmazonS3FileUtils(bucketName, endPoint, region, accessKey, secretKey); byte[] fileData= AmazonS3FileUtils.downloadInputStream(filename); try { OutputStream out = res.getOutputStream(); res.setCharacterEncoding("utf8"); res.setHeader("Content-disposition", "attachment; filename="+filename); // 更正Content-Type为jpg对应的MIME类型 String[] split = filename.split("\\."); if(split.length>1){ if("png".equals(split[1])){ res.setContentType("image/png"); }else if("bmp".equals(split[1])){ res.setContentType("image/bmp"); }else if("gif".equals(split[1])){ res.setContentType("image/gif"); }else { res.setContentType("image/jpeg"); } }else { res.setContentType("image/jpeg"); } out.write(fileData); out.flush(); out.close(); } catch (IOException ioe) { log.error(ioe.getMessage(), ioe); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException ioe) { log.error(ioe.getMessage(), ioe); } } } }
nacos配置参数
实话说我本来是想直接写死的,我觉得大概率一个项目就我一个地方再用这个东西了,然后就不大对,我刚好可以实践一下之前学习的nacos配置参数
@Value("${amazons3.ceph.bucketName}") private String bucketName; @Value("${amazons3.ceph.endPoint}") private String endPoint; @Value("${amazons3.ceph.region}") private String region; @Value("${amazons3.ceph.accessKey}") private String accessKey; @Value("${amazons3.ceph.secretKey}") private String secretKey;
这是配置上对应的读取nacos的地址
然后在你的配置文件里面找到对应的nacos,然后登录一下,
配置上之后点击发布就可以了,后续如果地址或者账号密码之类的东西改变可以只改配置文件而不影响到代码