|
|
|
@ -2,20 +2,24 @@ package com.qniao.rootcloudstatistics.until; |
|
|
|
|
|
|
|
import com.aliyun.oss.OSS; |
|
|
|
import com.aliyun.oss.OSSClient; |
|
|
|
import com.aliyun.oss.model.PutObjectResult; |
|
|
|
import com.qniao.rootcloudstatistics.config.ApolloConfig; |
|
|
|
import com.qniao.rootcloudstatistics.constant.ConfigConstant; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.net.URL; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Objects; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author Lzk |
|
|
|
* @date 2022/10/17 |
|
|
|
**/ |
|
|
|
@Slf4j |
|
|
|
public class OSSUtils { |
|
|
|
// Endpoint以杭州为例,其它Region请按实际情况填写。 |
|
|
|
private static final String endpoint = "http://oss-cn-shenzhen.aliyuncs.com"; |
|
|
|
@ -46,16 +50,26 @@ public class OSSUtils { |
|
|
|
String objectName = folderName + "/" + UUID.randomUUID() + "." + type; |
|
|
|
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret); |
|
|
|
// 填写网络流地址。 |
|
|
|
InputStream inputStream; |
|
|
|
InputStream inputStream = null; |
|
|
|
try { |
|
|
|
inputStream = new URL(url).openStream(); |
|
|
|
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。 |
|
|
|
ossClient.putObject(bucketName, objectName, inputStream); |
|
|
|
PutObjectResult result = ossClient.putObject(bucketName, objectName, inputStream); |
|
|
|
if (Objects.isNull(result)) { |
|
|
|
log.error("------OSS文件上传失败------" + objectName); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} finally { |
|
|
|
// 关闭OSSClient。 |
|
|
|
ossClient.shutdown(); |
|
|
|
if (Objects.nonNull(inputStream)) { |
|
|
|
try { |
|
|
|
inputStream.close(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return "https://" + bucketName + ".oss-cn-shenzhen.aliyuncs.com/" + objectName; |
|
|
|
|