// Before using the advanced API, ensure that the process contains a TransferManager instance. If such an instance does not exist, create one.// For the detailed code, see "Advanced API -> Sample Code: Creating TransferManager" in "Uploading Objects".TransferManager transferManager = createTransferManager();// Enter the bucket name in the format of `BucketName-APPID`.String bucketName = "examplebucket-1250000000";// Object key, the unique ID of an object in a bucket. For more information, please see [Object Key](https://www.tencentcloud.com/document/product/436/13324).String key = "exampleobject";// Local file pathString localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);ObjectMetadata objectMetadata = new ObjectMetadata();// Set the encryption algorithm to AES-256.objectMetadata.setServerSideEncryption(SSEAlgorithm.AES256.getAlgorithm());putObjectRequest.setMetadata(objectMetadata);// For server-side encryption scenarios, the returned ETag is not the MD5 value of the file. Therefore, MD5 verification on the client should be removed.// You can obtain the CRC64 value for verification as needed.System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// The advanced API will return an asynchronous result `Upload`.// You can call the `waitForUploadResult` method to wait for the upload to complete. If the upload is successful, `UploadResult` is returned. If the upload fails, an exception is returned.Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// After confirming that the process does not use the TransferManager anymore, close it.// For the detailed code, see "Advanced APIs -> Closing TransferManager" on the current page.shutdownTransferManager(transferManager);
// Before using the advanced API, ensure that the process contains a TransferManager instance. If such an instance does not exist, create one.// For the detailed code, see "Advanced API -> Sample Code: Creating TransferManager" in "Uploading Objects".TransferManager transferManager = createTransferManager();// Enter the bucket name in the format of `BucketName-APPID`.String bucketName = "examplebucket-1250000000";// Object key, the unique ID of an object in a bucket. For more information, please see [Object Key](https://www.tencentcloud.com/document/product/436/13324).String key = "exampleobject";// Local file pathString localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);String base64EncodedKey = "MDEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkNERUY=";// "sseCustomerKey" is a Base64-encoded key.SSECustomerKey sseCustomerKey = new SSECustomerKey(base64EncodedKey);putObjectRequest.setSSECustomerKey(sseCustomerKey);// For server-side encryption scenarios, the returned ETag is not the MD5 value of the file. Therefore, MD5 verification on the client should be removed.// You can obtain the CRC64 value for verification as needed.System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// The advanced API will return an asynchronous result `Upload`.// You can call the `waitForUploadResult` method to wait for the upload to complete. If the upload is successful, `UploadResult` is returned. If the upload fails, an exception is returned.Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// After confirming that the process does not use the TransferManager anymore, close it.// For the detailed code, see "Advanced APIs -> Closing TransferManager" on the current page.shutdownTransferManager(transferManager);
// Before using the advanced API, ensure that the process contains a TransferManager instance. If such an instance does not exist, create one.// For the detailed code, see "Advanced API -> Sample Code: Creating TransferManager" in "Uploading Objects".TransferManager transferManager = createTransferManager();// Enter the bucket name in the format of `BucketName-APPID`.String bucketName = "examplebucket-1250000000";// Object key, the unique ID of an object in a bucket. For more information, please see [Object Key](https://www.tencentcloud.com/document/product/436/13324).String key = "exampleobject";// Local file pathString localFilePath = "/path/to/localFile";File localFile = new File(localFilePath);PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);// KMS CMKString kmsKeyId = "your-kms-key-id";String encryptionContext = Base64.encodeAsString("{\\"Ssekmstest\\":\\"Ssekmstest\\"}".getBytes());SSECOSKeyManagementParams ssecosKeyManagementParams = new SSECOSKeyManagementParams(kmsKeyId, encryptionContext);putObjectRequest.setSSECOSKeyManagementParams(ssecosKeyManagementParams);// For server-side encryption scenarios, the returned ETag is not the MD5 value of the file. Therefore, MD5 verification on the client should be removed.// You can obtain the CRC64 value for verification as needed.System.setProperty(SkipMd5CheckStrategy.DISABLE_PUT_OBJECT_MD5_VALIDATION_PROPERTY, "true");try {// The advanced API will return an asynchronous result `Upload`.// You can call the `waitForUploadResult` method to wait for the upload to complete. If the upload is successful, `UploadResult` is returned. If the upload fails, an exception is returned.Upload upload = transferManager.upload(putObjectRequest);UploadResult uploadResult = upload.waitForUploadResult();} catch (CosServiceException e) {e.printStackTrace();} catch (CosClientException e) {e.printStackTrace();} catch (InterruptedException e) {e.printStackTrace();}// After confirming that the process does not use the TransferManager anymore, close it.// For the detailed code, see "Advanced APIs -> Closing TransferManager" on the current page.shutdownTransferManager(transferManager);
Apakah halaman ini membantu?