// 使用高级接口必须先保证本进程存在一个 TransferManager 实例,如果没有则创建// 详细代码参见上传对象:高级接口 -> 示例代码:创建 TransferManagerTransferManager transferManager = createTransferManager();// 存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式String bucketName = "examplebucket-1250000000";// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://www.tencentcloud.com/document/product/436/13324)String key = "exampleobject";// 本地文件路径String localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);ObjectMetadata objectMetadata = new ObjectMetadata();// 设置加密算法为AES256objectMetadata.setServerSideEncryption(SSEAlgorithm.AES256.getAlgorithm());putObjectRequest.setMetadata(objectMetadata);// 服务端加密场景下,返回的etag不再代表文件的md5,所以需要去掉客户端的md5校验// 如有需要,可获取crc64,自行校验System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// 高级接口会返回一个异步结果Upload// 可同步地调用 waitForUploadResult 方法等待上传完成,成功返回UploadResult, 失败抛出异常Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// 确定本进程不再使用 transferManager 实例之后,关闭之// 详细代码参见本页:高级接口 -> 关闭 TransferManagershutdownTransferManager(transferManager);
// 使用高级接口必须先保证本进程存在一个 TransferManager 实例,如果没有则创建// 详细代码参见上传对象:高级接口 -> 示例代码:创建 TransferManagerTransferManager transferManager = createTransferManager();// 存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式String bucketName = "examplebucket-1250000000";// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://www.tencentcloud.com/document/product/436/13324)String key = "exampleobject";// 本地文件路径String localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);String base64EncodedKey = "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=";// sseCustomerKey是base64编码的密钥SSECustomerKey sseCustomerKey = new SSECustomerKey(base64EncodedKey);putObjectRequest.setSSECustomerKey(sseCustomerKey);// 服务端加密场景下,返回的etag不再代表文件的md5,所以需要去掉客户端的md5校验// 如有需要,可获取crc64,自行校验System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// 高级接口会返回一个异步结果Upload// 可同步地调用 waitForUploadResult 方法等待上传完成,成功返回UploadResult, 失败抛出异常Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// 确定本进程不再使用 transferManager 实例之后,关闭之// 详细代码参见本页:高级接口 -> 关闭 TransferManagershutdownTransferManager(transferManager);
// 使用高级接口必须先保证本进程存在一个 TransferManager 实例,如果没有则创建// 详细代码参见上传对象:高级接口 -> 示例代码:创建 TransferManagerTransferManager transferManager = createTransferManager();// 存储桶的命名格式为 BucketName-APPID,此处填写的存储桶名称必须为此格式String bucketName = "examplebucket-1250000000";// 对象键(Key)是对象在存储桶中的唯一标识。详情请参见 [对象键](https://www.tencentcloud.com/document/product/436/13324)String key = "exampleobject";// 本地文件路径String localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);// KMS 的主密钥String kmsKeyId = "your-kms-key-id";String encryptionContext = Base64.encodeAsString("{\\"Ssekmstest\\":\\"Ssekmstest\\"}".getBytes());SSECOSKeyManagementParams ssecosKeyManagementParams = new SSECOSKeyManagementParams(kmsKeyId, encryptionContext);putObjectRequest.setSSECOSKeyManagementParams(ssecosKeyManagementParams);// 服务端加密场景下,返回的etag不再代表文件的md5,所以需要去掉客户端的md5校验// 如有需要,可获取crc64,自行校验System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// 高级接口会返回一个异步结果Upload// 可同步地调用 waitForUploadResult 方法等待上传完成,成功返回UploadResult, 失败抛出异常Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// 确定本进程不再使用 transferManager 实例之后,关闭之// 详细代码参见本页:高级接口 -> 关闭 TransferManagershutdownTransferManager(transferManager);
本页内容是否解决了您的问题?