Search in sources :

Example 61 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project cos-java-sdk-v5 by tencentyun.

the class TransferManagerDemo method copyFileSetMetadata.

public static void copyFileSetMetadata() {
    // 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-guangzhou"));
    // 3 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    ClientConfig srcClientConfig = new ClientConfig(new Region("ap-shanghai"));
    COSClient srcCosclient = new COSClient(cred, srcClientConfig);
    ExecutorService threadPool = Executors.newFixedThreadPool(2);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartCopyThreshold(5 * 1024 * 1024);
    transferManager.setConfiguration(transferManagerConfiguration);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "mysrcbucket-123456789";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "mydestbucekt-123456789";
    // 要拷贝的目的文件
    String destKey = "bbb/ccc.txt";
    ObjectMetadata objectMetadata = new ObjectMetadata();
    Map<String, String> userMeta = new HashMap<String, String>();
    userMeta.put("usermeta", "hello-mult-copy");
    objectMetadata.setUserMetadata(userMeta);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    System.out.println(copyObjectRequest.getDestinationBucketName());
    copyObjectRequest.setNewObjectMetadata(objectMetadata);
    try {
        Copy copy = transferManager.copy(copyObjectRequest, srcCosclient, null);
        // 返回一个异步结果copy, 可同步的调用waitForCopyResult等待copy结束, 成功返回CopyResult, 失败抛出异常.
        // showTransferProgress(copy);
        CopyResult copyResult = copy.waitForCopyResult();
        System.out.println(copyResult.getCrc64Ecma());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
    cosclient.shutdown();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) HashMap(java.util.HashMap) CosClientException(com.qcloud.cos.exception.CosClientException) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) Copy(com.qcloud.cos.transfer.Copy) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CopyResult(com.qcloud.cos.model.CopyResult)

Example 62 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project alluxio by Alluxio.

the class COSOutputStream method close.

/**
 * Closes this output stream. When an output stream is closed, the local temporary file is
 * uploaded to COS Service. Once the file is uploaded, the temporary file is deleted.
 */
@Override
public void close() throws IOException {
    if (mClosed.getAndSet(true)) {
        return;
    }
    mLocalOutputStream.close();
    try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(mFile))) {
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentLength(mFile.length());
        if (mHash != null) {
            byte[] hashBytes = mHash.digest();
            meta.setContentMD5(new String(Base64.encodeBase64(hashBytes)));
        }
        mCosClient.putObject(mBucketName, mKey, in, meta);
    } catch (CosClientException e) {
        LOG.error("Failed to upload {}. ", mKey);
        throw new IOException(e);
    } finally {
        // upload or if the upload failed.
        if (!mFile.delete()) {
            LOG.error("Failed to delete temporary file @ {}", mFile.getPath());
        }
    }
    return;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) IOException(java.io.IOException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) FileInputStream(java.io.FileInputStream)

Example 63 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project litemall by linlinjava.

the class TencentStorage method store.

@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。例如,在对象的访问域名 `bucket1-1250000000.cos.ap-guangzhou.myqcloud.com/doc1/pic1.jpg`
        // 中,对象键为 doc1/pic1.jpg, 详情参考 [对象键](https://cloud.tencent.com/document/product/436/13324)
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        getCOSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
Also used : ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) MalformedURLException(java.net.MalformedURLException)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)63 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)15 COSClient (com.qcloud.cos.COSClient)14 ClientConfig (com.qcloud.cos.ClientConfig)14 Region (com.qcloud.cos.region.Region)14 CosClientException (com.qcloud.cos.exception.CosClientException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 PutObjectResult (com.qcloud.cos.model.PutObjectResult)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)11 Test (org.junit.Test)11 CosServiceException (com.qcloud.cos.exception.CosServiceException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)6 UploadResult (com.qcloud.cos.model.UploadResult)5 TransferManager (com.qcloud.cos.transfer.TransferManager)5 Upload (com.qcloud.cos.transfer.Upload)5