Search in sources :

Example 6 with ListObjectsRequest

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

the class ListObjectsDemo method listObjectsDemo.

public static void listObjectsDemo() {
    // 1 初始化用户身份信息(appid, 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";
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    // 设置bucket名称
    listObjectsRequest.setBucketName(bucketName);
    // prefix表示列出的object的key以prefix开始
    listObjectsRequest.setPrefix("");
    // 设置最大遍历出多少个对象, 一次listobject最大支持1000
    listObjectsRequest.setMaxKeys(1000);
    // listObjectsRequest.setDelimiter("/");
    ObjectListing objectListing = null;
    try {
        objectListing = cosclient.listObjects(listObjectsRequest);
    } catch (CosServiceException e) {
        e.printStackTrace();
    } catch (CosClientException e) {
        e.printStackTrace();
    }
    // common prefix表示表示被delimiter截断的路径, 如delimter设置为/, common prefix则表示所有子目录的路径
    List<String> commonPrefixs = objectListing.getCommonPrefixes();
    // object summary表示所有列出的object列表
    List<COSObjectSummary> cosObjectSummaries = objectListing.getObjectSummaries();
    for (COSObjectSummary cosObjectSummary : cosObjectSummaries) {
        // 文件的路径key
        String key = cosObjectSummary.getKey();
        // 文件的etag
        String etag = cosObjectSummary.getETag();
        // 文件的长度
        long fileSize = cosObjectSummary.getSize();
        // 文件的存储类型
        String storageClasses = cosObjectSummary.getStorageClass();
        System.out.println("key: " + key);
    }
    cosclient.shutdown();
}
Also used : COSCredentials(com.qcloud.cos.auth.COSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) BasicCOSCredentials(com.qcloud.cos.auth.BasicCOSCredentials) COSObjectSummary(com.qcloud.cos.model.COSObjectSummary) CosClientException(com.qcloud.cos.exception.CosClientException) ObjectListing(com.qcloud.cos.model.ObjectListing) COSClient(com.qcloud.cos.COSClient) ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) CosServiceException(com.qcloud.cos.exception.CosServiceException) Region(com.qcloud.cos.region.Region) ClientConfig(com.qcloud.cos.ClientConfig)

Example 7 with ListObjectsRequest

use of com.qcloud.cos.model.ListObjectsRequest 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)

Example 8 with ListObjectsRequest

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

the class ListObjectTest method ListObjectStartWithDelimiterTest.

@Test
public void ListObjectStartWithDelimiterTest() {
    if (!judgeUserInfoValid()) {
        return;
    }
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucket, keyPrefix, null, "/", 100);
    ObjectListing objectListing = cosclient.listObjects(listObjectsRequest);
    assertEquals(1L, objectListing.getCommonPrefixes().size());
    assertEquals(0L, objectListing.getObjectSummaries().size());
}
Also used : ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) ObjectListing(com.qcloud.cos.model.ObjectListing) Test(org.junit.Test)

Example 9 with ListObjectsRequest

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

the class ListObjectTest method ListObjectStartWithNoMaxKey.

@Test
public void ListObjectStartWithNoMaxKey() {
    if (!judgeUserInfoValid()) {
        return;
    }
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket).withDelimiter("/");
    ObjectListing objectListing = cosclient.listObjects(listObjectsRequest);
    assertEquals(1L, objectListing.getCommonPrefixes().size());
    assertEquals(0L, objectListing.getObjectSummaries().size());
}
Also used : ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) ObjectListing(com.qcloud.cos.model.ObjectListing) Test(org.junit.Test)

Example 10 with ListObjectsRequest

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

the class COSUnderFileSystem method getObjectListingChunk.

@Override
protected ObjectListingChunk getObjectListingChunk(String key, boolean recursive) throws IOException {
    String delimiter = recursive ? "" : PATH_SEPARATOR;
    key = PathUtils.normalizePath(key, PATH_SEPARATOR);
    // In case key is root (empty string) do not normalize prefix
    key = key.equals(PATH_SEPARATOR) ? "" : key;
    ListObjectsRequest request = new ListObjectsRequest();
    request.setBucketName(mBucketNameInternal);
    request.setPrefix(key);
    request.setMaxKeys(getListingChunkLength(mUfsConf));
    request.setDelimiter(delimiter);
    ObjectListing result = getObjectListingChunk(request);
    if (result != null) {
        return new COSObjectListingChunk(request, result);
    }
    return null;
}
Also used : ListObjectsRequest(com.qcloud.cos.model.ListObjectsRequest) ObjectListing(com.qcloud.cos.model.ObjectListing)

Aggregations

ListObjectsRequest (com.qcloud.cos.model.ListObjectsRequest)10 ObjectListing (com.qcloud.cos.model.ObjectListing)9 COSObjectSummary (com.qcloud.cos.model.COSObjectSummary)5 CosClientException (com.qcloud.cos.exception.CosClientException)4 CosServiceException (com.qcloud.cos.exception.CosServiceException)4 Test (org.junit.Test)3 COSClient (com.qcloud.cos.COSClient)2 ClientConfig (com.qcloud.cos.ClientConfig)2 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)2 COSCredentials (com.qcloud.cos.auth.COSCredentials)2 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)2 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)2 Region (com.qcloud.cos.region.Region)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 COSProgressListener (com.qcloud.cos.event.COSProgressListener)1 COSProgressListenerChain (com.qcloud.cos.event.COSProgressListenerChain)1 MultipleFileTransferProgressUpdatingListener (com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)1 MultipleFileTransferStateChangeListener (com.qcloud.cos.event.MultipleFileTransferStateChangeListener)1 ProgressListener (com.qcloud.cos.event.ProgressListener)1