java实现基于AmazonS3文件上传、下载、删除操作

1 篇文章 0 订阅
1 篇文章 0 订阅

  1、创建工具类 AwsS3Utils 

import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;

import java.io.File;

/**
 * 工具类:S3服务器中文件上传、删除操作(用来存储jmeter脚本中参数化文件)
 */
public class AwsS3Utils {
    private static org.apache.logging.log4j.Logger logger;
    private static final String FILE_TYPE = "fileType";
    
    //隐私信息已模糊处理(keyID、key、endpoint、bucket)
    /**
     * keyID
     */
    private static String accessKeyID = "*************";

    /**
     * key
     */
    private static String secretKey = "************************";

    /**
     * endpoint
     */
    private static String endpoint = "https://xxx.b.xxxxx.cn:443/";

    /**
     * default bucket
     */
    private static String defaultBucket = "xxxxxxxx";
    /**
     * 向 AWS 客户端明确提供凭证
     */
    private static final BasicAWSCredentials AWS_CREDENTIALS = new BasicAWSCredentials(accessKeyID, secretKey);

    /**
     * s3 客户端
     */
    private static final AmazonS3 S3 = AmazonS3ClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(AWS_CREDENTIALS))
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, ""))
            .build();

    /**
     * 存放文件至s3
     *
     * @param bucketName 桶名称 AmazonS3BucketConstant
     * @param key        key名称
     * @param file       文件
     * @return PutObjectResult
     */
    public static PutObjectResult putFile(String bucketName, String key, File file) {
        long startMillis = System.currentTimeMillis();
        long endMillis;
        PutObjectResult result = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            String name = file.getName();
            //新增文件目录 performanceFile
            // 记录文件类型
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.addUserMetadata(FILE_TYPE, name.substring(name.lastIndexOf(".") + 1));
            putObjectRequest.setMetadata(metadata);
            result = S3.putObject(putObjectRequest);
            endMillis = System.currentTimeMillis();
        } catch (AmazonServiceException e) {
            endMillis = System.currentTimeMillis();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.

        }
        return result;
    }

    /**
     * 根据文件名称进行删除
     * @param bucketName
     * @param key
     * @return
     */
    public static void delFile(String bucketName, String key){
        if(S3.doesBucketExistV2(bucketName)==false){
            logger.info(bucketName+"不存在");
            return ;
        }

        //根据桶名及文件夹名获取该桶该文件夹操作对象
        ListObjectsRequest lor = new ListObjectsRequest().withBucketName(defaultBucket).withPrefix("perfFile/");
        ObjectListing objectListing = S3.listObjects(lor);

        String str_key = null;

        //根据操作对象列出所有文件对象,单个对象使用objectSummary.getKey()即可获取此文件完整路径,配合桶名可以用于操作
        for(S3ObjectSummary objectSummary : objectListing.getObjectSummaries()){
            str_key = objectSummary.getKey();
            //文件名是否匹配
            if(str_key.matches(key)){
                //根据桶名及key信息进行删除
                S3.deleteObject(bucketName,key);
                break;
            }
        }
    }

    public static void delFiles(String key){
          delFile(defaultBucket,key);
    }

    public static PutObjectResult putFile(String key, File file) {
        return putFile(defaultBucket, key, file);
    }

    /**
     * 读取文件
     * @param bucketName 桶名称 AmazonS3BucketConstant
     * @param key        key名称
     * @return S3Object: s3Object.getBucketName(); s3Object.getKey(); InputStream inputStream = s3Object.getObjectContent()...
     * 务必关闭 S3Object:     s3Object.close();
     */
    public static S3Object getFile(String bucketName, String key) {
        long startMillis = System.currentTimeMillis();
        long endMillis;
        S3Object s3Object = null;
        try {
            s3Object = S3.getObject(bucketName, key);
            endMillis = System.currentTimeMillis();
        } catch (AmazonServiceException e) {
            endMillis = System.currentTimeMillis();
        } catch (SdkClientException e) {
            // Amazon S3 couldn't be contacted for a response, or the client
            // couldn't parse the response from Amazon S3.
        }
        return s3Object;
    }
    public static S3Object getFile(String key) {
        return getFile(defaultBucket, key);
    }
}

2、controller层调用示例

@RequestMapping("/parametric")
@RestController
public class ParametricController {

    private ParametricService parametricService ;
    public final Logger logger=  LoggerFactory.getLogger(this.getClass());

    /**
     * 文件上传
     * @param multipartFile
     * @param parametricFilePath
     * @param parametricFileS3Path
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST,value ="/create")
    public void create(@RequestParam("file") MultipartFile multipartFile,
                         @RequestParam("parametricFilePath") String parametricFilePath,
                         @RequestParam("parametricFileS3Path") String parametricFileS3Path) throws  Exception{

        //空文件限制
        if(multipartFile.isEmpty()){
            return "上传文件不能为空!";
        }
        //文件大小限制,最大单文件20M
        if(multipartFile.getSize()>20971520){
            return "上传文件大小超过20M!";
        }
        
        //创建s3 client对象
        AwsS3Utils awsS3Utils=new AwsS3Utils();

        String localFileName=multipartFile.getOriginalFilename();//获取文件名称
        String s3Path="perfFile/";
        String key=s3Path+localFileName;//性能测试参数化文件s3服务器存放位置
        String fileName = localFileName.substring(0,localFileName.length()-4);
        String last4Str=localFileName.substring(localFileName.length()-4);//文件名后缀
        String localFilePath="D:\\testDownloading";

        //文件类型判断,只能为CSV、txt类型
        if(!last4Str.equalsIgnoreCase(".csv")){
            if(!last4Str.equalsIgnoreCase(".txt")){
                return "只能上传csv、txt两种类型的文件!";
            }
        }
        //根据上传的文件名称去参数化文件数据库中查询是否存在重名
        if(parametricService.isExistFile(fileName)){
            return "已存在重名文件,请重命名后上传!";
        }

        //将multipartFile转为File
        File uploadFile=new File(localFileName);
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),uploadFile);
        if(uploadFile.exists()){
            uploadFile.deleteOnExit();
        }
        PutObjectResult putObjectResult=awsS3Utils.putFile(key,uploadFile);
        logger.info("文件"+localFileName+"上传到S3服务器成功!");

    }


   /**
     *从s3服务器中下载参数化文件到本地指定目录
     *@author by junior
     * @date 2020/7/27.
     * @param key 文件KEY
     * @param targetFilePath 本地文件存放路径
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.GET,value ="/download")
    public static void amazonS3Downloading( @RequestParam("key") String key,@RequestParam("targetFilePath") String targetFilePath) throws IOException {
        S3Object parameterFile= AwsS3Utils.getFile(key);
        if(parameterFile!=null){
            InputStream inputStream=null;
            FileOutputStream fileOutputStream=null;
            byte[] data=null;
            try {
                //获取S3object对象文本信息
                inputStream=parameterFile.getObjectContent(); 
                data =new byte[inputStream.available()];
                int len=0;
                fileOutputStream=new FileOutputStream(targetFilePath);

                while((len=inputStream.read(data)) != -1){
                    fileOutputStream.write(data,0,len);
                }
               logger.info("*********下载文件成功!*********");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(fileOutputStream!=null){
                    try {
                        fileOutputStream.close();//文件流关闭
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(inputStream!=null){
                    try {
                        inputStream.close();//文件流关闭
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

   /**
     * 参数化文件信息删除
     * @param parametricId
     * @return
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.DELETE,value = "/delete")
    public void delete(@RequestParam Integer parametricId) throws Exception{
       Integer resultDel = 0;
       Result<InterfaceParametricMappingRecord> records = interfaceParametricMappingDao.selectInterfaceParametricMappingByParametricId(parametricId);

        //关联表中未存在引用
        if (records.size() == 0 && records != null) {
            //删除S3服务器上指定文件
            Result<ParametricRecord> pRecord = parametricDao.selectParametricById(parametricId);
            if (pRecord != null && pRecord.size() > 0) {
                AwsS3Utils awsS3Utils = new AwsS3Utils();
                String paraName = pRecord.get(0).getParametricFileName()+pRecord.get(0).getParametricFileSuffix();
                //性能测试参数化文件s3服务器存放位置
                String s3Path = "perfFile/";
                String key = s3Path + paraName;

                //删除S3服务器上指定文件
                awsS3Utils.delFiles(key);
            }
            resultDel = parametricDao.delParametricById(parametricId);
        }
    }
  
}

如觉得对你有帮助,请记得点个赞,个人整理不易....

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值