Search in sources :

Example 1 with ObjectMetadata

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

the class COSUnderFileSystem method createEmptyObject.

@Override
public boolean createEmptyObject(String key) {
    try {
        ObjectMetadata objMeta = new ObjectMetadata();
        objMeta.setContentLength(0);
        mClient.putObject(mBucketNameInternal, key, new ByteArrayInputStream(new byte[0]), objMeta);
        return true;
    } catch (CosClientException e) {
        LOG.error("Failed to create object: {}", key, e);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 2 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project jeesuite-libs by vakinge.

the class QcloudProvider method getObjectMetadata.

@Override
public CObjectMetadata getObjectMetadata(String bucketName, String fileKey) {
    try {
        String _bucketName = buildBucketName(bucketName);
        String _fileKey = resolveFileKey(bucketName, fileKey);
        ObjectMetadata metadata = cosclient.getObjectMetadata(_bucketName, _fileKey);
        CObjectMetadata objectMetadata = new CObjectMetadata();
        objectMetadata.setCreateTime(metadata.getLastModified());
        objectMetadata.setMimeType(metadata.getContentType());
        objectMetadata.setFilesize(metadata.getContentLength());
        objectMetadata.setHash(metadata.getContentMD5());
        objectMetadata.setExpirationTime(metadata.getExpirationTime());
        objectMetadata.setCustomMetadatas(metadata.getUserMetadata());
        return objectMetadata;
    } catch (Exception e) {
        throw new JeesuiteBaseException(500, buildMessage(bucketName, e));
    }
}
Also used : CObjectMetadata(com.jeesuite.cos.CObjectMetadata) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) CObjectMetadata(com.jeesuite.cos.CObjectMetadata) CosServiceException(com.qcloud.cos.exception.CosServiceException) IOException(java.io.IOException) JeesuiteBaseException(com.jeesuite.common.JeesuiteBaseException)

Example 3 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata in project accright-blog by Accright.

the class COSApi method uploadFileToCOS.

/**
 * 使用文件流上传文件至COS
 * @param file
 * @return
 */
public String uploadFileToCOS(MultipartFile file) throws IOException {
    // 获取上传文件的名称用于获取后缀
    String fileName = file.getOriginalFilename();
    // 获取文件的后缀
    String prex = fileName.substring(fileName.lastIndexOf("."));
    // 设置文件的key
    String key = System.currentTimeMillis() + prex;
    // 获取文件输入流
    InputStream inputStream = file.getInputStream();
    // 获取文件元信息
    ObjectMetadata objectMetadata = new ObjectMetadata();
    // 设置输入流长度为 500
    objectMetadata.setContentLength(file.getSize());
    // 设置 Content type, 默认是 application/octet-stream
    objectMetadata.setContentType(file.getContentType());
    PutObjectResult putObjectResult = cosclient.putObject(bucketName, key, inputStream, objectMetadata);
    // 关闭输入流
    inputStream.close();
    // 设置过期时间   ---可以返回带有过期时间的URL
    // Date expiration = new Date(new Date().getTime() + 30L * 60L * 1000L);
    // URL urlx = cosclient.generatePresignedUrl(bucketName, key, expiration,HttpMethodName.GET);
    // return urlx.toString();
    // 关闭客户端
    cosclient.shutdown();
    // 返回URL
    return BUCKET_URL + key;
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) InputStream(java.io.InputStream) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 4 with ObjectMetadata

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

the class CopyFileDemo method copyWithNewMetaDataDemo.

public static void copyWithNewMetaDataDemo() {
    // 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);
    ExecutorService threadPool = Executors.newFixedThreadPool(32);
    // 传入一个threadpool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池。
    TransferManager transferManager = new TransferManager(cosclient, threadPool);
    // 要拷贝的bucket region, 支持跨园区拷贝
    Region srcBucketRegion = new Region("ap-shanghai");
    // 源bucket, bucket名需包含appid
    String srcBucketName = "srcBucket-1251668577";
    // 要拷贝的源文件
    String srcKey = "aaa/bbb.txt";
    // 目的bucket, bucket名需包含appid
    String destBucketName = "destBucket-1251668577";
    // 要拷贝的目的文件
    String destKey = "ccc/ddd.txt";
    ClientConfig srcClientConfig = new ClientConfig(srcBucketRegion);
    COSClient srcCosclient = new COSClient(cred, srcClientConfig);
    GetObjectRequest getReq = new GetObjectRequest(srcBucketName, srcKey);
    File srcFile = new File("srcFile");
    srcCosclient.getObject(getReq, srcFile);
    String srcMD5 = "";
    try {
        srcMD5 = Md5Utils.md5Hex(srcFile);
    } catch (Exception e) {
        e.printStackTrace();
    }
    ObjectMetadata destMeta = new ObjectMetadata();
    destMeta.addUserMetadata("md5", srcMD5);
    CopyObjectRequest copyObjectRequest = new CopyObjectRequest(srcBucketRegion, srcBucketName, srcKey, destBucketName, destKey);
    copyObjectRequest.setNewObjectMetadata(destMeta);
    try {
        CopyObjectResult copyObjectResult = cosclient.copyObject(copyObjectRequest);
        System.out.print(copyObjectResult.getRequestId());
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    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) CosClientException(com.qcloud.cos.exception.CosClientException) CosServiceException(com.qcloud.cos.exception.CosServiceException) COSClient(com.qcloud.cos.COSClient) CopyObjectRequest(com.qcloud.cos.model.CopyObjectRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) CopyObjectResult(com.qcloud.cos.model.CopyObjectResult) 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) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 5 with ObjectMetadata

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

the class GetObjectDemo method GetObjectToFileDemo.

public static void GetObjectToFileDemo() {
    // 初始化用户身份信息(secretId, secretKey)
    COSCredentials cred = new BasicCOSCredentials("AKIDXXXXXXXX", "1A2Z3YYYYYYYYYY");
    // 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
    ClientConfig clientConfig = new ClientConfig(new Region("ap-guangzhou"));
    // 生成cos客户端
    COSClient cosclient = new COSClient(cred, clientConfig);
    String key = "test/my_test.json";
    String bucketName = "mybucket-1251668577";
    boolean useTrafficLimit = false;
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    if (useTrafficLimit) {
        getObjectRequest.setTrafficLimit(8 * 1024 * 1024);
    }
    File localFile = new File("my_test.json");
    ObjectMetadata objectMetadata = cosclient.getObject(getObjectRequest, localFile);
    System.out.println(objectMetadata.getContentLength());
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Aggregations

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