Search in sources :

Example 1 with MultipleFileTransferStateChangeListener

use of com.qcloud.cos.event.MultipleFileTransferStateChangeListener 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 2 with MultipleFileTransferStateChangeListener

use of com.qcloud.cos.event.MultipleFileTransferStateChangeListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method batchPostImageAuditing.

public MultipleImageAuditingImpl batchPostImageAuditing(List<ImageAuditingRequest> requestList) {
    String description = "Send image auditing jobs in bulk";
    TransferProgress transferProgress = new TransferProgress();
    List<ImageAuditingImpl> imageAuditingList = new ArrayList<>();
    final MultipleImageAuditingImpl multipleImageAuditing = new MultipleImageAuditingImpl(description, transferProgress, imageAuditingList);
    multipleImageAuditing.setMonitor(new MultipleFileTransferMonitor(multipleImageAuditing, imageAuditingList));
    final CountDownLatch latch = new CountDownLatch(1);
    MultipleFileTransferStateChangeListener transferListener = new MultipleFileTransferStateChangeListener(latch, multipleImageAuditing);
    for (ImageAuditingRequest imageAuditingRequest : requestList) {
        imageAuditingList.add(doImageAuditing(imageAuditingRequest, transferListener));
    }
    latch.countDown();
    return multipleImageAuditing;
}
Also used : MultipleFileTransferStateChangeListener(com.qcloud.cos.event.MultipleFileTransferStateChangeListener) ArrayList(java.util.ArrayList) ImageAuditingRequest(com.qcloud.cos.model.ciModel.auditing.ImageAuditingRequest) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with MultipleFileTransferStateChangeListener

use of com.qcloud.cos.event.MultipleFileTransferStateChangeListener in project cos-java-sdk-v5 by tencentyun.

the class TransferManager method downloadDirectory.

/**
 * Downloads all objects in the virtual directory designated by the keyPrefix given to the
 * destination directory given. All virtual subdirectories will be downloaded recursively.
 *
 * @param bucketName The bucket containing the virtual directory
 * @param keyPrefix The key prefix for the virtual directory, or null for the entire bucket. All
 *        subdirectories will be downloaded recursively.
 * @param destinationDirectory The directory to place downloaded files. Subdirectories will be
 *        created as necessary.
 */
public MultipleFileDownload downloadDirectory(String bucketName, String keyPrefix, File destinationDirectory) {
    if (keyPrefix == null)
        keyPrefix = "";
    List<COSObjectSummary> objectSummaries = new LinkedList<COSObjectSummary>();
    Stack<String> commonPrefixes = new Stack<String>();
    commonPrefixes.add(keyPrefix);
    long totalSize = 0;
    // This is a depth-first search.
    do {
        String prefix = commonPrefixes.pop();
        ObjectListing listObjectsResponse = null;
        do {
            if (listObjectsResponse == null) {
                ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName).withDelimiter(DEFAULT_DELIMITER).withPrefix(prefix);
                listObjectsResponse = cos.listObjects(listObjectsRequest);
            } else {
                listObjectsResponse = cos.listNextBatchOfObjects(listObjectsResponse);
            }
            for (COSObjectSummary s : listObjectsResponse.getObjectSummaries()) {
                // name.
                if (!s.getKey().equals(prefix) && !listObjectsResponse.getCommonPrefixes().contains(s.getKey() + DEFAULT_DELIMITER)) {
                    objectSummaries.add(s);
                    totalSize += s.getSize();
                } else {
                    log.debug("Skipping download for object " + s.getKey() + " since it is also a virtual directory");
                }
            }
            commonPrefixes.addAll(listObjectsResponse.getCommonPrefixes());
        } while (listObjectsResponse.isTruncated());
    } while (!commonPrefixes.isEmpty());
    /* This is the hook for adding additional progress listeners */
    ProgressListenerChain additionalListeners = new ProgressListenerChain();
    TransferProgress transferProgress = new TransferProgress();
    transferProgress.setTotalBytesToTransfer(totalSize);
    /*
         * Bind additional progress listeners to this MultipleFileTransferProgressUpdatingListener
         * to receive ByteTransferred events from each single-file download implementation.
         */
    ProgressListener listener = new MultipleFileTransferProgressUpdatingListener(transferProgress, additionalListeners);
    List<DownloadImpl> downloads = new ArrayList<DownloadImpl>();
    String description = "Downloading from " + bucketName + "/" + keyPrefix;
    final MultipleFileDownloadImpl multipleFileDownload = new MultipleFileDownloadImpl(description, transferProgress, additionalListeners, keyPrefix, bucketName, downloads);
    multipleFileDownload.setMonitor(new MultipleFileTransferMonitor(multipleFileDownload, downloads));
    final CountDownLatch latch = new CountDownLatch(1);
    MultipleFileTransferStateChangeListener transferListener = new MultipleFileTransferStateChangeListener(latch, multipleFileDownload);
    for (COSObjectSummary summary : objectSummaries) {
        // TODO: non-standard delimiters
        File f = new File(destinationDirectory, summary.getKey());
        File parentFile = f.getParentFile();
        if (parentFile == null || !parentFile.exists() && !parentFile.mkdirs()) {
            throw new RuntimeException("Couldn't create parent directories for " + f.getAbsolutePath());
        }
        // All the single-file downloads share the same
        // MultipleFileTransferProgressUpdatingListener and
        // MultipleFileTransferStateChangeListener
        downloads.add((DownloadImpl) doDownload(new GetObjectRequest(summary.getBucketName(), summary.getKey()).<GetObjectRequest>withGeneralProgressListener(listener), f, transferListener, null, false));
    }
    if (downloads.isEmpty()) {
        multipleFileDownload.setState(TransferState.Completed);
        return multipleFileDownload;
    }
    // Notify all state changes waiting for the downloads to all be queued
    // to wake up and continue.
    latch.countDown();
    return multipleFileDownload;
}
Also used : MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener) COSObjectSummary(com.qcloud.cos.model.COSObjectSummary) ArrayList(java.util.ArrayList) ObjectListing(com.qcloud.cos.model.ObjectListing) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) Stack(java.util.Stack) ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) 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) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Aggregations

MultipleFileTransferStateChangeListener (com.qcloud.cos.event.MultipleFileTransferStateChangeListener)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 COSProgressListener (com.qcloud.cos.event.COSProgressListener)2 COSProgressListenerChain (com.qcloud.cos.event.COSProgressListenerChain)2 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)2 ProgressListener (com.qcloud.cos.event.ProgressListener)2 ProgressListenerChain (com.qcloud.cos.event.ProgressListenerChain)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 COSObjectSummary (com.qcloud.cos.model.COSObjectSummary)1 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)1 ListObjectsRequest (com.qcloud.cos.model.ListObjectsRequest)1 ObjectListing (com.qcloud.cos.model.ObjectListing)1 ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)1 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)1 ImageAuditingRequest (com.qcloud.cos.model.ciModel.auditing.ImageAuditingRequest)1 Stack (java.util.Stack)1