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());
}
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());
}
}
}
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;
}
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;
}
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;
}
Aggregations