華為云控制臺(tái)
<dependency>
<groupId>com.huaweicloud</groupId>
<artifactId>esdk-obs-java-bundle</artifactId>
<version>[3.21.11,)</version>
</dependency>
# 訪問密鑰 AK
huawei.obs.secret.ak=xxx
# 訪問密鑰 SK
huawei.obs.secret.sk=xxx
# USERNAME
huawei.obs.secret.username=xxx
#上傳的終端節(jié)點(diǎn) Endpoint
huawei.obs.upload.endPoint=xxx
#訪問域名
huawei.obs.access.endPoint=xxx
# 桶,存放地點(diǎn)
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 具體的文件名(含存儲(chǔ)路徑)
* @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上傳文件報(bào)錯(cuò)Exception===============" + e.getMessage());
} finally {
if (obsClient != null) {
try {
// 關(guān)閉OBS連接
obsClient.close();
} catch (IOException e) {
log.error("OBS關(guān)閉連接報(bào)錯(cuò)诸衔!===============" + e.getMessage());
}
}
}
return putObjectResult;
}
/**
* OBS-上傳文件
*
* @param objectKey 具體的文件名(含存儲(chǔ)路徑)
* @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上傳文件報(bào)錯(cuò)Exception===============" + e.getMessage());
} finally {
if (obsClient != null) {
try {
// 關(guān)閉OBS連接
obsClient.close();
} catch (IOException e) {
log.error("OBS關(guān)閉連接報(bào)錯(cuò)蚓聘!===============" + e.getMessage());
}
}
}
return putObjectResult;
}
/**
* OBS-下載文件(獲取對(duì)象)
*
* @param objectKey 具體的文件名(含存儲(chǔ)路徑)
* @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下載文件報(bào)錯(cuò)Exception===============" + e.getMessage());
} finally {
if (obsClient != null) {
try {
// 關(guān)閉OBS連接
obsClient.close();
} catch (IOException e) {
log.error("OBS關(guān)閉連接報(bào)錯(cuò)侦讨!===============" + 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上傳文件報(bào)錯(cuò)Exception===============" + e.getMessage());
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
log.error("bos關(guān)閉連接失敗===============" + e.getMessage());
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
log.error("input關(guān)閉連接失敗===============" + e.getMessage());
}
}
}
return bos;
}
/**
* OBS-打印錯(cuò)誤信息
*
* @param objectKey 路徑KEY
* @param e 錯(cuò)誤信息
* @param action 操作
* @return ByteArrayOutputStream
*/
private static void printObsException(String objectKey, ObsException e, String action) {
log.info("OBS文件報(bào)錯(cuò)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());
// 記錄同步錯(cuò)誤日志
}
public static void main(String[] args) throws Exception {
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者