use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class BucketStatTest method testGetBucketStat.
@Test
public void testGetBucketStat() {
String key = "obj-upload-file-stat.txt";
String uploadId = null;
try {
File file = createSampleFile(key, 1024 * 500);
// upload a file
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
// init upload
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
uploadId = initiateMultipartUploadResult.getUploadId();
BucketStat stat = ossClient.getBucketStat(bucketName);
System.out.println(stat.getStorageSize() + "," + stat.getObjectCount() + "," + stat.getMultipartUploadCount());
Assert.assertTrue(stat.getStorageSize() >= 1024 * 300);
Assert.assertTrue(stat.getObjectCount() >= 1);
Assert.assertTrue(stat.getMultipartUploadCount() >= 1);
Assert.assertEquals(stat.getRequestId().length(), REQUEST_ID_LEN);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (uploadId != null) {
AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, uploadId);
ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
}
}
}
use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class UploadFileTest method testUploadFileWithoutCheckpoint.
@Test
public void testUploadFileWithoutCheckpoint() {
final String key = "obj-upload-file-wcp";
try {
File file = createSampleFile(key, 1024 * 500);
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
ObjectListing objects = ossClient.listObjects(bucketName, key);
Assert.assertEquals(objects.getObjectSummaries().size(), 1);
Assert.assertEquals(objects.getObjectSummaries().get(0).getKey(), key);
Assert.assertEquals(objects.getObjectSummaries().get(0).getSize(), file.length());
Assert.assertEquals(objects.getRequestId().length(), REQUEST_ID_LEN);
ObjectMetadata meta = ossClient.getObjectMetadata(bucketName, key);
Assert.assertEquals(meta.getContentLength(), file.length());
Assert.assertEquals(meta.getContentType(), "text/plain");
File fileNew = new File(key + "-new.txt");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ossClient.getObject(getObjectRequest, fileNew);
Assert.assertEquals(file.length(), fileNew.length());
ossClient.deleteObject(bucketName, key);
fileNew.delete();
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class DownloadFileTest method testUploadFileWithCheckpointFile.
@Test
public void testUploadFileWithCheckpointFile() {
final String key = "obj-download-file-cpf";
try {
File file = createSampleFile(key, 1024 * 500);
// upload file
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
uploadFileRequest.setEnableCheckpoint(true);
uploadFileRequest.setCheckpointFile("BingWallpaper.ucp");
ObjectMetadata objMetadata = new ObjectMetadata();
objMetadata.addUserMetadata("prop", "propval");
uploadFileRequest.setObjectMetadata(objMetadata);
UploadFileResult uploadRes = ossClient.uploadFile(uploadFileRequest);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getBucketName(), bucketName);
Assert.assertEquals(uploadRes.getMultipartUploadResult().getKey(), key);
// download file
String filePathNew = key + "-new.txt";
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
downloadFileRequest.setDownloadFile(filePathNew);
downloadFileRequest.setTaskNum(10);
downloadFileRequest.setEnableCheckpoint(true);
downloadFileRequest.setCheckpointFile("BingWallpaper.dcp");
DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objMeta = downloadRes.getObjectMetadata();
Assert.assertEquals(objMeta.getContentLength(), 102400);
Assert.assertEquals(objMeta.getObjectType(), "Multipart");
Assert.assertEquals(objMeta.getUserMetadata().get("prop"), "propval");
File fileNew = new File(filePathNew);
Assert.assertTrue("comparte file", compareFile(file.getAbsolutePath(), fileNew.getAbsolutePath()));
ossClient.deleteObject(bucketName, key);
fileNew.delete();
} catch (Throwable e) {
e.printStackTrace();
Assert.fail(e.getMessage());
ossClient.deleteBucket(bucketName);
}
}
use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class OSSUploadOperation method uploadFileWithCheckpoint.
private UploadFileResult uploadFileWithCheckpoint(UploadFileRequest uploadFileRequest) throws Throwable {
UploadFileResult uploadFileResult = new UploadFileResult();
UploadCheckPoint uploadCheckPoint = new UploadCheckPoint();
// checkpoint file.
if (uploadFileRequest.isEnableCheckpoint()) {
// whole file needs the re-upload.
try {
uploadCheckPoint.load(uploadFileRequest.getCheckpointFile());
} catch (Exception e) {
remove(uploadFileRequest.getCheckpointFile());
}
// The file uploaded is updated, re-upload.
if (!uploadCheckPoint.isValid(uploadFileRequest.getUploadFile())) {
prepare(uploadCheckPoint, uploadFileRequest);
remove(uploadFileRequest.getCheckpointFile());
}
} else {
// The checkpoint is not enabled, re-upload.
prepare(uploadCheckPoint, uploadFileRequest);
}
// The progress tracker starts
ProgressListener listener = uploadFileRequest.getProgressListener();
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
// Concurrently upload parts.
List<PartResult> partResults = upload(uploadCheckPoint, uploadFileRequest);
for (PartResult partResult : partResults) {
if (partResult.isFailed()) {
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
throw partResult.getException();
}
}
// The progress tracker publishes the data.
ProgressPublisher.publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
// Complete parts.
CompleteMultipartUploadResult multipartUploadResult = complete(uploadCheckPoint, uploadFileRequest);
uploadFileResult.setMultipartUploadResult(multipartUploadResult);
// The checkpoint is enabled and upload the checkpoint file.
if (uploadFileRequest.isEnableCheckpoint()) {
remove(uploadFileRequest.getCheckpointFile());
}
return uploadFileResult;
}
Aggregations