腾讯cos上传图片

2018-10-22  本文已影响0人  莫夏_b560

1、上传路径"/...../....../....../a.jpg" 上传生成对应的文件目录

2、获取图片可根据需求获取url

public String getImgUrl(String key,boolean needSign){

COSCredentials cred;

if (needSign) {

cred =new BasicCOSCredentials(secretId,secretKey);

}

else {

cred =new AnonymousCOSCredentials();

}

ClientConfig clientConfig =new ClientConfig(new Region(region));

clientConfig.setEndPointSuffix(endPoint);

COSClient cosClient =new COSClient(cred, clientConfig);

GeneratePresignedUrlRequest req =

new GeneratePresignedUrlRequest(bucket, key, HttpMethodName.GET);

//时效

    Date expirationDate =new Date(System.currentTimeMillis() +30L *60L *1000L);

req.setExpiration(expirationDate);

URL downloadUrl = cosClient.generatePresignedUrl(req);

String url = downloadUrl.toString();

url = url.replace(String.valueOf(new StringBuilder(bucket).append(".")),"");

return url;

}

上一篇下一篇

猜你喜欢

热点阅读