Search in sources :

Example 6 with Upload

use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.

the class QRCodeDemo method identifyQrCodeWithTransferManager.

public static void identifyQrCodeWithTransferManager(TransferManager transferManager) throws InterruptedException {
    // bucket名需包含appid
    // api 请参考 https://cloud.tencent.com/document/product/436/54070
    String bucketName = "examplebucket-1250000000";
    String key = "qrcode.png";
    File localFile = new File("E://qrcode.png");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PicOperations picOperations = new PicOperations();
    picOperations.setIsPicInfo(1);
    List<PicOperations.Rule> ruleList = new LinkedList<>();
    PicOperations.Rule rule1 = new PicOperations.Rule();
    rule1.setBucket(bucketName);
    rule1.setFileId("qrcode-1.png");
    rule1.setRule("QRcode/cover/1");
    ruleList.add(rule1);
    picOperations.setRules(ruleList);
    putObjectRequest.setPicOperations(picOperations);
    Upload upload = transferManager.upload(putObjectRequest);
    UploadResult uploadResult = upload.waitForUploadResult();
    CIUploadResult ciUploadResult = uploadResult.getCiUploadResult();
    System.out.println(uploadResult.getRequestId());
    System.out.println(ciUploadResult.getOriginalInfo().getEtag());
    for (CIObject ciObject : ciUploadResult.getProcessResults().getObjectList()) {
        System.out.println(ciObject.getLocation());
    }
}
Also used : PicOperations(com.qcloud.cos.model.ciModel.persistence.PicOperations) Upload(com.qcloud.cos.transfer.Upload) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) CIUploadResult(com.qcloud.cos.model.ciModel.persistence.CIUploadResult) InitiateMultipartUploadResult(com.qcloud.cos.model.InitiateMultipartUploadResult) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) LinkedList(java.util.LinkedList) CIObject(com.qcloud.cos.model.ciModel.persistence.CIObject)

Example 7 with Upload

use of com.qcloud.cos.transfer.Upload in project jeesuite-libs by vakinge.

the class QcloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    PutObjectRequest request;
    String fileKey = object.getFileKey();
    String bucketName = buildBucketName(object.getBucketName());
    if (object.getFile() != null) {
        request = new PutObjectRequest(bucketName, fileKey, object.getFile());
    } else if (object.getBytes() != null) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
    } else if (object.getInputStream() != null) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
    } else {
        throw new IllegalArgumentException("upload object is NULL");
    }
    try {
        if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
            Upload upload = transferManager.upload(request);
            com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
        } else {
            PutObjectResult result = cosclient.putObject(request);
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) Upload(com.qcloud.cos.transfer.Upload) CosServiceException(com.qcloud.cos.exception.CosServiceException) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) IOException(java.io.IOException) CUploadResult(com.mendmix.cos.CUploadResult) JeesuiteBaseException(com.mendmix.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) CObjectMetadata(com.mendmix.cos.CObjectMetadata) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 8 with Upload

use of com.qcloud.cos.transfer.Upload in project jeesuite-libs by vakinge.

the class QcloudProvider method upload.

@Override
public CUploadResult upload(CUploadObject object) {
    PutObjectRequest request;
    String fileKey = object.getFileKey();
    String bucketName = buildBucketName(object.getBucketName());
    if (object.getFile() != null) {
        request = new PutObjectRequest(bucketName, fileKey, object.getFile());
    } else if (object.getBytes() != null) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(object.getBytes());
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, inputStream, objectMetadata);
    } else if (object.getInputStream() != null) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(object.getFileSize());
        request = new PutObjectRequest(bucketName, fileKey, object.getInputStream(), objectMetadata);
    } else {
        throw new IllegalArgumentException("upload object is NULL");
    }
    try {
        if (object.getFileSize() > conf.getMaxAllowdSingleFileSize()) {
            Upload upload = transferManager.upload(request);
            com.qcloud.cos.model.UploadResult result = upload.waitForUploadResult();
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getCrc64Ecma());
        } else {
            PutObjectResult result = cosclient.putObject(request);
            return new CUploadResult(fileKey, getFullPath(object.getBucketName(), fileKey), result.getContentMd5());
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) Upload(com.qcloud.cos.transfer.Upload) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) CUploadResult(com.jeesuite.cos.CUploadResult) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CObjectMetadata(com.jeesuite.cos.CObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 9 with Upload

use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.

the class KMSEncryptionClientDemo method transferManagerDemo.

static void transferManagerDemo() {
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosClient, threadPool);
    TransferManagerConfiguration transferManagerConfiguration = new TransferManagerConfiguration();
    transferManagerConfiguration.setMultipartUploadThreshold(1024 * 1024);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, bigLocalFile);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常.
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        System.out.println(uploadResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    try {
        // 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待download结束, 成功返回DownloadResult, 失败抛出异常.
        Download download = transferManager.download(getObjectRequest, new File("downLen10m.txt"));
        download.waitForCompletion();
        System.out.println(download.getObjectMetadata().getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    transferManager.shutdownNow();
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) TransferManagerConfiguration(com.qcloud.cos.transfer.TransferManagerConfiguration) CosServiceException(com.qcloud.cos.exception.CosServiceException) CosClientException(com.qcloud.cos.exception.CosClientException) ExecutorService(java.util.concurrent.ExecutorService) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 10 with Upload

use of com.qcloud.cos.transfer.Upload in project cos-java-sdk-v5 by tencentyun.

the class AbstractCOSEncryptionClientTest method testTransferManagerUploadDownBigFile.

@Test
public void testTransferManagerUploadDownBigFile() throws IOException, CosServiceException, CosClientException, InterruptedException {
    if (!judgeUserInfoValid()) {
        return;
    }
    TransferManager transferManager = new TransferManager(cosclient);
    File localFile = buildTestFile(1024 * 1024 * 10L);
    File downFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    String destKey = key + ".copy";
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, localFile);
        ObjectMetadata objectMetadata = new ObjectMetadata();
        putObjectRequest.setMetadata(objectMetadata);
        Upload upload = transferManager.upload(putObjectRequest);
        UploadResult uploadResult = upload.waitForUploadResult();
        assertTrue(uploadResult.getETag().contains("-"));
        assertNotNull(uploadResult.getRequestId());
        assertNotNull(uploadResult.getDateStr());
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);
        Download download = transferManager.download(getObjectRequest, downFile);
        download.waitForCompletion();
        // check file
        assertEquals(Md5Utils.md5Hex(localFile), Md5Utils.md5Hex(downFile));
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(bucket, key, bucket, destKey);
        Copy copy = transferManager.copy(copyObjectRequest, cosclient, null);
        copy.waitForCompletion();
    } finally {
        // delete file on cos
        clearObject(key);
        // delete file on cos
        clearObject(destKey);
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downFile.exists()) {
            assertTrue(downFile.delete());
        }
    }
}
Also used : TransferManager(com.qcloud.cos.transfer.TransferManager) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) Copy(com.qcloud.cos.transfer.Copy) Upload(com.qcloud.cos.transfer.Upload) UploadResult(com.qcloud.cos.model.UploadResult) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) Download(com.qcloud.cos.transfer.Download) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest) Test(org.junit.Test)

Aggregations

Upload (com.qcloud.cos.transfer.Upload)13 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)12 UploadResult (com.qcloud.cos.model.UploadResult)10 File (java.io.File)10 TransferManager (com.qcloud.cos.transfer.TransferManager)7 CosServiceException (com.qcloud.cos.exception.CosServiceException)6 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)5 MultipleFileUpload (com.qcloud.cos.transfer.MultipleFileUpload)5 CosClientException (com.qcloud.cos.exception.CosClientException)4 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)4 Download (com.qcloud.cos.transfer.Download)4 ExecutorService (java.util.concurrent.ExecutorService)4 COSClient (com.qcloud.cos.COSClient)3 ClientConfig (com.qcloud.cos.ClientConfig)3 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)3 COSCredentials (com.qcloud.cos.auth.COSCredentials)3 CompleteMultipartUploadResult (com.qcloud.cos.model.CompleteMultipartUploadResult)3 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)3 InitiateMultipartUploadResult (com.qcloud.cos.model.InitiateMultipartUploadResult)3 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)3