当前位置:网站首页>Huawei cloud OBS file upload and download tool class

Huawei cloud OBS file upload and download tool class

2022-07-06 08:02:00 Java white notes

Java- Hua Wei Yun OBS File upload and download tools

1. Hua Wei Yun obs File upload download

package com.xxx.util;

import cn.hutool.core.io.FileUtil;
import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.*;
import com.xxx.web.exception.BizErrorException;
import com.xxx.web.exception.enums.BizErrorCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author xxx
 * @createTime 2021/12/6 16:31
 * @description  Huawei OBS Tool class 
 */
@Slf4j
@Component
@RefreshScope
public class HuaweiOBSUtil {

    private static String endPoint;
    private static String ak;
    private static String sk;
    private static String bucketName;

    @Value("${obs.endPoint}")
    public void setEndPoint(String endPoint) {
        HuaweiOBSUtil.endPoint = endPoint;
    }

    @Value("${obs.ak}")
    public void setAk(String ak) {
        HuaweiOBSUtil.ak = ak;
    }

    @Value("${obs.sk}")
    public void setSk(String sk) {
        HuaweiOBSUtil.sk = sk;
    }

    @Value("${obs.bucketName}")
    public void setBucketName(String bucketName) {
        HuaweiOBSUtil.bucketName = bucketName;
    }
    /**
     *  Upload File Type file 
     *
     * @param file
     * @return
     */
    public static String uploadFile(File file) {
        return getUploadFileUrl(file);
    }

    /**
     *  Upload MultipartFile Type file 
     *
     * @param multipartFile
     * @return
     */
    public static String uploadFile(MultipartFile multipartFile) {
        return getUploadFileUrl(com.xxx.util.FileUtil.MultipartFileToFile(multipartFile));
    }

    private static String getUploadFileUrl(File file) {
        if (com.xxx.util.FileUtil.checkFileNotNull(file)) {
            String fileName = FileUtil.getName(file);
            log.info(" To upload pictures :" + fileName);
            /*log.info("ak:" + ak);
            log.info("sk:" + sk);
            log.info("endPoint:" + endPoint);*/
            ObsClient obsClient = new ObsClient(ak, sk, endPoint);
            try {
                // Determine whether the bucket exists , Create if it does not exist 
                if (!obsClient.headBucket(bucketName)) {
                    obsClient.createBucket(bucketName);
                }
                PutObjectRequest request = new PutObjectRequest();
                request.setBucketName(bucketName);
                request.setObjectKey(fileName);
                request.setFile(file);
                request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
                PutObjectResult result = obsClient.putObject(request);
                String url = result.getObjectUrl();
                log.info(" Picture path :" + url);
                return url;
            } catch (Exception e) {
                log.error(" Picture upload error :{}", e);
                throw new BizErrorException(BizErrorCodeEnum.FILE_UPLOAD_FAILURE);
            }/* finally {
                 Delete local temporary files 
                HuaweiOBSUtil.deleteTempFile(file);
            }*/
        }
        return null;
    }


    /**
     *  Upload picture customization code
     *
     * @param ak
     * @param sk
     * @param endPoint
     * @param file
     * @return
     */
    public static String uploadFileByCode(String ak, String sk, String endPoint, String bucket, File file) {
        //String pathname = objectName;
        try {
            String fileName = FileUtil.getName(file);
            log.info(" To upload pictures :" + fileName);
            ObsClient obsClient = new ObsClient(ak, sk, endPoint);
            // Determine whether the bucket exists , Create if it does not exist 
            if (!obsClient.headBucket(bucket)) {
                obsClient.createBucket(bucket);
            }
            PutObjectRequest request = new PutObjectRequest();
            request.setBucketName(bucket);
            request.setObjectKey(fileName);
            request.setFile(file);
            request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ);
            PutObjectResult result = obsClient.putObject(request);
            String url = result.getObjectUrl();
            log.info(" File name :"+fileName+" Picture path :" + url);
            return url;
        } catch (Exception e) {
            log.error(" Picture upload error :{}", e);
        } /*finally {
            HuaweiOBSUtil.deleteTempFile(file);
        }*/
        return null;
    }

    /**
     *  Delete local temporary files 
     *
     * @param file
     */
    public static void deleteTempFile(File file) {
        if (file != null) {
            File del = new File(file.toURI());
            del.delete();
        }
    }
    /**
     *  Get the name and download according to the file address File Files of type 
     * @param fileUrl
     * @return
     */
    public static MultipartFile downloadFileByUrl(String fileUrl){
        try {
            String fileName = getFilenameByUrl(fileUrl);
            //  establish ObsClient example 
            ObsClient obsClient = new ObsClient(ak, sk, endPoint);
            ObsObject obsObject = obsClient.getObject(bucketName, fileName);
            InputStream inputStream = obsObject.getObjectContent();
            // Turn into MultipartFile
            MultipartFile multipartFile = InputStreamConvertMultipartFileUtil.getMultipartFile(inputStream, fileName);
            //File file = com.xxx.util.FileUtil.MultipartFileToFile(multipartFile);
            return multipartFile;

        } catch (ObsException e) {
            log.error(" File download failed :{}", e.getMessage());
            throw new BizErrorException(BizErrorCodeEnum.GET_FILE_DOWNLOAD_URL_FAIL);
        }
    }

    /**
     *  Batch n Days to delete the previous file 
     *
     * @param ak
     * @param sk
     * @param endPoint
     * @param bucket
     * @param requireHours
     */
    public static void batchDeleteForHoursago(String ak, String sk, String endPoint, String bucket, int requireHours) {
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        long currentTime = new Date().getTime();
        try {
            ListObjectsRequest listRequest = new ListObjectsRequest(bucket);
            listRequest.setMaxKeys(1000); //  Return at most each time 1000 Objects 
            ObjectListing listResult;
            Date lastModified;
            long hourMillisecond = 1000 * 3600 * 1;
            //  Paging query 
            do {
                List<KeyAndVersion> toDelete = new ArrayList<>();
                listResult = obsClient.listObjects(listRequest);
                for (ObsObject obsObject : listResult.getObjects()) {
                    lastModified = obsObject.getMetadata().getLastModified();
                    long diffs = (currentTime - lastModified.getTime()) / hourMillisecond; //  Subtract the file modification time from the current time 
                    if (diffs > requireHours
                            && (obsObject.getObjectKey().endsWith(".ts") || obsObject.getObjectKey().endsWith(".mp4"))) {
                        log.info(" The file is now {} Hours , Object change date :{}, File object :{}", diffs, lastModified, obsObject.getObjectKey());
                        toDelete.add(new KeyAndVersion(obsObject.getObjectKey()));
                    }
                }
                //  Set the starting position of the next enumeration 
                listRequest.setMarker(listResult.getNextMarker());

                // Delete files in bulk 
                log.info(" To be deleted OBS Number of objects :{}", toDelete.size());
                if (!CollectionUtils.isEmpty(toDelete)) {
                    DeleteObjectsRequest deleteRequest = new DeleteObjectsRequest(bucket);
                    deleteRequest.setQuiet(true); //  Set to quiet Pattern , Only objects that failed to be deleted are returned 
                    deleteRequest.setKeyAndVersions(toDelete.toArray(new KeyAndVersion[toDelete.size()]));
                    DeleteObjectsResult deleteResult = obsClient.deleteObjects(deleteRequest);
                    if (!CollectionUtils.isEmpty(deleteResult.getErrorResults())) {
                        log.error(" Delete failed OBS Number of objects :{}", deleteResult.getErrorResults().size());
                    }
                }
            } while (listResult.isTruncated());

        } catch (Exception e) {
            log.error(" Huawei OBS Batch deletion exception ", e);
        } finally {
            try {
                obsClient.close();
            } catch (IOException e) {
                log.error(" Huawei OBS Failed to close the client ", e);
            }
        }
    }

    /**
     *  Delete a single object 
     *
     * @param ak
     * @param sk
     * @param endPoint
     * @param bucket
     * @param objectKey
     * @return
     */
    public static boolean deleteFile(String ak, String sk, String endPoint, String bucket, String objectKey) {
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        DeleteObjectResult deleteObjectResult = obsClient.deleteObject(bucket, objectKey);
        boolean deleteMarker = deleteObjectResult.isDeleteMarker();
        try {
            obsClient.close();
        } catch (IOException e) {
            log.error(" Huawei OBS Failed to close the client ", e);
        }
        return deleteMarker;
    }

    /**
     *  Query the used space in the bucket 
     *
     * @param ak
     * @param sk
     * @param endPoint
     * @param bucket
     * @return  Unit byte 
     */
    public static long getBucketUseSize(String ak, String sk, String endPoint, String bucket) {
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        BucketStorageInfo storageInfo = obsClient.getBucketStorageInfo(bucket);
        log.info("{}  Number of objects in bucket :{}   The amount of space used B:{} GB:{}", bucket, storageInfo.getObjectNumber(), storageInfo.getSize(), storageInfo.getSize() / 1024 / 1024 / 1024);
        try {
            obsClient.close();
        } catch (IOException e) {
            log.error(" Huawei OBS Failed to close the client ", e);
        }
        return storageInfo.getSize();
    }


    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            return sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return sbf.toString();
    }

    /**
     *  According to the download address url Get the file name 
     * @param url
     * @throws IOException
     */
    public static String getFilenameByUrl(String url){
        String fileName= null;
        try {
            //url Encoding processing , The Chinese name will become a percent code 
            String decode = URLDecoder.decode(url, "utf-8");
            fileName = decode.substring(decode.lastIndexOf("/")+1);
            log.info("fileName :" + fileName);
        } catch (UnsupportedEncodingException e) {
            log.error("getFilenameByUrl() called with exception => 【url = {}】", url,e);
            e.printStackTrace();
        }
        return fileName;
    }
}

2. Circulation of documents MultipartFile

package com.xxx.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *  Flow the input into a file 
 * @author xiaoxiangyuan
 *
 */
public class InputStreamConvertMultipartFileUtil {

	private static Logger log = LoggerFactory.getLogger(InputStreamConvertMultipartFileUtil.class);

	 /**
     *  Get encapsulated MultipartFile
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return MultipartFile
     */
    public static MultipartFile getMultipartFile(InputStream inputStream, String fileName) {
        FileItem fileItem = createFileItem(inputStream, fileName);
        //CommonsMultipartFile yes feign Yes multipartFile Encapsulation , But to FileItem Class object 
        return new CommonsMultipartFile(fileItem);
    }


    /**
     * FileItem Class object creation 
     *
     * @param inputStream inputStream
     * @param fileName    fileName
     * @return FileItem
     */
    public static FileItem createFileItem(InputStream inputStream, String fileName) {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[10 * 1024 * 1024];
        OutputStream os = null;
        // Use the output stream to output the bytes of the input stream 
        try {
            os = item.getOutputStream();
            while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            inputStream.close();
        } catch (IOException e) {
            log.error("Stream copy exception", e);
            throw new IllegalArgumentException(" File upload failed ");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    log.error("Stream close exception", e);

                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Stream close exception", e);
                }
            }
        }
        return item;
    }

    public static void main(String[] args) {
  }
}

package com.xxx.util.img;

import org.springframework.web.multipart.MultipartFile;

import java.io.*;

public class ConvertToMultipartFile implements MultipartFile {
    private byte[] fileBytes;
    String name;
    String originalFilename;
    String contentType;
    boolean isEmpty;
    long size;

    public ConvertToMultipartFile(byte[] fileBytes, String name, String originalFilename, String contentType,
                                  long size) {
        this.fileBytes = fileBytes;
        this.name = name;
        this.originalFilename = originalFilename;
        this.contentType = contentType;
        this.size = size;
        this.isEmpty = false;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getOriginalFilename() {
        return originalFilename;
    }

    @Override
    public String getContentType() {
        return contentType;
    }

    @Override
    public boolean isEmpty() {
        return isEmpty;
    }

    @Override
    public long getSize() {
        return size;
    }

    @Override
    public byte[] getBytes() throws IOException {
        return fileBytes;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return new ByteArrayInputStream(fileBytes);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        new FileOutputStream(dest).write(fileBytes);
    }
}

3.File Convert to MultipartFile

 /**
     * File Convert to MultipartFile
     * @param file
     * @return
     */
    public static MultipartFile fileToMultipartFile(File file) {
        FileItem item = new org.apache.commons.fileupload.disk.DiskFileItemFactory().createItem("file"
                , MediaType.MULTIPART_FORM_DATA_VALUE
                , true
                , file.getName());
        try (InputStream input = new FileInputStream(file);
             OutputStream os = item.getOutputStream()) {
            //  Flow transfer 
            IOUtils.copy(input, os);
        } catch (Exception e) {
            throw new IllegalArgumentException("Invalid file: " + e, e);
        }

        return new CommonsMultipartFile(item);
    }

4. take MultipartFile Convert to File

/**
     *  take MultipartFile Convert to File
     * @param multiFile
     * @return
     */
    public static File MultipartFileToFile(MultipartFile multiFile) {
        //  Get the file name 
        String fileName = multiFile.getOriginalFilename();
        //  Get file suffix 
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        //  If necessary, prevent duplicate temporary files generated , The random code can be added after the file name 

        try {
            File file = File.createTempFile(FileUtil.getFileNameNotPrefix(fileName), prefix);
            multiFile.transferTo(file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

5.MultipartFile Get the file code as base64

   /** *  take MultipartFile  The image file is encoded as base64 * @param file * @param status true Add :data:multipart/form-data;base64, Prefix  false  Without prefixes  * @return */
    public static String generateBase64(MultipartFile file,Boolean status){
    
        if (file == null || file.isEmpty()) {
    
            throw new RuntimeException(" Picture cannot be empty !");
        }
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String contentType = file.getContentType();
        byte[] imageBytes = null;
        String base64EncoderImg="";
        try {
    
            imageBytes = file.getBytes();
            BASE64Encoder base64Encoder =new BASE64Encoder();
            /** * 1.Java Use BASE64Encoder  You need to add a picture header ("data:" + contentType + ";base64,"), *  among contentType Is the content format of the file . * 2.Java In the use of BASE64Enconder().encode() There will be string newline problems , This is because RFC 822 Specified in the , *  Every time 72 Add a newline symbol to characters , This will cause in use base64 There was a problem with the string , *  So we should first use replaceAll("[\\s*\t\n\r]", "") Solve the problem of line feed . */
            if (status) {
    
                base64EncoderImg = "data:" + contentType + ";base64," + base64Encoder.encode(imageBytes);
            }else{
    
                base64EncoderImg =  base64Encoder.encode(imageBytes);
            }
            base64EncoderImg = base64EncoderImg.replaceAll("[\\s*\t\n\r]", "");
        } catch (IOException e) {
    
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return base64EncoderImg;
    }

原网站

版权声明
本文为[Java white notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060758032552.html