因为项目使用的是腾讯云的对象存储,数据万象正是基于对象存储而为客户提供的专业一体化的图片解决方案,涵盖图片上传、下载、存储、处理、识别等功能。本文说的是如何使用数据万象对图片进行等比例缩放的问题。
创建好存储桶,为创建好的存储桶开通数据万象服务,这里不多说,不懂的童鞋,请点击如何创建存储桶以及开通数据万象的链接。
腾讯云的官方文档给出了API,持久化处理,这个可以使用postman利用http协议来调试的。分为两种情况:
不管是上传时处理还是云上数据处理,它们的共同点都需要生成经过授权的签名Authorization
/**
* 生成授权的签名
* @param secretId 密钥的id
* @param secretKey 密钥的key
* @param uploadFileName 上传的文件名
* @param httpMethodName http的方法(post、put)
* @return
*/
public String createAuthorizedSignature(String secretId, String secretKey, String uploadFileName, HttpMethodName httpMethodName){
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
COSSigner signer = new COSSigner();
//设置过期时间为1个小时
Date expiredTime = new Date(System.currentTimeMillis() + 3600L * 1000L);
//授权的签名
String authorizedSignature = signer.buildAuthorizationStr(
httpMethodName,
FssConsts.RIGHT_SLASH + uploadFileName,
cred,
expiredTime
);
return authorizedSignature;
}
/**
* 上传原图,同时生成缩略图(put方法)
* 限定缩略图的宽度和高度的最大值分别为 200 和 200,进行等比缩放。
*
* 请求路径 :http://bucketName-appId.pic.region.myqcloud.com/test.jpg
* Pic-Operations : {"is_pic_info":1,"rules":[{"fileid":"test.jpg","rule":"imageMogr2/thumbnail/200x200"}]}
* @param file 文件
* @param bucketName 存储桶名称
* @param uploadFileName 上传文件名
* @param secretId 密钥id
* @param secretKey 密钥key
* @param region 存储地域
* @return
* @throws MalformedURLException
*/
public boolean uploadPicture(MultipartFile file, String bucketName, String uploadFileName,
String secretId, String secretKey, String region) throws MalformedURLException {
boolean flag = false;
//设置请求路径和设置头部、授权签名、缩略图的key
String host = bucketName + ".pic." + region + ".myqcloud.com";
String requestUrl = "http://" + host + "/" + uploadFileName;
URL url = new URL(requestUrl);
String authorizedSignature = createAuthorizedSignature(secretId, secretKey, uploadFileName, HttpMethodName.PUT);
//缩略图在oss上的存储路径
String createThumbnailKey = thumbnail + "/" + uploadFileName);
String operations = "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"" + createThumbnailKey + "\",\"rule\":\"imageMogr2/thumbnail/200x200\"}]}";
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty(TcloudConsts.HTTP_HOST, host);
connection.setRequestProperty(TcloudConsts.HTTP_AUTHORIZATION, authorizedSignature);
connection.setRequestProperty(TcloudConsts.HTTP_PIC_OPERATIONS, operations);
connection.setDoOutput(true);
connection.setRequestMethod(TcloudConsts.HTTP_PUT_METHOD);
// 写入要上传的数据
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
InputStream inputStream = file.getInputStream();
byte[] buff= new byte[1024];
int len;
while( (len = inputStream.read(buff)) != -1) {
out.write(buff, 0, len);
}
inputStream.close();
out.close();
if(200 == connection.getResponseCode()){
flag = true;
}
connection.disconnect();
} catch (IOException e) {
log.error("IO流异常", e);
}
return flag;
}
/**
* 仅上传缩略图(原图已存储于oss上, post方法)
*
* 请求路径 :http://bucketName-appId.pic.region.myqcloud.com/test.jpg?image_process
* Pic-Operations : {"is_pic_info":1,"rules":[{"fileid":"test.jpg","rule":"imageMogr2/thumbnail/200x200"}]}
* @param bucketName
* @param uploadFileName
* @param secretId
* @param secretKey
* @param region
* @return
* @throws MalformedURLException
*/
public static boolean onlyUploadThumbnail(String bucketName, String uploadFileName, String secretId,
String secretKey, String region) throws MalformedURLException {
//设置请求路径和设置头部、授权签名、缩略图的key
String host = bucketName + ".pic." + region + ".myqcloud.com";
String requestUrl = "http://" + host + FssConsts.RIGHT_SLASH + uploadFileName + "?image_process";
URL url = new URL(requestUrl);
String authorizedSignature = createAuthorizedSignature(secretId, secretKey, uploadFileName, HttpMethodName.POST);
//缩略图在oss上的存储路径
String createThumbnailKey = thumbnail + "/" + uploadFileName);
String operations = "{\"is_pic_info\":1,\"rules\":[{\"fileid\":\"" + createThumbnailKey + "\",\"rule\":\"imageMogr2/thumbnail/200x200\"}]}";
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty(TcloudConsts.HTTP_HOST, host);
connection.setRequestProperty(TcloudConsts.HTTP_AUTHORIZATION, authorizedSignature);
connection.setRequestProperty(TcloudConsts.HTTP_PIC_OPERATIONS, operations);
connection.setDoOutput(true);
connection.setRequestMethod(TcloudConsts.HTTP_POST_METHOD);
connection.disconnect();
return true;
} catch (IOException e) {
log.error("IO流异常",e);
return false;
}
}
致此,不管是上传时处理还是云上数据处理的代码均已实现。