com.qcloud.cos.request.UploadFileRequest Java Examples
The following examples show how to use
com.qcloud.cos.request.UploadFileRequest.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QcloudCloudStorageService.java From sdb-mall with Apache License 2.0 | 6 votes |
@Override public String upload(byte[] data, String path) { //腾讯云必需要以"/"开头 if(!path.startsWith("/")) { path = "/" + path; } //上传到腾讯云 UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data); String response = client.uploadFile(request); JSONObject jsonObject = JSONObject.parseObject(response); if(jsonObject.getInteger("code") != 0) { throw new RRException("文件上传失败," + jsonObject.getString("message")); } return config.getQcloudDomain() + path; }
Example #2
Source File: QcloudCloudStorageService.java From renren-fast with GNU General Public License v3.0 | 6 votes |
@Override public String upload(byte[] data, String path) { //腾讯云必需要以"/"开头 if(!path.startsWith("/")) { path = "/" + path; } //上传到腾讯云 UploadFileRequest request = new UploadFileRequest(config.getQcloudBucketName(), path, data); String response = client.uploadFile(request); JSONObject jsonObject = JSONObject.fromObject(response); if(jsonObject.getInt("code") != 0) { throw new RRException("文件上传失败," + jsonObject.getString("message")); } return config.getQcloudDomain() + path; }
Example #3
Source File: FileOp.java From cos-java-sdk with MIT License | 6 votes |
/** * 上传文件请求, 对小文件(8MB以下使用单文件上传接口), 大文件使用分片上传接口 * * @param request * 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, * 其他为失败, message为success或者失败原因 * @throws AbstractCosException * SDK定义的COS异常, 通常是输入参数有误或者环境问题(如网络不通) */ public String uploadFile(UploadFileRequest request) throws AbstractCosException { request.check_param(); String localPath = request.getLocalPath(); long fileSize = 0; try { fileSize = CommonFileUtils.getFileLength(localPath); } catch (Exception e) { throw new UnknownException(e.toString()); } long suitSingleFileSize = 8 * 1024 * 1024; if (fileSize < suitSingleFileSize) { return uploadSingleFile(request); } else { UploadSliceFileRequest sliceRequest = new UploadSliceFileRequest(request); sliceRequest.setInsertOnly(request.getInsertOnly()); return uploadSliceFile(sliceRequest); } }
Example #4
Source File: FileOp.java From cos-java-sdk-v4 with MIT License | 5 votes |
/** * 上传文件请求, 对小文件(8MB以下使用单文件上传接口), 大文件使用分片上传接口 * * @param request 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, 其他为失败, * message为success或者失败原因 * @throws AbstractCosException SDK定义的COS异常, 通常是输入参数有误或者环境问题(如网络不通) */ public String uploadFile(UploadFileRequest request) throws AbstractCosException { request.check_param(); String localPath = request.getLocalPath(); long fileSize = 0; if (request.isUploadFromBuffer()) { fileSize = request.getContentBufer().length; } else { try { fileSize = CommonFileUtils.getFileLength(localPath); } catch (Exception e) { throw new UnknownException(e.toString()); } } long suitSingleFileSize = 8 * 1024 * 1024; if (fileSize < suitSingleFileSize) { return uploadSingleFile(request); } else { UploadSliceFileRequest sliceRequest = new UploadSliceFileRequest(request); sliceRequest.setInsertOnly(request.getInsertOnly()); if (request.isUploadFromBuffer()) { sliceRequest.setContentBufer(request.getContentBufer()); } sliceRequest.setEnableShaDigest(request.isEnableShaDigest()); sliceRequest.setTaskNum(request.getTaskNum()); return uploadSliceFile(sliceRequest); } }
Example #5
Source File: Demo.java From cos-java-sdk with MIT License | 4 votes |
public static void main(String[] args) throws Exception { // 设置用户属性, 包括appid, secretId和SecretKey // 这些属性可以通过cos控制台获取(https://console.qcloud.com/cos) int appId = 1000000; String secretId = "xxxxxxxxxxxxxxxxx"; String secretKey = "xxxxxxxxxxxxxxxxx"; // 初始化cosClient COSClient cosClient = new COSClient(appId, secretId, secretKey); // 设置要操作的bucket String bucketName = "mybucket"; /////////////////////////////////////////////////////////////// // 文件操作 // /////////////////////////////////////////////////////////////// // 1. 上传文件(默认不覆盖) // 将本地的local_file_1.txt上传到bucket下的根分区下,并命名为sample_file.txt // 默认不覆盖, 如果cos上已有文件, 则返回错误 String cosFilePath = "/sample_file.txt"; String localFilePath1 = "src/test/resources/local_file_1.txt"; UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, cosFilePath, localFilePath1); String uploadFileRet = cosClient.uploadFile(uploadFileRequest); System.out.println("upload file ret:" + uploadFileRet); // 2. 上传文件(覆盖) // 将本地的local_file_2.txt上传到bucket下的根分区下,并命名为sample_file.txt String localFilePath2 = "src/test/resources/local_file_2.txt"; UploadFileRequest overWriteFileRequest = new UploadFileRequest(bucketName, cosFilePath, localFilePath2); overWriteFileRequest.setInsertOnly(InsertOnly.OVER_WRITE); String overWriteFileRet = cosClient.uploadFile(overWriteFileRequest); System.out.println("overwrite file ret:" + overWriteFileRet); // 3. 获取文件属性 StatFileRequest statFileRequest = new StatFileRequest(bucketName, cosFilePath); String statFileRet = cosClient.statFile(statFileRequest); System.out.println("stat file ret:" + statFileRet); // 4. 更新文件属性 UpdateFileRequest updateFileRequest = new UpdateFileRequest(bucketName, cosFilePath); updateFileRequest.setBizAttr("测试目录"); updateFileRequest.setAuthority(FileAuthority.WPRIVATE); updateFileRequest.setCacheControl("no cache"); updateFileRequest.setContentDisposition("cos_sample.txt"); updateFileRequest.setContentLanguage("english"); updateFileRequest.setContentEncoding("gzip"); updateFileRequest.setContentType("application/json"); updateFileRequest.setXCosMeta("x-cos-meta-xxx", "xxx"); updateFileRequest.setXCosMeta("x-cos-meta-yyy", "yyy"); String updateFileRet = cosClient.updateFile(updateFileRequest); System.out.println("update file ret:" + updateFileRet); // 5. 更新文件后再次获取属性 statFileRet = cosClient.statFile(statFileRequest); System.out.println("stat file ret:" + statFileRet); // 6. 移动文件 String moveFilePath = "/sample_file_move.txt"; MoveFileRequest moveFileRequest = new MoveFileRequest(bucketName, cosFilePath, moveFilePath); String moveFileRet = cosClient.moveFile(moveFileRequest); System.out.println("move file ret:" + moveFileRet); // 7. 删除文件 DelFileRequest delFileRequest = new DelFileRequest(bucketName, moveFilePath); String delFileRet = cosClient.delFile(delFileRequest); System.out.println("del file ret:" + delFileRet); /////////////////////////////////////////////////////////////// // 目录操作 // /////////////////////////////////////////////////////////////// // 1. 生成目录, 目录名为sample_folder String cosFolderPath = "/xxsample_folder/"; CreateFolderRequest createFolderRequest = new CreateFolderRequest(bucketName, cosFolderPath); String createFolderRet = cosClient.createFolder(createFolderRequest); System.out.println("create folder ret:" + createFolderRet); // 2. 更新目录的biz_attr属性 UpdateFolderRequest updateFolderRequest = new UpdateFolderRequest(bucketName, cosFolderPath); updateFolderRequest.setBizAttr("这是一个测试目录"); String updateFolderRet = cosClient.updateFolder(updateFolderRequest); System.out.println("update folder ret:" + updateFolderRet); // 3. 获取目录属性 StatFolderRequest statFolderRequest = new StatFolderRequest(bucketName, cosFolderPath); String statFolderRet = cosClient.statFolder(statFolderRequest); System.out.println("stat folder ret:" + statFolderRet); // 4. list目录, 获取目录下的成员 ListFolderRequest listFolderRequest = new ListFolderRequest(bucketName, cosFolderPath); String listFolderRet = cosClient.listFolder(listFolderRequest); System.out.println("list folder ret:" + listFolderRet); // 5. 删除目录 DelFolderRequest delFolderRequest = new DelFolderRequest(bucketName, cosFolderPath); String delFolderRet = cosClient.delFolder(delFolderRequest); System.out.println("del folder ret:" + delFolderRet); // 关闭释放资源 cosClient.shutdown(); System.out.println("shutdown!"); }
Example #6
Source File: COS.java From cos-java-sdk-v4 with MIT License | 2 votes |
/** * 上传文件请求, 对小文件(8MB以下)使用单文件上传接口, 大文件使用分片上传接口, 推荐使用 * * @param request 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, 其他为失败, * message为success或者失败原因 */ String uploadFile(UploadFileRequest request);
Example #7
Source File: COS.java From cos-java-sdk-v4 with MIT License | 2 votes |
/** * 上传单文件请求, 不分片,优先推荐使用uploadFile接口 * * @param request 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, 其他为失败, * message为success或者失败原因 */ String uploadSingleFile(UploadFileRequest request);
Example #8
Source File: COS.java From cos-java-sdk with MIT License | 2 votes |
/** * 上传文件请求, 对小文件(8MB以下)使用单文件上传接口, 大文件使用分片上传接口, 推荐使用 * * @param request * 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, * 其他为失败, message为success或者失败原因 */ String uploadFile(UploadFileRequest request);
Example #9
Source File: COS.java From cos-java-sdk with MIT License | 2 votes |
/** * 上传单文件请求, 不分片,优先推荐使用uploadFile接口 * * @param request * 上传文件请求 * @return JSON格式的字符串, 格式为{"code":$code, "message":"$mess"}, code为0表示成功, * 其他为失败, message为success或者失败原因 */ String uploadSingleFile(UploadFileRequest request);