Search in sources :

Example 16 with ObjectMetadata

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

the class ListVersionsTest method headAndGetVersion.

private void headAndGetVersion(String key, String versionId, String expectedEtag, long expectedLength, File downloadLocalFile) throws CosServiceException, FileNotFoundException, IOException {
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata = cosclient.getObjectMetadata(getObjectMetadataRequest);
    assertFalse(objectMetadata.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata.getContentLength());
    assertEquals(expectedEtag, objectMetadata.getETag());
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key, versionId);
    ObjectMetadata objectMetadata2 = cosclient.getObject(getObjectRequest, downloadLocalFile);
    assertEquals(expectedLength, downloadLocalFile.length());
    assertEquals(expectedEtag, Md5Utils.md5Hex(downloadLocalFile));
    assertFalse(objectMetadata2.isDeleteMarker());
    assertEquals(expectedLength, objectMetadata2.getContentLength());
    assertEquals(expectedEtag, objectMetadata2.getETag());
    assertTrue(downloadLocalFile.delete());
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) GetObjectRequest(com.qcloud.cos.model.GetObjectRequest)

Example 17 with ObjectMetadata

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

the class AbstractCOSClientTest method testPutObjectByStreamDiffSize.

// 流式上传不同尺寸的文件
protected void testPutObjectByStreamDiffSize(long size, ObjectMetadata originMetaData) throws IOException {
    if (!judgeUserInfoValid()) {
        return;
    }
    File localFile = buildTestFile(size);
    String localFileMd5 = Md5Utils.md5Hex(localFile);
    File downLoadFile = new File(localFile.getAbsolutePath() + ".down");
    String key = "ut/" + localFile.getName();
    originMetaData.setContentLength(size);
    try {
        // put object
        putObjectFromLocalFileByInputStream(localFile, localFile.length(), Md5Utils.md5Hex(localFile), key, originMetaData);
        // get object
        getObject(key, downLoadFile, null, size, localFileMd5);
        // head object
        ObjectMetadata queryObjectMeta = headSimpleObject(key, size, localFileMd5);
        // check meta data
        checkMetaData(originMetaData, queryObjectMeta);
    } finally {
        // delete file on cos
        clearObject(key);
        // delete local file
        if (localFile.exists()) {
            assertTrue(localFile.delete());
        }
        if (downLoadFile.exists()) {
            assertTrue(downLoadFile.delete());
        }
    }
}
Also used : File(java.io.File) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata)

Example 18 with ObjectMetadata

use of com.qcloud.cos.model.ObjectMetadata 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 19 with ObjectMetadata

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

the class TransferManager method copy.

/**
 * <p>
 * Schedules a new transfer to copy data from one object to another . This method is
 * non-blocking and returns immediately (i.e. before the copy has finished).
 * </p>
 * <p>
 * Note: You need to use this method if the {@link TransferManager} is created with a regional
 * COS client and the source & destination buckets are in different regions.
 * </p>
 * <p>
 * <code>TransferManager</code> doesn't support copying of encrypted objects whose encryption
 * materials are stored in an 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>
 *
 * <p>
 * <b>Note:</b> If the {@link TransferManager} is created with a regional COS client and the
 * source & destination buckets are in different regions, use the
 * {@link #copy(CopyObjectRequest, COS, TransferStateChangeListener)} method.
 * </p>
 *
 * @param copyObjectRequest The request containing all the parameters for the copy.
 * @param srcCOS An COS client constructed for the region in which the source object's bucket is
 *        located.
 * @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 COS srcCOS, 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.");
    assertParameterNotNull(srcCOS, "The srcCOS parameter is mandatory");
    String description = "Copying object from " + copyObjectRequest.getSourceBucketName() + "/" + copyObjectRequest.getSourceKey() + " to " + copyObjectRequest.getDestinationBucketName() + "/" + copyObjectRequest.getDestinationKey();
    GetObjectMetadataRequest getObjectMetadataRequest = new GetObjectMetadataRequest(copyObjectRequest.getSourceBucketName(), copyObjectRequest.getSourceKey()).withVersionId(copyObjectRequest.getSourceVersionId());
    ObjectMetadata metadata = srcCOS.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);
    copy.setMonitor(watcher);
    return copy;
}
Also used : GetObjectMetadataRequest(com.qcloud.cos.model.GetObjectMetadataRequest) CopyImpl(com.qcloud.cos.internal.CopyImpl) COSProgressListenerChain(com.qcloud.cos.event.COSProgressListenerChain) ProgressListenerChain(com.qcloud.cos.event.ProgressListenerChain) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) TransferProgressUpdatingListener(com.qcloud.cos.event.TransferProgressUpdatingListener) MultipleFileTransferProgressUpdatingListener(com.qcloud.cos.event.MultipleFileTransferProgressUpdatingListener)

Example 20 with ObjectMetadata

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

the class UploadCallable method uploadPartsInSeries.

/**
 * Uploads all parts in the request in serial in this thread, then completes the upload and
 * returns the result.
 */
private UploadResult uploadPartsInSeries(UploadPartRequestFactory requestFactory) {
    final List<PartETag> partETags = new ArrayList<PartETag>();
    while (requestFactory.hasMoreRequests()) {
        if (threadPool.isShutdown())
            throw new CancellationException("TransferManager has been shutdown");
        UploadPartRequest uploadPartRequest = requestFactory.getNextUploadPartRequest();
        // Mark the stream in case we need to reset it
        InputStream inputStream = uploadPartRequest.getInputStream();
        if (inputStream != null && inputStream.markSupported()) {
            if (uploadPartRequest.getPartSize() >= Integer.MAX_VALUE) {
                inputStream.mark(Integer.MAX_VALUE);
            } else {
                inputStream.mark((int) uploadPartRequest.getPartSize());
            }
        }
        partETags.add(cos.uploadPart(uploadPartRequest).getPartETag());
    }
    CompleteMultipartUploadRequest req = new CompleteMultipartUploadRequest(origReq.getBucketName(), origReq.getKey(), multipartUploadId, partETags).withGeneralProgressListener(origReq.getGeneralProgressListener());
    ObjectMetadata origMeta = origReq.getMetadata();
    if (origMeta != null) {
        ObjectMetadata objMeta = req.getObjectMetadata();
        if (objMeta == null) {
            objMeta = new ObjectMetadata();
        }
        objMeta.setUserMetadata(origMeta.getUserMetadata());
        req.setObjectMetadata(objMeta);
    }
    if (origReq.getPicOperations() != null) {
        req.setPicOperations(origReq.getPicOperations());
    }
    TransferManagerUtils.populateEndpointAddr(origReq, req);
    CompleteMultipartUploadResult res = cos.completeMultipartUpload(req);
    UploadResult uploadResult = new UploadResult();
    uploadResult.setBucketName(res.getBucketName());
    uploadResult.setKey(res.getKey());
    uploadResult.setETag(res.getETag());
    uploadResult.setVersionId(res.getVersionId());
    uploadResult.setRequestId(res.getRequestId());
    uploadResult.setDateStr(res.getDateStr());
    uploadResult.setCrc64Ecma(res.getCrc64Ecma());
    uploadResult.setCiUploadResult(res.getCiUploadResult());
    return uploadResult;
}
Also used : CancellationException(java.util.concurrent.CancellationException) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.qcloud.cos.model.UploadPartRequest) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) CompleteMultipartUploadResult(com.qcloud.cos.model.CompleteMultipartUploadResult) UploadResult(com.qcloud.cos.model.UploadResult) ObjectMetadata(com.qcloud.cos.model.ObjectMetadata) PartETag(com.qcloud.cos.model.PartETag) CompleteMultipartUploadRequest(com.qcloud.cos.model.CompleteMultipartUploadRequest)

Aggregations

ObjectMetadata (com.qcloud.cos.model.ObjectMetadata)77 CosClientException (com.qcloud.cos.exception.CosClientException)26 CosServiceException (com.qcloud.cos.exception.CosServiceException)23 IOException (java.io.IOException)22 File (java.io.File)21 PutObjectRequest (com.qcloud.cos.model.PutObjectRequest)17 COSClient (com.qcloud.cos.COSClient)15 ClientConfig (com.qcloud.cos.ClientConfig)14 GetObjectRequest (com.qcloud.cos.model.GetObjectRequest)14 PutObjectResult (com.qcloud.cos.model.PutObjectResult)14 Region (com.qcloud.cos.region.Region)14 ResponseNotCompleteException (com.qcloud.cos.exception.ResponseNotCompleteException)13 BasicCOSCredentials (com.qcloud.cos.auth.BasicCOSCredentials)12 COSCredentials (com.qcloud.cos.auth.COSCredentials)12 Test (org.junit.Test)11 GetObjectMetadataRequest (com.qcloud.cos.model.GetObjectMetadataRequest)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 InputStream (java.io.InputStream)8 CopyObjectRequest (com.qcloud.cos.model.CopyObjectRequest)7 COSObject (com.qcloud.cos.model.COSObject)6