问题描述:上传图片至腾讯云服务器,存储桶Region一直在国内地址,访问正常的,新启一个项目,存储桶Region迁移到孟买ap-mumbai区域,出现上传不成功
问题原因:
<dependency><groupId>com.qcloud</groupId><artifactId>cos_api</artifactId><version>4.4</version>
</dependency>
com.qlcoud版本太低,去腾讯云官网https://cloud.tencent.com/document/product/436/31355,找到升级到 XML Java SDK,按照步骤来,先升级pom文件的jar版本,然后根据实际的业务去更改,主要分3步骤,第一步更换高版本jar包,第二步查文档重写获取COSClient连接的代码(见下图),第三步
低版本UploadFileRequest类在高版本弃用了,使用PutObjectRequest来操作,举个例子(后面也有图片),弄清了很简单,昨天刚开始弄得时候也是一头雾水,因为第三方提供的类方法失效了,意味着你的看新的源码猜测是啥意思,然后get到自己需要的信息返回
老:
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName,"/web/"+cosPath,localPath); String uploadFileRet = cosClient.uploadFile(uploadFileRequest); return uploadFileRet;
新
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "pictrue/" + cosPath, localFile); putObjectRequest.setStorageClass(StorageClass.Standard_IA); Date expiration = new Date(new Date().getTime() + 5 * 60 * 10000); URL url = cosClient.generatePresignedUrl(bucketName, "pictrue/" + cosPath, expiration);
//拼接新的imageUrl String access_url = "http://" + url.getHost() + url.getPath();
return access_url;
ps:老版本
COSClient有个uploadFile(uploadFileRequest)方法,新版本已经弃用,如果需要返回腾讯云服务器访问的信息,通过
URL url = cosClient.generatePresignedUrl(bucketName, "pictrue/" + cosPath, expiration);
//拼接新的imageUrl
String access_url = "http://" + url.getHost() + url.getPath();得到类似的返回给前端的路径json格式
"access_url": "http://sk-1252923386.cos.ap-mumbai.myqcloud.com/pictrue/1556259113368.png"