提交 | 用户 | age
|
7b319c
|
1 |
package com.hx.util; |
C |
2 |
|
|
3 |
import com.aliyun.oss.OSSClient; |
|
4 |
import com.aliyun.oss.model.OSSObject; |
|
5 |
import com.qcloud.cos.COSClient; |
|
6 |
import com.qcloud.cos.ClientConfig; |
|
7 |
import com.qcloud.cos.auth.BasicCOSCredentials; |
|
8 |
import com.qcloud.cos.auth.COSCredentials; |
ccd95b
|
9 |
import com.qcloud.cos.model.ObjectMetadata; |
7b319c
|
10 |
import com.qcloud.cos.model.PutObjectRequest; |
C |
11 |
import com.qcloud.cos.model.PutObjectResult; |
|
12 |
import com.qcloud.cos.region.Region; |
|
13 |
import org.springframework.web.multipart.MultipartFile; |
|
14 |
|
|
15 |
import java.io.ByteArrayOutputStream; |
|
16 |
import java.io.File; |
|
17 |
import java.io.IOException; |
|
18 |
import java.io.InputStream; |
|
19 |
import java.util.Date; |
|
20 |
|
|
21 |
/**腾讯云 COS |
|
22 |
* @author ChenJiaHe |
|
23 |
* @date 2020-12-03 |
|
24 |
*/ |
|
25 |
public class COSUtil { |
|
26 |
|
|
27 |
/** 后端调用上传图片 |
|
28 |
* @param key 上传路径(包括图片名称和和后缀),指定要上传到 COS 上对象键 |
|
29 |
* @param localFile |
|
30 |
* @param secretId 用户id |
|
31 |
* @param secretKey 用户秘钥 |
|
32 |
* @param regionName 存在域,参考腾讯云 |
|
33 |
* @param bucketName 指定要上传到的存储桶 |
|
34 |
* @return |
|
35 |
* @throws IOException |
|
36 |
*/ |
ccd95b
|
37 |
public static String uploadImg(String key,MultipartFile localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { |
7b319c
|
38 |
|
C |
39 |
// 1 初始化用户身份信息(secretId, secretKey)。 |
|
40 |
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); |
|
41 |
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 |
|
42 |
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。 |
|
43 |
Region region = new Region(regionName); |
|
44 |
ClientConfig clientConfig = new ClientConfig(region); |
|
45 |
// 3 生成 cos 客户端。 |
|
46 |
COSClient cosClient = new COSClient(cred, clientConfig); |
|
47 |
|
|
48 |
//开始上传 |
ccd95b
|
49 |
ObjectMetadata objectMetadata = new ObjectMetadata(); |
C |
50 |
// 设置输入流长度为500 |
ee9728
|
51 |
objectMetadata.setContentLength(localFile.getSize()); |
ccd95b
|
52 |
// 设置 Content type, 默认是 application/octet-stream,对于本地文件上传,默认根据本地文件的后缀进行映射 |
C |
53 |
// ,例如 jpg 文件映射 为image/jpeg对于流式上传 默认是 application/octet-stream |
|
54 |
//objectMetadata.setContentType("application/pdf"); |
|
55 |
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key,localFile.getInputStream(),objectMetadata); |
7b319c
|
56 |
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); |
C |
57 |
|
|
58 |
//拼接路径 |
|
59 |
StringBuilder imgUrl = new StringBuilder(); |
486aaa
|
60 |
imgUrl.append("https://"+bucketName+".cos."+regionName+".myqcloud.com"); |
7b319c
|
61 |
if(key.startsWith("/")){ |
C |
62 |
imgUrl.append(key); |
|
63 |
}else{ |
|
64 |
imgUrl.append("/"+key); |
|
65 |
} |
a99c1c
|
66 |
cosClient.shutdown(); |
7b319c
|
67 |
return imgUrl.toString(); |
C |
68 |
|
|
69 |
} |
|
70 |
|
49f28b
|
71 |
|
C |
72 |
/** 后端调用上传图片 |
|
73 |
* @param key 上传路径(包括图片名称和和后缀),指定要上传到 COS 上对象键 |
|
74 |
* @param localFile |
|
75 |
* @param secretId 用户id |
|
76 |
* @param secretKey 用户秘钥 |
|
77 |
* @param regionName 存在域,参考腾讯云 |
|
78 |
* @param bucketName 指定要上传到的存储桶 |
|
79 |
* @return |
|
80 |
* @throws IOException |
|
81 |
*/ |
|
82 |
public static String uploadImg(String key,File localFile,String secretId, String secretKey,String regionName,String bucketName) throws IOException { |
|
83 |
|
|
84 |
// 1 初始化用户身份信息(secretId, secretKey)。 |
|
85 |
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey); |
|
86 |
// 2 设置 bucket 的区域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 |
|
87 |
// clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。 |
|
88 |
Region region = new Region(regionName); |
|
89 |
ClientConfig clientConfig = new ClientConfig(region); |
|
90 |
// 3 生成 cos 客户端。 |
|
91 |
COSClient cosClient = new COSClient(cred, clientConfig); |
|
92 |
|
|
93 |
// 设置 Content type, 默认是 application/octet-stream,对于本地文件上传,默认根据本地文件的后缀进行映射 |
|
94 |
// ,例如 jpg 文件映射 为image/jpeg对于流式上传 默认是 application/octet-stream |
|
95 |
//objectMetadata.setContentType("application/pdf"); |
|
96 |
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile); |
|
97 |
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest); |
|
98 |
|
|
99 |
//拼接路径 |
|
100 |
StringBuilder imgUrl = new StringBuilder(); |
|
101 |
imgUrl.append("https://"+bucketName+".cos."+regionName+".myqcloud.com"); |
|
102 |
if(key.startsWith("/")){ |
|
103 |
imgUrl.append(key); |
|
104 |
}else{ |
|
105 |
imgUrl.append("/"+key); |
|
106 |
} |
a99c1c
|
107 |
cosClient.shutdown(); |
49f28b
|
108 |
return imgUrl.toString(); |
C |
109 |
|
|
110 |
} |
|
111 |
|
7b319c
|
112 |
} |