首页 > 其他分享 >S3文件上传下载

S3文件上传下载

时间:2023-01-04 09:56:51浏览次数:35  
标签:文件 file String S3 上传下载 param key new logger

  1、文件上传

/**
 * s3文件上传
 * @param file
 * @param bizType
 * @param bizNo
 * @return
 */
public Long uploadS3(MultipartFile file) {
    StopWatch sw = new StopWatch();
    sw.start();
    String extention = FilenameUtils.getExtension(file.getOriginalFilename()).toLowerCase();
    String filePathUrl="";
    String creatorId = getLoginUserId();
    AwsS3Util awsS3Util = null;
    try {
        //创建s3 client对象
        awsS3Util=new AwsS3Util();
        //生成随机文件名
        String localFileName = UUID.randomUUID() + "." + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
        //在随机名前加上年月
        String s3FileFullPath = s3Path+getFileUploadPath() + localFileName;
        ObjectMetadata metadata  = new ObjectMetadata();
        metadata.setContentType(file.getContentType());
        metadata.setContentLength(file.getSize());
        byte[] resultByte = DigestUtils.md5(file.getInputStream());
        String streamMD5 = new String(Base64.encode(resultByte));
        metadata.setContentMD5(streamMD5);
        PutObjectResult putObjectResult=awsS3Util.putFile2(bucket,s3FileFullPath,file.getInputStream(),metadata);
        logger.info("putObjectResult:"+ JSON.toJSONString(putObjectResult));
        logger.info("文件"+localFileName+"上传到S3服务器成功!");
        // 生成URL expiration设置失效时间 单位:秒
        Date expiration = DateUtil.addSecond(new Date(),60);
        GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(bucket, s3FileFullPath).withExpiration(expiration);
        URL url = awsS3Util.generatePresignedUrl(urlRequest);
        logger.info("上传S3完成,url:" + JSON.toJSONString(url));
        //截取?前url永久有效
        filePathUrl = url.toString().substring(0, url.toString().indexOf("?"));
        System.out.println("上传S3完成,filePathUrl:" + filePathUrl);
    } catch (Exception e) {
        logger.error("s3上传文件异常:",e);
        throw new BizException(ResultCode.UPLOAD_ERROR);
    }
    //保存文件入库。        
    sw.stop();
    logger.info("耗时:{} ms.\n", sw.getTotalTimeMillis());
    return attach.getId();
}

  2、文件下载

/**
 * 从S3服务器上下载文件
 * @param attach
 * @param request
 * @param response
 */
private void downloadFromS3(Attach attach, HttpServletRequest request, HttpServletResponse response) {
    try {
        byte[] data = commonUtil.toByteArray((attach.getFilePath()));
        String fileName = attach.getName();
        request.setCharacterEncoding("ISO8859-1");
        String nameStr = URLEncoder.encode(fileName, "UTF-8");
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Headers", "accept,x-requested-with,Content-Type");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + nameStr);
        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        outputStream.write(data);
        outputStream.flush();
        outputStream.close();
        response.flushBuffer();
    } catch (Exception e) {
        logger.error("下载异常:",e);
    }
}

 3、工具类

/**
 * 工具类:S3服务器中文件上传、删除操作
*/
public class AwsS3Util {
    private static Logger logger = LoggerFactory.getLogger(AwsS3Util.class);
    //访问key
    private static String accessKey="aaaaaaaaaaa";
    //秘钥
    private static String secretKey="bbbbbbbbbb";
    //地址
    private static String host="http://192.168.1.1:8080";
    //桶
    private static String bucket="bucketTest";
    //路径
    private static String s3Path="/test";

    /**
     * 向 AWS 客户端明确提供凭证
     */
    private static final BasicAWSCredentials AWS_CREDENTIALS = new BasicAWSCredentials(accessKey, secretKey);

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

    /**
     * 存放文件至s3
     *
     * @param bucketName 桶名称 AmazonS3BucketConstant
     * @param key        key名称
     * @param file       文件
     * @return PutObjectResult
     */
    public static PutObjectResult putFile(String bucketName, String key, File file) {
        StopWatch sw =new StopWatch();
        sw.start();
        PutObjectResult result = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            String name = file.getName();
            //新增文件目录 performanceFile
            // 记录文件类型
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.addUserMetadata("fileType", name.substring(name.lastIndexOf(".") + 1));
            putObjectRequest.setMetadata(metadata);
            result = S3.putObject(putObjectRequest);
        } catch (AmazonServiceException e) {
            logger.error("s3文件上传异常:",e);
        } catch (SdkClientException e) {
            logger.error("s3文件上传异常2:",e);
        }
        sw.stop();
        logger.info("耗时:{} ms.\n", sw.getTotalTimeMillis());
        return result;
    }

    /**
     * 存放文件至s3
     *
     * @param bucketName 桶名称 AmazonS3BucketConstant
     * @param key        key名称
     * @param input       文件
     * @return PutObjectResult
     */
    public static PutObjectResult putFile2(String bucketName, String key, InputStream input,ObjectMetadata metadata) {
        StopWatch sw =new StopWatch();
        sw.start();
        PutObjectResult result = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input,metadata)
                    .withCannedAcl(CannedAccessControlList.PublicRead);
            result = S3.putObject(putObjectRequest);
        } catch (AmazonServiceException e) {
            logger.error("s3文件上传异常2:",e);
        } catch (SdkClientException e) {
            logger.error("s3文件上传异常3:",e);
        }
        sw.stop();
        logger.info("耗时:{} ms.\n", sw.getTotalTimeMillis());
        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(bucket).withPrefix(s3Path);
        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(bucket,key);
    }

    public static PutObjectResult putFile(String key, File file) {
        return putFile(bucket, 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) {
        StopWatch sw = new StopWatch();
        sw.start();
        S3Object s3Object = null;
        try {
            s3Object = S3.getObject(bucketName, key);
        } catch (AmazonServiceException e) {
            logger.error("s3获取文件异常:",e);
        } catch (SdkClientException e) {
            logger.error("s3获取文件异常2:",e);
        }
        sw.stop();
        logger.info("耗时:{} ms.\n", sw.getTotalTimeMillis());
        return s3Object;
    }
    public S3Object getFile(String key) {
        return getFile(bucket, key);
    }

    public URL  generatePresignedUrl(GeneratePresignedUrlRequest urlRequest){
        return S3.generatePresignedUrl(urlRequest);
    }

    public static MultipartFile getMultipartFile(File file) {
        FileItem item = new DiskFileItemFactory().createItem("file"
                , MediaType.MULTIPART_FORM_DATA_VALUE
                , true
                , file.getName());
        try (InputStream input = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            // 流转移
            IOUtils.copy(input, os);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid file: " + e, e);
        }

        return new CommonsMultipartFile(item);
    }    

    public static void getFiles(String key){
        //创建s3 client对象
        AwsS3Util awsS3Util = new AwsS3Util();
        S3Object parameterFile=awsS3Util.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;
                String targetFilePath="D:\\doc\\";
                fileOutputStream=new FileOutputStream(targetFilePath);

                while((len=inputStream.read(data)) != -1){
                    fileOutputStream.write(data,0,len);
                }
            } 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();
                    }
                }
            }
        }
    }
    
     /**
     * 默认文件的存储文件夹路径为当前年/月
     * @return
     */
    private String getFileUploadPath() {
        String path = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String year = df.format(new Date()).substring(0, 4);
        String month = df.format(new Date()).substring(5, 7);
        path = year+separator+month+separator;
        return path;
    }
    
}

 

标签:文件,file,String,S3,上传下载,param,key,new,logger
From: https://www.cnblogs.com/xiaozw/p/17022921.html

相关文章