use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class UploadSample method main.
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
// The local file to upload---it must exist.
uploadFileRequest.setUploadFile(uploadFile);
// Sets the concurrent upload task number to 5.
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB.
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enables the checkpoint file. By default it's off.
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
CompleteMultipartUploadResult multipartUploadResult = uploadResult.getMultipartUploadResult();
System.out.println(multipartUploadResult.getETag());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason.");
System.out.println("Error Message: " + oe.getErrorCode());
System.out.println("Error Code: " + oe.getErrorCode());
System.out.println("Request ID: " + oe.getRequestId());
System.out.println("Host ID: " + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network.");
System.out.println("Error Message: " + ce.getMessage());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
use of com.aliyun.oss.model.UploadFileResult in project aliyun-oss-java-sdk by aliyun.
the class UploadFileTest method testUploadFileWithCheckpointFile.
@Test
public void testUploadFileWithCheckpointFile() {
final String key = "obj-upload-file-cpf";
try {
File file = createSampleFile(key, 1024 * 500);
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
uploadFileRequest.setEnableCheckpoint(true);
uploadFileRequest.setCheckpointFile("BingWallpaper.ucp");
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 UploadFileTest method testUploadFileWithCheckpoint.
@Test
public void testUploadFileWithCheckpoint() {
final String key = "obj-upload-file-cp";
try {
File file = createSampleFile(key, 1024 * 500);
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
uploadFileRequest.setEnableCheckpoint(true);
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 testUploadFileWithCheckpoint.
@Test
public void testUploadFileWithCheckpoint() {
final String key = "obj-download-file-cp";
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);
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);
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 DownloadFileTest method testUploadFileWithoutCheckpoint.
@Test
public void testUploadFileWithoutCheckpoint() {
final String key = "obj-download-file-wcp";
try {
File file = createSampleFile(key, 1024 * 500);
// upload file
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
uploadFileRequest.setUploadFile(file.getAbsolutePath());
uploadFileRequest.setTaskNum(10);
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);
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);
}
}
Aggregations