use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class CAMRoleDemo method SimpleUploadFileFromLocal.
public static void SimpleUploadFileFromLocal() {
InstanceMetadataCredentialsEndpointProvider endpointProvider = new InstanceMetadataCredentialsEndpointProvider(InstanceMetadataCredentialsEndpointProvider.Instance.CVM);
InstanceCredentialsFetcher instanceCredentialsFetcher = new InstanceCredentialsFetcher(endpointProvider);
COSCredentialsProvider cosCredentialsProvider = new InstanceCredentialsProvider(instanceCredentialsFetcher);
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
COSClient cosClient = new COSClient(cosCredentialsProvider, clientConfig);
String bucketName = "3399demo-125xxxxxxxx";
String key = "test/demo.txt";
File localFile = new File("test");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.setStorageClass(StorageClass.Standard);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
cosClient.shutdown();
}
use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class PutObjectDemo method putObjectDemo.
static void putObjectDemo() {
String bucketName = "examplebucket-1251668577";
String key = "abc/abc.txt";
String localPath = "abc.txt";
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setHeader("expires", new Date(1660000000000L));
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, new File(localPath));
putObjectRequest.withMetadata(objectMetadata);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
COSObject cosObject = cosClient.getObject(getObjectRequest);
System.out.println(cosObject.getObjectMetadata().getRequestId());
cosClient.shutdown();
}
use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class AsymmetricKeyEncryptionClientDemo method putObjectDemo.
static void putObjectDemo() {
// 上传文件
// 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
}
use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class KMSEncryptionClientDemo method putObjectDemo.
static void putObjectDemo() {
// 上传文件
// 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getRequestId());
}
use of com.qcloud.cos.model.PutObjectRequest in project cos-java-sdk-v5 by tencentyun.
the class KmsUploadDemo method SimpleUploadWithKmsMeta.
public static void SimpleUploadWithKmsMeta() {
COSCredentials cred = new BasicCOSCredentials("SECRET_ID", "SECRET_KEY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
// 设置使用https请求
clientConfig.setHttpProtocol(HttpProtocol.https);
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.txt";
File localFile = new File("/test.log");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
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 {
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// putobjectResult会返回文件的etag
String etag = putObjectResult.getETag();
String crc64 = putObjectResult.getCrc64Ecma();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
Aggregations