use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class CopyCallable method initiateMultipartUpload.
/**
* Initiates a multipart upload and returns the upload id
*/
private String initiateMultipartUpload(CopyObjectRequest origReq) {
InitiateMultipartUploadRequest req = new InitiateMultipartUploadRequest(origReq.getDestinationBucketName(), origReq.getDestinationKey()).withCannedACL(origReq.getCannedAccessControlList()).withAccessControlList(origReq.getAccessControlList()).withStorageClass(origReq.getStorageClass()).withGeneralProgressListener(origReq.getGeneralProgressListener());
ObjectMetadata newObjectMetadata = origReq.getNewObjectMetadata();
if (newObjectMetadata == null) {
newObjectMetadata = new ObjectMetadata();
}
if (newObjectMetadata.getContentType() == null) {
newObjectMetadata.setContentType(metadata.getContentType());
}
req.setObjectMetadata(newObjectMetadata);
TransferManagerUtils.populateEndpointAddr(origReq, req);
String uploadId = cos.initiateMultipartUpload(req).getUploadId();
log.debug("Initiated new multipart upload: " + uploadId);
return uploadId;
}
use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class SimpleUploadFileDemo method SimpleUploadFileFromStream.
// 从输入流进行读取并上传到COS
public static void SimpleUploadFileFromStream() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
String key = "aaa/bbb.jpg";
File localFile = new File("src/test/resources/len10M.txt");
InputStream input = new ByteArrayInputStream(new byte[10]);
ObjectMetadata objectMetadata = new ObjectMetadata();
// 从输入流上传必须制定content length, 否则http客户端可能会缓存所有数据,存在内存OOM的情况
objectMetadata.setContentLength(10);
// 默认下载时根据cos路径key的后缀返回响应的contenttype, 上传时设置contenttype会覆盖默认值
objectMetadata.setContentType("image/jpeg");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, input, objectMetadata);
// 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
putObjectRequest.setStorageClass(StorageClass.Standard_IA);
try {
PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
// putobjectResult会返回文件的etag
String etag = putObjectResult.getETag();
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
}
// 关闭客户端
cosclient.shutdown();
}
use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class SymmetricKeyEncryptionClientDemo method getObjectDemo.
static void getObjectDemo() {
// 下载文件
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
File downloadFile = new File("downSym.txt");
ObjectMetadata objectMetadata = cosClient.getObject(getObjectRequest, downloadFile);
System.out.println(objectMetadata.getRequestId());
}
use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class TemporyTokenDemo method UseTemporyTokenUploadAndDownload.
// 使用临时秘钥进行上传和下载
public static void UseTemporyTokenUploadAndDownload() {
// 使用云api秘钥,可以获取一个临时secret id,secret key和session token,
BasicSessionCredentials cred = getSessionCredential();
// 设置区域, 这里设置为北京一区
ClientConfig clientConfig = new ClientConfig(new Region("ap-beijing-1"));
// 生成cos客户端对象
COSClient cosClient = new COSClient(cred, clientConfig);
// 上传的bucket名字
String bucketName = "rabbitliutj-1000000";
// 上传object, 建议20M以下的文件使用该接口
File localFile = new File("src/test/resources/len5M.txt");
String key = "upload_single_demo5M.txt";
// 上传
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
ObjectMetadata objectMetadata = new ObjectMetadata();
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
System.out.println(putObjectResult.getMetadata());
// 下载
File downFile = new File("src/test/resources/len5M_down.txt");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ObjectMetadata downObjectMeta = cosClient.getObject(getObjectRequest, downFile);
// 关闭客户端(关闭后台线程)
cosClient.shutdown();
}
use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.
the class TransferManagerDemo method multipartUploadWithMetaData.
public static void multipartUploadWithMetaData() {
// 1 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
// 2 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region("ap-shanghai"));
// 3 生成cos客户端
COSClient cosclient = new COSClient(cred, clientConfig);
// bucket名需包含appid
String bucketName = "mybucket-1251668577";
ExecutorService threadPool = Executors.newFixedThreadPool(2);
// 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
TransferManager transferManager = new TransferManager(cosclient, threadPool);
String key = "aaa/bbb.txt";
File localFile = new File("src/test/resources/len20M.txt");
ObjectMetadata objectMetadata = new ObjectMetadata();
Map<String, String> userMeta = new HashMap<String, String>();
userMeta.put("usermeta", "hello-mult");
objectMetadata.setUserMetadata(userMeta);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
putObjectRequest.withMetadata(objectMetadata);
try {
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
long startTime = System.currentTimeMillis();
Upload upload = transferManager.upload(putObjectRequest);
// showTransferProgress(upload);
UploadResult uploadResult = upload.waitForUploadResult();
long endTime = System.currentTimeMillis();
System.out.println("used time: " + (endTime - startTime) / 1000);
System.out.println(uploadResult.getETag());
System.out.println(uploadResult.getCrc64Ecma());
} catch (CosServiceException e) {
e.printStackTrace();
} catch (CosClientException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
transferManager.shutdownNow();
cosclient.shutdown();
}
Aggregations