Search in sources :

Example 16 with GetObjectRequest

use of com.qcloud.cos.model.GetObjectRequest 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();
}
Also used : COSClient(com.qcloud.cos.COSClient) BasicSessionCredentials(com.qcloud.cos.auth.BasicSessionCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 17 with GetObjectRequest

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

the class TransferManagerDemo method resumableDownloadFile.

public static void resumableDownloadFile() {
    // 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);
    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    GetObjectRequest getObj = new GetObjectRequest("mybucket-1000000000", "/path/to/key");
    File dstFile = new File("dstFile");
    Download download = transferManager.download(getObj, dstFile, true);
    showTransferProgress(download);
    try {
        download.waitForCompletion();
    } 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) CosClientException(com.qcloud.cos.exception.CosClientException) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) ExecutorService(java.util.concurrent.ExecutorService) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) PersistableDownload(com.qcloud.cos.transfer.PersistableDownload) MultipleFileDownload(com.qcloud.cos.transfer.MultipleFileDownload) Download(com.qcloud.cos.transfer.Download)

Example 18 with GetObjectRequest

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

the class BasicImageProcessing method imageZoomDemo.

public static void imageZoomDemo(COSClient cosClient) {
    String bucketName = "examplebucket-1250000000";
    String key = "qrcode.png";
    GetObjectRequest getObj = new GetObjectRequest(bucketName, key);
    // 宽高缩放50%
    String rule = "imageMogr2/thumbnail/!50p";
    getObj.putCustomQueryParameter(rule, null);
    cosClient.getObject(getObj, new File("qrcode-50p.png"));
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File)

Example 19 with GetObjectRequest

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

the class BasicImageProcessing method imageRotateDemo.

public static void imageRotateDemo(COSClient cosClient) {
    String bucketName = "examplebucket-1250000000";
    String key = "qrcode.png";
    GetObjectRequest getObj = new GetObjectRequest(bucketName, key);
    // 宽高缩放50%
    String rule = "imageMogr2/rotate/90";
    getObj.putCustomQueryParameter(rule, null);
    cosClient.getObject(getObj, new File("qrcode-rotate.png"));
}
Also used : GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File)

Example 20 with GetObjectRequest

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

the class COSCryptoModuleBase method putInstructionFileSecurely.

@Override
public final PutObjectResult putInstructionFileSecurely(PutInstructionFileRequest req) {
    final COSObjectId id = req.getCOSObjectId();
    final GetObjectRequest getreq = new GetObjectRequest(id);
    // Get the object from cos
    final COSObject retrieved = cos.getObject(getreq);
    // We only need the meta-data already retrieved, not the data stream.
    // So close it immediately to prevent resource leakage.
    IOUtils.closeQuietly(retrieved, log);
    if (retrieved == null) {
        throw new IllegalArgumentException("The specified COS object (" + id + ") doesn't exist.");
    }
    COSObjectWrapper wrapped = new COSObjectWrapper(retrieved, id);
    try {
        final ContentCryptoMaterial origCCM = contentCryptoMaterialOf(wrapped);
        securityCheck(origCCM, wrapped);
        // Re-ecnrypt the CEK in a new content crypto material
        final EncryptionMaterials newKEK = req.getEncryptionMaterials();
        final ContentCryptoMaterial newCCM;
        if (newKEK == null) {
            newCCM = origCCM.recreate(req.getMaterialsDescription(), this.kekMaterialsProvider, cryptoScheme, cryptoConfig.getCryptoProvider(), kms, req);
        } else {
            newCCM = origCCM.recreate(newKEK, this.kekMaterialsProvider, cryptoScheme, cryptoConfig.getCryptoProvider(), kms, req);
        }
        PutObjectRequest putInstFileRequest = req.createPutObjectRequest(retrieved);
        // Put the new instruction file into COS
        return cos.putObject(updateInstructionPutRequest(putInstFileRequest, newCCM));
    } catch (RuntimeException ex) {
        // If we're unable to set up the decryption, make sure we close the
        // HTTP connection
        IOUtils.closeQuietly(retrieved, log);
        throw ex;
    } catch (Error error) {
        IOUtils.closeQuietly(retrieved, log);
        throw error;
    }
}
Also used : COSObjectId(com.qcloud.cos.model.COSObjectId) COSObject(com.qcloud.cos.model.COSObject) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) AbstractPutObjectRequest(com.qcloud.cos.model.AbstractPutObjectRequest) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)36 File (java.io.File)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)16 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)14 COSObject (com.qcloud.cos.model.COSObject)12 CosClientException (com.qcloud.cos.exception.CosClientException)10 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)10 Test (org.junit.Test)9 TransferManager (com.qcloud.cos.transfer.TransferManager)8 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)7 COSCredentials (com.qcloud.cos.auth.COSCredentials)7 Download (com.qcloud.cos.transfer.Download)7 COSClient (com.qcloud.cos.COSClient)6 ClientConfig (com.qcloud.cos.ClientConfig)6 Region (com.qcloud.cos.region.Region)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)5 MultipleFileDownload (com.qcloud.cos.transfer.MultipleFileDownload)5 ExecutorService (java.util.concurrent.ExecutorService)5