S3(亚马逊云)工具类及使用【java】
前言
提示:这里是本文要记录的大概内容:
以下是亚马逊云工具类使用说明,有问题请评论 我们一起探讨。
提示:以下是本篇文章正文内容,下面案例可供参考
FileServiceImpl
package com.adups.business.mgmt.service.file.impl;
import com.adups.business.mgmt.config.S3Config;
import com.adups.business.mgmt.service.file.FileService;
import com.adups.business.mgmt.utils.AmazonS3Manager;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.UUID;
@Slf4j
@Service
public class FileServiceImpl implements FileService {
@Autowired
private S3Config s3Config;
/**
* 上传文件
*
* @param uploadPath
* @param uploadFile
* @return
*/
public String upload(String uploadPath, File uploadFile) {
String filePathName = getFilePath(s3Config.getDirectory() + uploadPath, uploadFile);
AmazonS3Manager.uploadToS3(s3Config.getBucketName(), uploadFile, filePathName, CannedAccessControlList.PublicReadWrite);
return filePathName;
}
/**
* 获取临时 URL
*
* @param pathUrl 包存储的路径,形如 xxxx/xxx/xx/x.zip
* @return
*/
@Override
public String getUrl(String pathUrl) {
String url = AmazonS3Manager.getUrlToS3(s3Config.getBucketName(), pathUrl);
return url;
}
/**
* @param path
* @param file
* @desc 生成路径以及文件名
*/
@Override
public String getFilePath(String path, File file) {
/**获取文件后缀名**/
String suffix = file.getName().substring(file.getName().lastIndexOf("."));
/**上传的文件名称重命名**/
String fileName = path + UUID.randomUUID() + "/" + file.getName();
return fileName;
}
}
AmazonS3Manager
package com.adups.business.mgmt.utils;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
/**
* @author DingYF
* @date : 2022/8/19 14:54
*/
@Slf4j
public class AmazonS3Manager {
// public static final Logger logger = LoggerFactory.getLogger(AmazonS3Manager.class);
private static String AMAZON_ENDPOINT_URL;
private static String AMAZON_AWS_ACCESS_KEY;
private static String AMAZON_AWS_SECRET_KEY;
static {
try {
AMAZON_ENDPOINT_URL = "https://jqecs.sgm.saic-gm.com:9021";
AMAZON_AWS_ACCESS_KEY = "ismsqa";
AMAZON_AWS_SECRET_KEY = "Qj4471I7F/0W2T1CenJObP85zaaauDYsMR5imlgS";
} catch (Exception e) {
log.error("amazonS3文件服务器基础参数加载出错" + e.getMessage());
}
log.info("amazonS3 文件服务器基础参数加载完成 access_key=" + AMAZON_AWS_ACCESS_KEY + " endpoint_url=" + AMAZON_ENDPOINT_URL);
}
/**
* 初始化连接,每次使用需要重新连接
*/
public static AmazonS3 initAmazonS3() {
AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(AMAZON_AWS_ACCESS_KEY, AMAZON_AWS_SECRET_KEY));
s3.setRegion(Region.getRegion(Regions.CN_NORTH_1));
s3.setEndpoint(AMAZON_ENDPOINT_URL);
s3.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
return s3;
}
/**
* 上传文件
*
* @param bucketName 桶名
* @param tempFile 待上传文件
* @param remoteFileName 文件名
* @return
*/
public static boolean uploadToS3(String bucketName, File tempFile, String remoteFileName, CannedAccessControlList fileType) {
try {
AmazonS3 s3 = initAmazonS3();
if (!s3.doesBucketExistV2(bucketName)) {
s3.createBucket(bucketName);
}
s3.putObject(new PutObjectRequest(bucketName, remoteFileName, tempFile).withCannedAcl(fileType));
return true;
} catch (Exception ase) {
log.error("amazonS3上传文件File模式异常 " + ase.getMessage(), ase);
}
return false;
}
/**
* 上传文件
*
* @param bucketName 桶名
* @param multipartFile 待上传文件
* @param remoteFileName 文件名
* @return
*/
public static boolean uploadToS3(String bucketName, CommonsMultipartFile multipartFile, String remoteFileName, CannedAccessControlList fileType) {
InputStream in = null;
try {
AmazonS3 s3 = initAmazonS3();
in = multipartFile.getInputStream();
FileItem fileItem = multipartFile.getFileItem();
if (!s3.doesBucketExistV2(bucketName)) {
s3.createBucket(bucketName);
}
ObjectMetadata omd = new ObjectMetadata();
omd.setContentType(fileItem.getContentType());
omd.setContentLength(fileItem.getSize());
omd.setHeader("filename", fileItem.getName());
s3.putObject(new PutObjectRequest(bucketName, remoteFileName, in, omd).withCannedAcl(fileType));
return true;
} catch (Exception ase) {
log.error("amazonS3上传文件InputStream模式异常 " + ase.getMessage(), ase);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
log.error("amazonS3上传文件 关闭InputStream流异常 " + e.getMessage(), e);
}
}
}
return false;
}
/**
* 下载文件
*
* @param bucketName 桶名
* @param remoteFileName 文件名
* @param path 下载路径
*/
public static boolean downFromS3(String bucketName, String remoteFileName, String path) {
try {
AmazonS3 s3 = initAmazonS3();
GetObjectRequest request = new GetObjectRequest(bucketName, remoteFileName);
ObjectMetadata metadata = s3.getObject(request, new File(path));
return true;
} catch (Exception ase) {
log.error("amazonS3下载文件异常 " + ase.getMessage(), ase);
}
return false;
}
/**
* 删除文件
*
* @param bucketName 桶名
* @param remoteFileName 待删除文件名
* @throws IOException
*/
public static void delFromS3(String bucketName, String remoteFileName) {
try {
AmazonS3 s3 = initAmazonS3();
s3.deleteObject(bucketName, remoteFileName);
} catch (Exception ase) {
log.error("amazonS3删除文件异常 " + ase.getMessage(), ase);
}
}
/**
* 获取短链接 带过期时间
*
* @param bucketName 桶名称
* @param remoteFileName 文件名称
* @param expiration 过期时间/秒
*/
public static String getUrlFromS3(String bucketName, String remoteFileName, int expiration) {
try {
AmazonS3 s3 = initAmazonS3();
GeneratePresignedUrlRequest httpRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
httpRequest.setExpiration(new Date(new Date().getTime() + expiration * 1000));
URL url = s3.generatePresignedUrl(httpRequest);
return String.valueOf(url);
} catch (Exception e) {
log.error("amazonS3获取临时链接异常 " + e.getMessage(), e);
}
return null;
}
/**
* 获取永久链接
*
* @param bucketName 桶名称
* @param remoteFileName 文件名称
*/
public static String getUrlFromS3(String bucketName, String remoteFileName) {
String url = "";
try {
AmazonS3 s3 = initAmazonS3();
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
url = String.valueOf(s3.generatePresignedUrl(urlRequest)).split("\\?")[0];
if (url.indexOf(remoteFileName) == -1) {
throw new RuntimeException("url文件名称校验不合法");
}
return url;
} catch (Exception e) {
log.error("amazonS3获取永久链接异常 " + e.getMessage(), e);
return "";
}
}
/**
* 获取永久链接-包含证书
*
* @param bucketName 桶名称
* @param remoteFileName 文件名称
*/
public static String getUrlToS3(String bucketName, String remoteFileName) {
try {
AmazonS3 s3 = initAmazonS3();
GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucketName, remoteFileName);
URL url = s3.generatePresignedUrl(urlRequest);
return String.valueOf(url);
} catch (Exception e) {
log.error("amazonS3获取永久链接异常 " + e.getMessage(), e);
return null;
}
}
public static AmazonS3 getS3() {
return initAmazonS3();
}
/**
* 根据桶名称获取文件集合
*/
public static List<S3ObjectSummary> getFileMsgByBucketName(String bucketName) {
AmazonS3 s3 = initAmazonS3();
ObjectListing objectListing = s3.listObjects(bucketName);
List<S3ObjectSummary> objectSummaries = objectListing.getObjectSummaries();
return objectSummaries;
}
}
S3Config
package com.adups.business.mgmt.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
/**
* @author yuzhengwei
*/
@Configuration
@ConfigurationProperties(prefix = "s3")
@Data
@Lazy
public class S3Config {
private String directory;
private String awsAccessKey;
private String awsSecretKey;
private String bucketName;
private String endpoint;
private String signingRegion;
}
配置yml
#S3配置
s3:
awsAccessKey: ismsqa
awsSecretKey: Qj4471I7F/0W2T1CenJObP85zaaauDYsMR5imlgS
bucketName: isms
endpoint: https://jqecs.sgm.saic-gm.com:9021
signingRegion: cn-northwest-1
directory: rds/
enable: true
#临时存放路径
util:
s3FilePath: /data/rds/s3/
maven包
<!-- AWS SDK start -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.415</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-core</artifactId>
<version>1.11.415</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kms</artifactId>
<version>1.11.415</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>jmespath-java</artifactId>
<version>1.11.415</version>
</dependency>
<!-- AWS SDK end -->
s3配置与使用
链接: link.