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

S3Util S3 上传下载

时间:2023-05-18 17:24:37浏览次数:31  
标签:return String S3Util 上传下载 fileName S3 file new import

 

S3Util 
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
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.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.Upload;
import com.amazonaws.util.AwsHostNameUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Future;
/***************************
 *<pre>
 * @Project Name : sea-file-service
 * @Package      : com.sea.
 * @File Name    : S3Util
 * @Author       : Sea
 * @Mail         : [email protected]
 * @Date         : 2023/5/18 12:25
 * @Purpose      :
 * @History      :
 *</pre>
 ***************************/
@Slf4j
public class S3Util {

    private static String AWS_ACCESS_KEY = "paZRwWpsuM3vkUuw";
    private static String AWS_SECRET_KEY = "WzLqVt5g9mtM33cQJyQApLOUivXahYeL";
    private static String BUCKET_NAME = "my-bucket";
    private static String S3_ENDPOINT_URL = "http://192.168.18.199:9001";


    public static AmazonS3 s3Client;
    public static TransferManager transferManager;
    static {
        initS3Client();
    }

    /**
     * 默认上传后,返回  bucket + /  + path
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static  class  FileNameTO{
        public String  path;
        public String  bucket;
    }

    /**
     * 默认上传后,返回  bucket + /  + path
     * @param fileName
     * @return
     */
    private static FileNameTO  parseFileName(String fileName){
        if(fileName.contains("/")){
            int index = fileName.indexOf("/");
            fileName = fileName.trim();
            String bucket = fileName.substring(0, index);
            String path = fileName.substring(index + 1);
            System.err.println("bucket " + bucket );
            System.err.println("path  " + path );
            return new FileNameTO(path,bucket);
        }
        return new FileNameTO();
    }
    /**
     * S3初始化
     */
    public static AmazonS3 initS3Client() {
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard();
        if(StringUtils.isNotBlank(S3_ENDPOINT_URL)){
            AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(S3_ENDPOINT_URL, AwsHostNameUtils.parseRegion(S3_ENDPOINT_URL, AmazonS3Client.S3_SERVICE_NAME));
            builder.withEndpointConfiguration(endpointConfiguration);
        }
        if(StringUtils.isNotBlank(AWS_ACCESS_KEY) && StringUtils.isNotBlank(AWS_SECRET_KEY)){
            AWSCredentials awsCredentials = new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY);
            builder.withCredentials(new AWSStaticCredentialsProvider(awsCredentials));
        }
        //设置S3的地区
        //builder.setRegion(regionName);
        s3Client = builder.build();
        transferManager = new TransferManager(s3Client);
        return s3Client;
    }


/**
     * @param bucketName
     * @return
     */
    public  Boolean  createBucket(String bucketName){
         s3Client.createBucket(new CreateBucketRequest(bucketName).withCannedAcl(CannedAccessControlList.PublicReadWrite));
         return true;
    }


    /**
     * 上传图片到S3
     * @param bucketName
     * @param file
     * @param filePath    bucketName/filePath/xx.png
     * @param transferManager
     * @return
     */
    @Async
    public Future<String> uploadFileToBucketAsync(String bucketName, File file, String filePath, TransferManager transferManager) {
        String url = uploadFileToBucket(bucketName, file, filePath, transferManager);
        return new AsyncResult<String>(url);
    }

    /**
     * 上传图片到S3
     * @param bucketName
     * @param file
     * @param filePath    bucketName/filePath/xx.png
     * @param transferManager
     * @return
     */
    public String uploadFileToBucket(String bucketName, File file, String filePath, TransferManager transferManager) {
        if (file.exists() == false) {
            log.error(" !!!  file not exists!");
            return "";
        }
        try {
            log.info("File name is {}", file.getName());
            PutObjectRequest pOrequest = new PutObjectRequest(bucketName, filePath, file).withCannedAcl(CannedAccessControlList.PublicReadWrite);;
            Upload upload = transferManager.upload(pOrequest);
            upload.waitForCompletion();
        } catch (Exception e)
        {
            log.error("upload file name : {} to  S3 error : {} ",file.getName() , e);
        }
        log.info("Upload " + file.getName() + " completed");
        return bucketName+"/"+filePath;
    }



    /**
     * 上传图片到S3 , 默认的bucket
     * @param url  网上url
     * @param S3Directory   bucket/directory/xx.png
     * @throws Exception
     */
    public static String upload(String url, String S3Directory)  {
        String fileName = "";
        try {
                AmazonS3 amazonS3Client = s3Client;
                //将url转为MultipartFile对象
                MultipartFile file = urlToMultipartFile(url);
                //通过url获取图片名称
                fileName = getFileName(url);
                if (StringUtils.isNotEmpty(S3Directory)) {
                    fileName = S3Directory + "/" + fileName;
                }
                ObjectMetadata objectMetadata = new ObjectMetadata();
                objectMetadata.setContentType(file.getContentType());
                objectMetadata.setContentLength(file.getSize());
                //调用S3上传文件
                PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, fileName, file.getInputStream(), objectMetadata)
                        .withCannedAcl(CannedAccessControlList.PublicRead);
                Upload upload = transferManager.upload(putObjectRequest);
                return  BUCKET_NAME+"/"+fileName;
            }
            catch (Exception e){
                log.error("upload file name : {} to  S3 error : {} ",fileName , e);
                return null;
            }
    }


    /**
     * judge existence By fileName
     * @param fileName
     * @return
     */
    public static boolean judgeExistFile(String fileName){
        boolean exist = s3Client.doesObjectExist(BUCKET_NAME, fileName);
        log.info(" {} exist :  {}", fileName , exist);
        return exist;
    }

    /**
     *  根据fileName , 生成临时 URL
     * @param fileName
     * @return
     */
    public static String generatePreViewUrl(String fileName) {
        FileNameTO fileNameTO = parseFileName(fileName);
        URL url = null;
        try {
            java.util.Date expiration = new java.util.Date();
            long milliSeconds = expiration.getTime();
            // Add 2 hour for user to download label.
            milliSeconds +=  2 * 60 * 60 * 1000;
            expiration.setTime(milliSeconds);
            GeneratePresignedUrlRequest generatePresignedUrlRequest =
                    new GeneratePresignedUrlRequest(fileNameTO.getBucket(), fileNameTO.getPath());
            generatePresignedUrlRequest.setMethod(HttpMethod.GET);
            generatePresignedUrlRequest.setExpiration(expiration);
            url = s3Client.generatePresignedUrl(generatePresignedUrlRequest);
            log.info("Pre-Signed URL = " + url.toString());
        } catch (Exception exception) {
            log.error(" {} generatePreViewUrl Error Message: {}" ,fileName, exception.getMessage());
        }
        String urlStr="";
        if(url!=null){
//            urlStr = url.toString().replaceAll("http", "https");
        }
        return urlStr;
    }




    /**
     * 下载文件到本地
     * @param fileName   bucket /  filePath
     * @return
     * @throws Exception
     */
    public static boolean downLoadToLoc(String fileName, String outFilePath){
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            inputStream = download(fileName);
            if(inputStream==null){return false;}
            fileOutputStream = new FileOutputStream(outFilePath);
            IOUtils.copy(inputStream,fileOutputStream);
            return true;
            } 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 false;
    }


    /**
     * down file by fileName
     * @param fileName    bucket /  filePath
     * @return InputStream
     */
    public static InputStream download(String fileName) {
        FileNameTO fileNameTO = parseFileName(fileName);
        InputStream stream = null;
        S3Object s3Object = null;
        try {
            s3Object = s3Client.getObject(new GetObjectRequest(fileNameTO.getBucket(), fileNameTO.getPath()));
            stream = s3Object.getObjectContent();
        } catch (AmazonServiceException ase) {
            if (ase.getStatusCode() == 404) {
                log.warn(fileName + " doesn`t exist in S3 server!");
            } else {
                log.error(" The Exception is {} " , ase);
            }
        } finally {
//            if (s3Object != null) {
//                try {
//                    s3Object.close();
//                } catch (IOException e) {
//                    log.info(e.getMessage(),e);
//                }
//            }
        }
        return stream;
    }




    /**
     * 获取文件列表
     *
     * @param limit
     * @param prefix
     * @return
     * @throws IOException
     */
    public static List<S3ObjectSummary> getFileListPaginator(Integer limit, String prefix) throws IOException {
        try {
            AmazonS3 amazonS3Client = s3Client;
            ListObjectsV2Request req = new ListObjectsV2Request()
                    .withBucketName(BUCKET_NAME).withMaxKeys(limit).withPrefix(prefix);
            ListObjectsV2Result result;
            result = amazonS3Client.listObjectsV2(req);
//            for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
//                System.out.printf(" - %s (size: %d)\n", objectSummary.getKey(), objectSummary.getSize());
//            }
            result.setTruncated(true);
            return result.getObjectSummaries();
        } catch (AmazonServiceException e) {
            // 已经打通S3但处理失败
            e.printStackTrace();
        } catch (SdkClientException e) {
            // 链接S3失败
            e.printStackTrace();
        }
        return null;
    }


    /**
     * url转MultipartFile
     *
     * @param url
     * @return
     * @throws Exception
     */
    public static MultipartFile urlToMultipartFile(String url) throws Exception {
        File file = null;
        MultipartFile multipartFile = null;
        try {
            HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
            httpUrl.connect();
            file = inputStreamToFile(httpUrl.getInputStream(), "template.png");
            multipartFile = fileToMultipartFile(file);
            httpUrl.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return multipartFile;
    }

    /**
     * inputStream 转 File
     *
     * @param ins
     * @param name
     * @return
     * @throws Exception
     */
    public static File inputStreamToFile(InputStream ins, String name) throws Exception {
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

    /**
     * file转multipartFile
     *
     * @param file
     * @return
     */
    public static MultipartFile fileToMultipartFile(File file) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        FileItem item = factory.createItem(file.getName(), "text/plain", true, file.getName());
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try {
            FileInputStream fis = new FileInputStream(file);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new CommonsMultipartFile(item);
    }



    /**
     * 获取图片名称
     * @param url
     * @return
     */
    public static String getFileName(String url) {
        String[] urlArr = (url + "").split("/");
        String fileName = urlArr.length>1 ? urlArr[urlArr.length-1]:url;
        return fileName;
    }


}

 

标签:return,String,S3Util,上传下载,fileName,S3,file,new,import
From: https://www.cnblogs.com/lshan/p/17412546.html

相关文章

  • SpringBoot中实现文件上传下载的三种解决方案(推荐)
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • SpringMVC中实现文件上传下载的三种解决方案(推荐)
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • c# web中实现文件上传下载的三种解决方案(推荐)
    ​IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头。 一. 两个必要响应头Accept-Ranges、ETag        客户端每次提交下载请求时,服务端都要添加这两个响应头,以保证客户端和服务端将此下载识别为可以断点续传......
  • 光伏逆变器总控板,TMS320F28335,2路CAN通讯,2路485通讯,1个EEROM,2路AD采样电路。
    光伏逆变器总控板,TMS320F28335,2路CAN通讯,2路485通讯,1个EEROM,2路AD采样电路。主要功能为采样光伏电池电压,MPPT最大功率跟踪,功率计算,系统开关机等。并且有对应的元器件明细表。提供程序代码。特产适合做此类项目的工程师参考,或者新手作为模板参考。备注:公司成熟产品,提供代码!ID:92566......
  • 量产20KW三相三电平光伏逆变器项目 主控平台:TMS320F28335+T
    量产20KW三相三电平光伏逆变器项目主控平台:TMS320F28335+TM320F28035逆变拓扑:双路光伏BOOST+三相三电平逆变功能:并网发电,双路高精度MPPT;描述:本方案适用于户用光伏系统,本方案主控采用主控TMS320F28335+TM320F28035ID:6168616129047088......
  • S3 MATLAB 图像处理综合实验平台 包括以下功能 Read image-
    S3MATLAB图像处理综合实验平台包括以下功能Readimage-open-gallery导入图片Resolutions图像模糊处理Imagehistogray灰度直方图显示,分水岭图像分割,gamma变换,直方图均衡化SpaticalFiltering图像添加噪声和滤波,以及图像锐化Frequencydomain一维和二维图像傅里叶变化Mo......
  • c#.net中实现文件上传下载的三种解决方案(推荐)
    ​ 以ASP.NETCoreWebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API,包括文件的上传和下载。 准备文件上传的API #region 文件上传  可以带参数        [HttpPost("upload")]        publicJsonResultuploadProject(I......
  • jsp中实现文件上传下载的三种解决方案(推荐)
    ​ 我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用。这次项目的需求:支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,ie8,ie9,Chrome,Firefox,360安全浏览器,并且刷新浏览器后仍然能够续传,重启浏览器(关闭......
  • 埃斯顿量产伺服控制器C代码和硬件图纸 1)TMS320F2 埃斯顿量产伺服控制器C代码和硬件图
    埃斯顿量产伺服控制器C代码和硬件图纸1)TMS320F2埃斯顿量产伺服控制器C代码和硬件图纸1)TMS320F28335+FPGA全套代码;全C写的DSP代码,VHDL写的FPGA代码(LatticeMXO1200)。2)AD电路图和PCB图,主控板、显示板、驱动板(含1KW、2KW和5KW),增量式编码器。3)程序代码能自动识别电机参数、惯量......
  • DIY伺服驱动器方案,某成熟量产型号,基于TMS320F28 DIY伺服驱动器方案,某成熟量产型号,基于
    DIY伺服驱动器方案,某成熟量产型号,基于TMS320F28DIY伺服驱动器方案,某成熟量产型号,基于TMS320F28069设计开发。原理图和PCB源格式(AD打开)-控制板/驱动板/电源板/滤波板基于TMS320F28069的控制源代码产品资料,代码注释少,需要有一定基础,建议小白不要拿ID:27260669815636452......