Java上传图⽚对接腾讯COS(对象存储)
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.54</version>
</dependency>
3、功能代码展⽰
# 以下配置信息开通COS资源后得到
cos.accessKey=// SecretId
cos.secretKey=// SecretKey
cos.bucketName=// 存储桶名称
cos.keyName=images  // ⽂件夹名称(可⾃定义)
package;
import TencentCDNService;
import UUIDUtils;
import COSClient;
import ClientConfig;
import BasicCOSCredentials;
import COSCredentials;
import CosClientException;
import CosServiceException;
import*;
import Region;
import Logger;
import LoggerFactory;
import Value;对象图片高清
import Service;
import MultipartFile;
import HttpServletRequest;
import HttpServletResponse;
import File;
import IOException;
import HashMap;
import Map;
/**
* @author: tlw
* @date: 2021年10⽉12⽇
* @Description: COS对象存储
*/
@Service
public class TencentCDNServiceImpl implements TencentCDNService {
private static final Logger LOG = Logger(TencentCDNServiceImpl.class);
@Value("${cos.accessKey}")
private String accessKey;
@Value("${cos.secretKey}")
private String secretKey;
@Value("${ionName}")
private String regionName;
@Value("${cos.bucketName}")
private String bucketName;
@Value("${cos.keyName}")
private String keyName;
private String keyName;
/**
* 上次图⽚对接腾讯CDN
*
* @param fileDataFileName
* @param request
* @param response
* @return
*/
public Map<String, Object> ContentCOS(MultipartFile fileDataFileName, HttpServletRequest request, HttpServletResponse response){        Map<String, Object> map =new HashMap<>();
// 1 初始化⽤户⾝份信息(secretId, secretKey)
COSCredentials cred =new BasicCOSCredentials(accessKey, secretKey);
// 2 设置bucket的区域, COS地域的简称请参照 www.qcloud/document/product/436/6224
ClientConfig clientConfig =new ClientConfig(new Region(regionName));
// 3 ⽣成cos客户端
COSClient cosclient =new COSClient(cred, clientConfig);
// 获取⽂件类型
String name = OriginalFilename();
String fileType = name.substring(name.lastIndexOf(".")+1);
// bucket名需包含appid
String key = keyName +"/"+ UUID()+"."+ fileType;
String url =null;
try{
// 处理⽂件路径
String filePath = Session().getServletContext().getRealPath("/")+ OriginalFilename();
File localFile =new File(filePath);
// 报错请求对象
AppendObjectRequest appendObjectRequest =new AppendObjectRequest(bucketName, key, localFile);
// 设置节点
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = cosclient.appendObject(appendObjectRequest);
// ⽂件⼤⼩
long nextAppendPosition = NextAppendPosition();
LOG.info("⽂件⼤⼩:{}", nextAppendPosition);
// 获取返回对象
GetObjectRequest getObjectRequest =new GetObjectRequest(bucketName, key);
COSObject cosObject = Object(getObjectRequest);
url = ObjectContent().getHttpRequest().getURI().toString();
LOG.info("COS对象地址:{}", url);
map.put("file_path", url);
map.put("success",true);
map.put("msg","上传成功");
}catch(CosServiceException e){
e.printStackTrace();
}catch(CosClientException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
cosclient.shutdown();
}
return map;
}
}
4、运⾏结果集返回
{
"msg":"上传成功",
"file_path":"qcloud/web/uat/dam/C4096AE79B4F4F80BF3994608B9381D6.jpg", "success":true
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。