腾讯云cos对象存储服务文件上传api就是一个大坑
一、介紹
對象存儲服務(Cloud Object Service)是基于騰訊多年海量服務經驗,對外提供的可靠、安全、易用的海量存儲服務。提供多樣化接入方式,以及全國部署的上傳加速集群,可以無縫銜接CDN進行加速下載。
二、cos 文件上傳api源碼
/** * 單個文件上傳,適用于小文件 * * @param bucketName * bucket名稱 * @param remotePath * 遠程文件路徑 * @param localPath * 本地文件路徑 * @return 服務器端返回的操作結果,成員code為0表示成功,具體參照文檔手冊 * @throws Exception */ public String uploadFile(String bucketName, String remotePath, String localPath) throws Exception { if (!FileProcess.isLegalFile(localPath)) { String errorMsg = localPath + " is not file or not exist or can't be read!"; LOG.error(errorMsg); JSONObject errorRet = new JSONObject(); errorRet.put(ResponseBodyKey.CODE, ErrorCode.PARAMS_ERROR); errorRet.put(ResponseBodyKey.MESSAGE, errorMsg); return errorRet.toString(); } FileInputStream localFileInputStream = null; try { localFileInputStream = FileProcess.getFileInputStream(localPath); return uploadFile(bucketName, remotePath, localFileInputStream); } catch (Exception e) { LOG.error("UploadFile {} occur a error {}", localPath, e.toString()); throw e; } finally { FileProcess.closeFileStream(localFileInputStream, localPath); } }三、為什么是個坑
從上面的代碼中,我們可以看出,使用cos的文件上傳接口,我們需要指定遠程文件地址(就是我們需存儲到cos的那個目錄下的那個文件比如/folder/1.txt)和本地文件路徑。下面我用三點來說為什么是個坑
1.在實際的開發中,很多時候,我們上傳文件到web后端,在controller中以file對象存在,像spring mvc 的MultipartFile 對象是不容易獲取到服務器緩存該文件的路徑;
2.在手機app上傳文件,app通常會采用http的方式把文件以字節數組的方式傳到后臺服務的,莫非還需要們在后臺服務緩存一下;
3.在分布式系統中一般會把文件操作放在一個專門提供上傳下載的分布式服務中比如采用dubbo,在這種方式下,一般采用字節或者采用BASE64Decoder轉化成字符串來傳送文件內容,如果采用cos自己原有的接口,還需要緩存一下文件。
? ? ?綜上所述,cos原有的接口就是一個坑,根本不實用。那么有什么好的解決方法呢,請繼續往下面看。
四,解決方法
在api中自己定義了一個擴展方法,把最后的localpath改為接收字節數組,代碼如下:
/** * 流文件上傳,適用于小文件,自定義擴展方法 * * @param bucketName * bucket名稱 * @param remotePath * 遠程文件路徑 * @param fileContent * 文件字節數組 * @return 服務器端返回的操作結果,成員code為0表示成功,具體參照文檔手冊 * @throws Exception */ public String uploadFileExt(String bucketName, String remotePath, byte[] fileContent) throws Exception { String url = getEncodedCosUrl(bucketName, remotePath); String shaDigest = CommonCodecUtils.getFileSha1(fileContent); HashMap<String, String> postData = new HashMap<String, String>(); postData.put(RequestBodyKey.OP, RequestBodyValue.OP_UPLOAD); postData.put(RequestBodyKey.SHA, shaDigest); long expired = getExpiredTimeInSec(); String sign = Sign.appSignature(appId, secretId, secretKey, expired, bucketName); HashMap<String, String> httpHeader = new HashMap<String, String>(); httpHeader.put(RequestHeaderKey.Authorization, sign); return httpSender.sendFileRequest(url, httpHeader, postData, fileContent, timeOut); }有需要的朋友只需把該方法,拷貝到CosCloud類當中就可以了。
五.把文件轉化成字節數組方式
1、springmvc 上傳controller中MultipartFile payfile文件參數獲取成字節數組方式:
payfile.getBytes();//這個方法就可以獲取字節數組2、將file文件轉化成字節數組方式
public static byte[] getByte(File file) throws Exception { byte[] bytes = null; if (file != null) { InputStream is = new FileInputStream(file); int length = (int) file.length(); if (length > Integer.MAX_VALUE) // 當文件的長度超過了int的最大值 { System.out.println("this file is max "); return null; } bytes = new byte[length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } // 如果得到的字節長度和file實際的長度不一致就可能出錯了 if (offset < bytes.length) { System.out.println("file length is error"); return null; } is.close(); } return bytes; }}注:非常大家瀏覽這篇文章,如果有什么不懂的或者有錯的地方請大家多多指教,謝謝!
總結
以上是生活随笔為你收集整理的腾讯云cos对象存储服务文件上传api就是一个大坑的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不会聊天的程序员,如何开发聊天机器人
- 下一篇: HTML5+CSS大作业——蓝色的异清轩