Search in sources :

Example 11 with PutObjectRequest

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

the class TransferManager method uploadFileList.

/**
 * Uploads all specified files to the bucket named, constructing relative keys depending on the
 * commonParentDirectory given.
 * <p>
 * COS will overwrite any existing objects that happen to have the same key, just as when
 * uploading individual files, so use with caution.
 * </p>
 *
 * @param bucketName The name of the bucket to upload objects to.
 * @param virtualDirectoryKeyPrefix The key prefix of the virtual directory to upload to. Use
 *        the null or empty string to upload files to the root of the bucket.
 * @param directory The common parent directory of files to upload. The keys of the files in the
 *        list of files are constructed relative to this directory and the
 *        virtualDirectoryKeyPrefix.
 * @param files A list of files to upload. The keys of the files are calculated relative to the
 *        common parent directory and the virtualDirectoryKeyPrefix.
 * @param metadataProvider A callback of type <code>ObjectMetadataProvider</code> which is used
 *        to provide metadata for each file being uploaded.
 */
public MultipleFileUpload uploadFileList(String bucketName, String virtualDirectoryKeyPrefix, File directory, List<File> files, ObjectMetadataProvider metadataProvider) {
    if (directory == null || !directory.exists() || !directory.isDirectory()) {
        throw new IllegalArgumentException("Must provide a common base directory for uploaded files");
    }
    if (virtualDirectoryKeyPrefix == null || virtualDirectoryKeyPrefix.length() == 0) {
        virtualDirectoryKeyPrefix = "";
    } else if (!virtualDirectoryKeyPrefix.endsWith("/")) {
        virtualDirectoryKeyPrefix = virtualDirectoryKeyPrefix + "/";
    }
    /* This is the hook for adding additional progress listeners */
    ProgressListenerChain additionalListeners = new ProgressListenerChain();
    TransferProgress progress = new TransferProgress();
    /*
         * Bind additional progress listeners to this MultipleFileTransferProgressUpdatingListener
         * to receive ByteTransferred events from each single-file upload implementation.
         */
    ProgressListener listener = new MultipleFileTransferProgressUpdatingListener(progress, additionalListeners);
    List<UploadImpl> uploads = new LinkedList<UploadImpl>();
    MultipleFileUploadImpl multipleFileUpload = new MultipleFileUploadImpl("Uploading etc", progress, additionalListeners, virtualDirectoryKeyPrefix, bucketName, uploads);
    multipleFileUpload.setMonitor(new MultipleFileTransferMonitor(multipleFileUpload, uploads));
    final CountDownLatch latch = new CountDownLatch(1);
    MultipleFileTransferStateChangeListener transferListener = new MultipleFileTransferStateChangeListener(latch, multipleFileUpload);
    if (files == null || files.isEmpty()) {
        multipleFileUpload.setState(TransferState.Completed);
    } else {
        /*
             * If the absolute path for the common/base directory does NOT end in a separator (which
             * is the case for anything but root directories), then we know there's still a
             * separator between the base directory and the rest of the file's path, so we increment
             * the starting position by one.
             */
        int startingPosition = directory.getAbsolutePath().length();
        if (!(directory.getAbsolutePath().endsWith(File.separator)))
            startingPosition++;
        long totalSize = 0;
        for (File f : files) {
            // Check, if file, since only files can be uploaded.
            if (f.isFile()) {
                totalSize += f.length();
                String key = f.getAbsolutePath().substring(startingPosition).replaceAll("\\\\", "/");
                ObjectMetadata metadata = new ObjectMetadata();
                // for each file being uploaded.
                if (metadataProvider != null) {
                    metadataProvider.provideObjectMetadata(f, metadata);
                }
                // All the single-file uploads share the same
                // MultipleFileTransferProgressUpdatingListener and
                // MultipleFileTransferStateChangeListener
                uploads.add((UploadImpl) doUpload(new PutObjectRequest(bucketName, virtualDirectoryKeyPrefix + key, f).withMetadata(metadata).<PutObjectRequest>withGeneralProgressListener(listener), transferListener, null, null));
            }
        }
        progress.setTotalBytesToTransfer(totalSize);
    }
    // Notify all state changes waiting for the uploads to all be queued
    // to wake up and continue
    latch.countDown();
    return multipleFileUpload;
}
Also used : MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MultipleFileTransferStateChangeListener(com.qcloud.cos.event.MultipleFileTransferStateChangeListener) ProgressListener(com.qcloud.cos.event.ProgressListener) COSProgressListener(com.qcloud.cos.event.COSProgressListener) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 12 with PutObjectRequest

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

the class TransferManager method resumeUpload.

// 
// /**
// * <p>
// * Schedules a new transfer to copy data from one Qcloud COS location to another Qcloud COS
// * location. This method is non-blocking and returns immediately (i.e. before the copy has
// * finished).
// * </p>
// * <p>
// * <code>TransferManager</code> doesn't support copying of encrypted objects whose encryption
// * materials is stored in instruction file.
// * </p>
// * <p>
// * Use the returned <code>Copy</code> object to check if the copy is complete.
// * </p>
// * <p>
// * If resources are available, the copy request will begin immediately. Otherwise, the copy is
// * scheduled and started as soon as resources become available.
// * </p>
// *
// * @param sourceBucketName The name of the bucket from where the object is to be copied.
// * @param sourceKey The name of the Qcloud COS object.
// * @param destinationBucketName The name of the bucket to where the Qcloud COS object has to
// be
// * copied.
// * @param destinationKey The name of the object in the destination bucket.
// *
// * @return A new <code>Copy</code> object to use to check the state of the copy request being
// * processed.
// *
// * @throws CosClientException If any errors are encountered in the client while making the
// * request or handling the response.
// * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
// * request.
// */
// 
// public Copy copy(String sourceBucketName, String sourceKey, String destinationBucketName,
// String destinationKey) throws CosServiceException, CosClientException {
// return copy(new CopyObjectRequest(sourceBucketName, sourceKey, destinationBucketName,
// destinationKey));
// }
// 
// /**
// * <p>
// * Schedules a new transfer to copy data from one Qcloud COS location to another Qcloud COS
// * location. This method is non-blocking and returns immediately (i.e. before the copy has
// * finished).
// * </p>
// * <p>
// * <code>TransferManager</code> doesn't support copying of encrypted objects whose encryption
// * materials is stored i instruction file.
// * </p>
// * <p>
// * Use the returned <code>Copy</code> object to check if the copy is complete.
// * </p>
// * <p>
// * If resources are available, the copy request will begin immediately. Otherwise, the copy is
// * scheduled and started as soon as resources become available.
// * </p>
// *
// * @param copyObjectRequest The request containing all the parameters for the copy.
// *
// * @return A new <code>Copy</code> object to use to check the state of the copy request being
// * processed.
// *
// * @throws CosClientException If any errors are encountered in the client while making the
// * request or handling the response.
// * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
// * request.
// */
// public Copy copy(final CopyObjectRequest copyObjectRequest) {
// return copy(copyObjectRequest, null);
// }
// 
// /**
// * <p>
// * Schedules a new transfer to copy data from one Qcloud COS location to another Qcloud COS
// * location. This method is non-blocking and returns immediately (i.e. before the copy has
// * finished).
// * </p>
// * <p>
// * <code>TransferManager</code> doesn't support copying of encrypted objects whose encryption
// * materials is stored in instruction file.
// * </p>
// * <p>
// * Use the returned <code>Copy</code> object to check if the copy is complete.
// * </p>
// * <p>
// * If resources are available, the copy request will begin immediately. Otherwise, the copy is
// * scheduled and started as soon as resources become available.
// * </p>
// *
// * @param copyObjectRequest The request containing all the parameters for the copy.
// * @param stateChangeListener The transfer state change listener to monitor the copy request
// * @return A new <code>Copy</code> object to use to check the state of the copy request being
// * processed.
// *
// * @throws CosClientException If any errors are encountered in the client while making the
// * request or handling the response.
// * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
// * request.
// */
// 
// public Copy copy(final CopyObjectRequest copyObjectRequest,
// final TransferStateChangeListener stateChangeListener)
// throws CosServiceException, CosClientException {
// 
// appendSingleObjectUserAgent(copyObjectRequest);
// 
// assertParameterNotNull(copyObjectRequest.getSourceBucketName(),
// "The source bucket name must be specified when a copy request is initiated.");
// assertParameterNotNull(copyObjectRequest.getSourceKey(),
// "The source object key must be specified when a copy request is initiated.");
// assertParameterNotNull(copyObjectRequest.getDestinationBucketName(),
// "The destination bucket name must be specified when a copy request is initiated.");
// assertParameterNotNull(copyObjectRequest.getDestinationKey(),
// "The destination object key must be specified when a copy request is initiated.");
// 
// String description = "Copying object from " + copyObjectRequest.getSourceBucketName() + "/"
// + copyObjectRequest.getSourceKey() + " to "
// + copyObjectRequest.getDestinationBucketName() + "/"
// + copyObjectRequest.getDestinationKey();
// 
// GetObjectMetadataRequest getObjectMetadataRequest =
// new GetObjectMetadataRequest(copyObjectRequest.getSourceBucketName(),
// copyObjectRequest.getSourceKey())
// .withSSECustomerKey(copyObjectRequest.getSourceSSECustomerKey());
// 
// ObjectMetadata metadata = cos.getObjectMetadata(getObjectMetadataRequest);
// 
// TransferProgress transferProgress = new TransferProgress();
// transferProgress.setTotalBytesToTransfer(metadata.getContentLength());
// 
// ProgressListenerChain listenerChain =
// new ProgressListenerChain(new TransferProgressUpdatingListener(transferProgress));
// CopyImpl copy =
// new CopyImpl(description, transferProgress, listenerChain, stateChangeListener);
// CopyCallable copyCallable = new CopyCallable(this, threadPool, copy, copyObjectRequest,
// metadata, listenerChain);
// CopyMonitor watcher = CopyMonitor.create(this, copy, threadPool, copyCallable,
// copyObjectRequest, listenerChain);
// watcher.setTimedThreadPool(timedThreadPool);
// copy.setMonitor(watcher);
// return copy;
// }
/**
 * Resumes an upload operation. This upload operation uses the same configuration
 * {@link TransferManagerConfiguration} as the original upload. Any data already uploaded will
 * be skipped, and only the remaining will be uploaded to Qcloud COS.
 *
 * @param persistableUpload the upload to resume.
 * @return A new <code>Upload</code> object to use to check the state of the upload, listen for
 *         progress notifications, and otherwise manage the upload.
 *
 * @throws CosClientException If any errors are encountered in the client while making the
 *         request or handling the response.
 * @throws CosServiceException If any errors occurred in Qcloud COS while processing the
 *         request.
 */
public Upload resumeUpload(PersistableUpload persistableUpload) {
    assertParameterNotNull(persistableUpload, "PauseUpload is mandatory to resume a upload.");
    configuration.setMinimumUploadPartSize(persistableUpload.getPartSize());
    configuration.setMultipartUploadThreshold(persistableUpload.getMutlipartUploadThreshold());
    return doUpload(new PutObjectRequest(persistableUpload.getBucketName(), persistableUpload.getKey(), new File(persistableUpload.getFile())), null, null, persistableUpload);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 13 with PutObjectRequest

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

the class SimpleUploadFileDemo method SimpleUploadFileFromLocal.

// 将本地文件上传到COS
public static void SimpleUploadFileFromLocal(boolean useTrafficLimit) {
    // 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.txt";
    File localFile = new File("src/test/resources/len10M.txt");
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    if (useTrafficLimit) {
        // 限流使用的单位是bit/s, 这里测试1MB/s的上传带宽限制
        putObjectRequest.setTrafficLimit(8 * 1024 * 1024);
    }
    // 设置存储类型, 默认是标准(Standard), 低频(standard_ia)
    putObjectRequest.setStorageClass(StorageClass.Standard_IA);
    try {
        PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
        // putobjectResult会返回文件的etag
        String etag = putObjectResult.getETag();
        String crc64 = putObjectResult.getCrc64Ecma();
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // 关闭客户端
    cosclient.shutdown();
}
Also used : COSClient(com.qcloud.cos.COSClient) COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) CosServiceException(com.qcloud.cos.exception.CosServiceException) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) CosClientException(com.qcloud.cos.exception.CosClientException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 14 with PutObjectRequest

use of com.qcloud.cos.model.PutObjectRequest 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();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) PutObjectResult(com.qcloud.cos.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CosClientException(com.qcloud.cos.exception.CosClientException) COSClient(com.qcloud.cos.COSClient) CosServiceException(com.qcloud.cos.exception.CosServiceException) ByteArrayInputStream(java.io.ByteArrayInputStream) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig) File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Example 15 with PutObjectRequest

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

the class SymmetricKeyEncryptionClientDemo method putObjectDemo.

static void putObjectDemo() {
    // 上传文件
    // 这里给出putObject的示例, 对于高级API上传,只用在生成TransferManager时传入COSEncryptionClient对象即可
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
    PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
    System.out.println(putObjectResult.getRequestId());
}
Also used : PutObjectResult(com.qcloud.cos.model.PutObjectResult) PutObjectRequest(com.qcloud.cos.model.PutObjectRequest)

Aggregations

PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)45 File (java.io.File)30 PutObjectResult (com.qcloud.cos.model.PutObjectResult)25 CosServiceException (com.qcloud.cos.exception.CosServiceException)17 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)17 CosClientException (com.qcloud.cos.exception.CosClientException)15 COSClient (com.qcloud.cos.COSClient)13 ClientConfig (com.qcloud.cos.ClientConfig)13 Region (com.qcloud.cos.region.Region)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Upload (com.qcloud.cos.transfer.Upload)12 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)10 UploadResult (com.qcloud.cos.model.UploadResult)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 LinkedList (java.util.LinkedList)8 Test (org.junit.Test)8 CIObject (com.qcloud.cos.model.ciModel.persistence.CIObject)7 CIUploadResult (com.qcloud.cos.model.ciModel.persistence.CIUploadResult)7 PicOperations (com.qcloud.cos.model.ciModel.persistence.PicOperations)7