因爲項目使用的是騰訊雲的對象存儲,數據萬象正是基於對象存儲而爲客戶提供的專業一體化的圖片解決方案,涵蓋圖片上傳、下載、存儲、處理、識別等功能。本文說的是如何使用數據萬象對圖片進行等比例縮放的問題。
1 前提
創建好存儲桶,爲創建好的存儲桶開通數據萬象服務,這裏不多說,不懂的童鞋,請點擊如何創建存儲桶以及開通數據萬象的鏈接。
2 如何使用
騰訊雲的官方文檔給出了API,持久化處理,這個可以使用postman利用http協議來調試的。分爲兩種情況:
2.1 上傳時處理(同時上傳原圖和縮略圖)
2.2 雲上數據處理(原圖已上傳,現上傳縮略圖)
2.3 代碼實現
不管是上傳時處理還是雲上數據處理,它們的共同點都需要生成經過授權的簽名Authorization
2.3.1 獲取授權的簽名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;
}
2.3.2 上傳時處理
/**
* 上傳原圖,同時生成縮略圖(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;
}
2.3.3 雲上數據處理
/**
* 僅上傳縮略圖(原圖已存儲於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;
}
}
致此,不管是上傳時處理還是雲上數據處理的代碼均已實現。