|
@@ -14,25 +14,51 @@ import java.io.File;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author zhongcp
|
|
* @author zhongcp
|
|
* @Date 2021/9/18
|
|
* @Date 2021/9/18
|
|
*/
|
|
*/
|
|
|
|
+@Component
|
|
public class QcloudFileUtils {
|
|
public class QcloudFileUtils {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(QcloudFileUtils.class);
|
|
private static final Logger logger = LoggerFactory.getLogger(QcloudFileUtils.class);
|
|
|
|
|
|
- static COSClient cosClient = createCli();
|
|
|
|
|
|
+ private static COSClient cosClient;
|
|
|
|
|
|
- static COSClient createCli() {
|
|
|
|
- return createCli("ap-shanghai");
|
|
|
|
|
|
+ private static String accessKey;
|
|
|
|
+
|
|
|
|
+ @Value("${cloud.accessKey}")
|
|
|
|
+ public void setAccessKey(String accessKeyParam) {
|
|
|
|
+ accessKey = accessKeyParam;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String secretKey;
|
|
|
|
+
|
|
|
|
+ @Value("${cloud.secretKey}")
|
|
|
|
+ public void setSecretKey(String secretKeyParam) {
|
|
|
|
+ secretKey = secretKeyParam;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String region;
|
|
|
|
+
|
|
|
|
+ @Value("${cloud.region}")
|
|
|
|
+ public void setRegion(String regionParam) {
|
|
|
|
+ region = regionParam;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static COSClient getCosClient() {
|
|
|
|
+ if (null == cosClient) {
|
|
|
|
+ cosClient = createCli(region);
|
|
|
|
+ }
|
|
|
|
+ return cosClient;
|
|
}
|
|
}
|
|
|
|
|
|
static COSClient createCli(String region) {
|
|
static COSClient createCli(String region) {
|
|
// 初始化用户身份信息(secretId, secretKey)
|
|
// 初始化用户身份信息(secretId, secretKey)
|
|
- COSCredentials cred = new BasicCOSCredentials("AKIDDF4dwzlGVTAmCMGvLVP4UfbHiuqVzFEw",
|
|
|
|
- "TgbFcjZ8HDse9ToujIabLo1yf5YqtfBX");
|
|
|
|
|
|
+ COSCredentials cred = new BasicCOSCredentials(accessKey, secretKey);
|
|
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
|
|
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
|
|
ClientConfig clientConfig = new ClientConfig(new Region(region));
|
|
ClientConfig clientConfig = new ClientConfig(new Region(region));
|
|
// 生成cos客户端
|
|
// 生成cos客户端
|
|
@@ -47,7 +73,7 @@ public class QcloudFileUtils {
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
|
|
putObjectRequest.withMetadata(objectMetadata);
|
|
putObjectRequest.withMetadata(objectMetadata);
|
|
|
|
|
|
- PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
|
|
|
+ PutObjectResult putObjectResult = getCosClient().putObject(putObjectRequest);
|
|
|
|
|
|
LogUtil.info(logger, "put file reqId: " + putObjectResult.getRequestId());
|
|
LogUtil.info(logger, "put file reqId: " + putObjectResult.getRequestId());
|
|
}
|
|
}
|
|
@@ -61,7 +87,7 @@ public class QcloudFileUtils {
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, stream, objectMetadata);
|
|
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, stream, objectMetadata);
|
|
// putObjectRequest.withMetadata(objectMetadata);
|
|
// putObjectRequest.withMetadata(objectMetadata);
|
|
|
|
|
|
- PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
|
|
|
|
|
+ PutObjectResult putObjectResult = getCosClient().putObject(putObjectRequest);
|
|
|
|
|
|
LogUtil.info(logger, "put file reqId: " + putObjectResult.getRequestId());
|
|
LogUtil.info(logger, "put file reqId: " + putObjectResult.getRequestId());
|
|
|
|
|
|
@@ -90,7 +116,7 @@ public class QcloudFileUtils {
|
|
public static void downloadFile(String key, String bucketName, String localDir, String saveName) {
|
|
public static void downloadFile(String key, String bucketName, String localDir, String saveName) {
|
|
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
|
|
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
|
|
File localFile = new File(localDir + saveName);
|
|
File localFile = new File(localDir + saveName);
|
|
- ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, localFile);
|
|
|
|
|
|
+ ObjectMetadata objectMetadata = getCosClient().getObject(getObjectRequest, localFile);
|
|
|
|
|
|
LogUtil.info(logger, "download file content length:" + objectMetadata.getContentLength());
|
|
LogUtil.info(logger, "download file content length:" + objectMetadata.getContentLength());
|
|
}
|
|
}
|