赞
踩
基于项目的不同规范写法上会有不同,最近项目中需要适配AmazonS3的文件存储。由于对于上传下载的方法都是在同一个类里面,所以我将初始化S3client的方法单独写成了一个工具类静态访问,不需要属性注入,减少了配置文件,同时由于有多种文件存储系统的实现,避免了在不需要使用S3的时候去注入。
避免引进的一整个sdk过于庞大,单独引入了四个依赖
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> <version>1.12.445</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-core</artifactId> <version>1.12.445</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-kms</artifactId> <version>1.12.445</version> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>jmespath-java</artifactId> <version>1.12.445</version> </dependency>
由于上传下载是一个非常频繁的动作,所以为了避免重复创建对象和安全性,用了单例模式创建对象
import com.amazonaws.ClientConfiguration; import com.amazonaws.Protocol; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; /** * @Author: lsy * @Description: * @DateTime: 2023/5/15 10:29 **/ public class AswUtil { private volatile static AmazonS3 s3; private AswUtil(){}; public static AmazonS3 getInstance(String accessKey,String secretKey,String endPoint){ if(null == s3){ synchronized (AswUtil.class){ if (null == s3){ ClientConfiguration config = new ClientConfiguration(); config.setProtocol(Protocol.HTTP); AwsClientBuilder.EndpointConfiguration endpointConfig = new AwsClientBuilder.EndpointConfiguration(endPoint, Regions.CN_NORTH_1.getName()); AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials); s3 = AmazonS3Client.builder() .withEndpointConfiguration(endpointConfig) .withClientConfiguration(config) .withCredentials(awsCredentialsProvider) .disableChunkedEncoding() .withPathStyleAccessEnabled(true) .build(); } } } return s3; } }
根据业务逻辑不同,参数和实现方法自行调整
private Boolean upload(File file,Map map,String path,String fileName) { String endPointUrl = Strings.getString(map.get("endPoint")); String accessKey = Strings.getString(map.get("accessKey")); String accessKeySecret = Strings.getString(map.get("accessKeySecret")); String path = Strings.getString(map.get("path")); String bucket = Strings.getString(map.get("bucket")); AmazonS3 s3 = AswUtil.getInstance(accessKey, accessKeySecret, endPointUrl); ftpPath = formatPath(path + "/" + ftpPath); String objectName = Strings.trim(ftpPath + "/" + fileName, "/"); return putObject(s3, file, bucket,objectName); } private Boolean putObject(AmazonS3 s3, File file, String bucketName,String objectName) { try { com.amazonaws.services.s3.model.ObjectMetadata objectMetadata = new com.amazonaws.services.s3.model.ObjectMetadata(); objectMetadata.setContentType("application/octet-stream"); objectMetadata.setContentLength(file.length()); com.amazonaws.services.s3.model.PutObjectResult putObjectResult = s3.putObject(new PutObjectRequest(bucketName, objectName, new FileInputStream(file), objectMetadata)); if (null != putObjectResult) { return true; } } catch (Exception e) { log.error("上传文件失败,失败原因:{}",e.getMessage(),e); } return false; }
private Boolean downLoad(String remotePath, String localPath, String remoteFileName, Map map){ String endPointUrl = Strings.getString(map.get("endPoint")); String accessKey = Strings.getString(map.get("accessKey")); String accessKeySecret = Strings.getString(map.get("accessKeySecret")); String path = Strings.getString(map.get("path")); String bucket = Strings.getString(map.get("bucket")); remotePath = formatPath(path + "/" + remotePath); String objectName = Strings.trim(remotePath + "/" + remoteFileName, "/"); AmazonS3 s3 = AswUtil.getInstance(accessKey, accessKeySecret, endPointUrl); return getObject(s3,objectName,bucket,localPath); } private Boolean getObject(AmazonS3 s3, String key,String bucketName,String localPath) { try { S3Object object = s3.getObject(bucketName, key); S3ObjectInputStream objectContent = object.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File(localPath)); byte[] read_buf = new byte[1024]; int read_len = 0; while ((read_len = objectContent.read(read_buf)) > 0) { fos.write(read_buf, 0, read_len); } objectContent.close(); fos.close(); return true; } catch (AmazonServiceException | IOException e) { log.error("下载文件失败,失败原因:{}",e.getMessage(),e); return false; } }
之前也是想要用http的方式直接访问AmazonS3的RestApi接口,但是组装参数过于麻烦,同时相关文档支持又不太具体,于是还是使用了引入jdk的方式来实现。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。