JAVA 华为云OBS上传下载

华为云控制台


    com.huaweicloud
    esdk-obs-java-bundle
    [3.21.11,)

# 访问密钥 AK
huawei.obs.secret.ak=xxx

# 访问密钥 SK
huawei.obs.secret.sk=xxx

# USERNAME
huawei.obs.secret.username=xxx

#上传的终端节点 Endpoint
huawei.obs.upload.endPoint=xxx

#访问域名
huawei.obs.access.endPoint=xxx

# 桶,存放地点
huawei.obs.upload.bucketName=xxx

# 上传的文件夹路径
huawei.obs.upload.parentDir=xxx/
package com.xxx.utils;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.ObsObject;
import com.obs.services.model.PutObjectResult;
import com.sys.api.InterfaceLogHelper;
import com.sys.utils.PropertiesUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class HuaWeiObsUtil {

    private static final Log log = LogFactory.getLog(HuaWeiObsUtil.class);

    private static final String PROP_FILE_NAME = "huaweicloud-obs-config.properties";

    private static final String AK = PropertiesUtils.getSpecifyProperty(PROP_FILE_NAME, "huawei.obs.secret.ak");

    private static final String SK = PropertiesUtils.getSpecifyProperty(PROP_FILE_NAME, "huawei.obs.secret.sk");

    private static final String ENDPOINT = PropertiesUtils.getSpecifyProperty(PROP_FILE_NAME, "huawei.obs.upload.endPoint");

    private static final String BUCKET_NAME = PropertiesUtils.getSpecifyProperty(PROP_FILE_NAME, "huawei.obs.upload.bucketName");

    private static final String PARENT_DIR = PropertiesUtils.getSpecifyProperty(PROP_FILE_NAME, "huawei.obs.upload.parentDir");

    /**
     * OBS-上传文件
     *
     * @param objectKey 具体的文件名(含存储路径)
     * @param inputStream 输入流
     * @return PutObjectResult
     */
    public static PutObjectResult uploadFile(String objectKey, InputStream inputStream) {

        ObsClient obsClient = null;
        PutObjectResult putObjectResult = null;

        try {

            obsClient = new ObsClient(AK, SK, ENDPOINT);
            putObjectResult = obsClient.putObject(BUCKET_NAME, PARENT_DIR + objectKey, inputStream);
            log.info("上传文件PutObjectResult============" + putObjectResult);

        } catch (ObsException e) {
            printObsException(objectKey, e, RpcInterTypeCodeListModel.HUAWEIUPLOADFILE);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("OBS上传文件报错Exception===============" + e.getMessage());
        } finally {

            if (obsClient != null) {
                try {
                    // 关闭OBS连接
                    obsClient.close();
                } catch (IOException e) {
                    log.error("OBS关闭连接报错!===============" + e.getMessage());
                }
            }

        }
        return putObjectResult;
    }

    /**
     * OBS-上传文件
     *
     * @param objectKey 具体的文件名(含存储路径)
     * @param fileName 文件路径
     * @return PutObjectResult
     */
    public static PutObjectResult uploadFile(String objectKey, String fileName) {

        ObsClient obsClient = null;
        PutObjectResult putObjectResult = null;

        try {

            obsClient = new ObsClient(AK, SK, ENDPOINT);
            putObjectResult = obsClient.putObject(BUCKET_NAME, PARENT_DIR + objectKey, new File(fileName));
            log.info("上传文件PutObjectResult============" + putObjectResult);

        } catch (ObsException e) {
            printObsException(objectKey, e, RpcInterTypeCodeListModel.HUAWEIUPLOADFILE);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("OBS上传文件报错Exception===============" + e.getMessage());
        } finally {

            if (obsClient != null) {
                try {
                    // 关闭OBS连接
                    obsClient.close();
                } catch (IOException e) {
                    log.error("OBS关闭连接报错!===============" + e.getMessage());
                }
            }

        }
        return putObjectResult;
    }

    /**
     * OBS-下载文件(获取对象)
     *
     * @param objectKey 具体的文件名(含存储路径)
     * @return ObsObject
     */
    public static ObsObject downloadFile(String objectKey) {

        ObsClient obsClient = null;
        ObsObject obsObject = null;

        try {

            obsClient = new ObsClient(AK, SK, ENDPOINT);
            obsObject = obsClient.getObject(BUCKET_NAME, PARENT_DIR + objectKey);
            log.info("下载文件ObsObject============" + obsObject);

        } catch (ObsException e) {
            printObsException(objectKey, e, RpcInterTypeCodeListModel.HUAWEIDOWNLOADFILE);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("OBS下载文件报错Exception===============" + e.getMessage());
        } finally {

            if (obsClient != null) {
                try {
                    // 关闭OBS连接
                    obsClient.close();
                } catch (IOException e) {
                    log.error("OBS关闭连接报错!===============" + e.getMessage());
                }
            }

        }
        return obsObject;
    }

    /**
     * OBS-下载文件(流式下载)
     *
     * @param obsObject 下载文件信息
     * @return ByteArrayOutputStream
     */
    public static ByteArrayOutputStream convertOutputStream(ObsObject obsObject) {

        InputStream input = null;
        ByteArrayOutputStream bos = null;

        try {

            // 流式下载
            input = obsObject.getObjectContent();
            byte[] b = new byte[1024];
            bos = new ByteArrayOutputStream();
            int len;
            while ((len = input.read(b)) != -1) {
                bos.write(b, 0, len);
            }
            log.info(new String(bos.toByteArray()));

            bos.close();
            input.close();

        } catch (Exception e) {
            e.printStackTrace();
            log.error("OBS上传文件报错Exception===============" + e.getMessage());
        } finally {

            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    log.error("bos关闭连接失败===============" + e.getMessage());
                }
            }
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    log.error("input关闭连接失败===============" + e.getMessage());
                }
            }

        }
        return bos;
    }

    /**
     * OBS-打印错误信息
     *
     * @param objectKey 路径KEY
     * @param e 错误信息
     * @param action 操作
     * @return ByteArrayOutputStream
     */
    private static void printObsException(String objectKey, ObsException e, String action) {
        log.info("OBS文件报错ObsException===============" + objectKey);
        log.info("ObsException e: ====" + e);
        log.info("Response Code: ====" + e.getResponseCode());
        log.info("Error Message: ====" + e.getErrorMessage());
        log.info("Error Code: =======" + e.getErrorCode());
        log.info("Request ID: =======" + e.getErrorRequestId());
        log.info("Host ID: ==========" + e.getErrorHostId());
        // 记录同步错误日志
        
    }

    public static void main(String[] args) throws Exception {

    }

}

你可能感兴趣的:(JAVA 华为云OBS上传下载)