use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class CRCSample method main.
public static void main(String[] args) throws IOException {
String content = "Hello OSS, Hi OSS, OSS OK.";
// CRC check is enabled by default for upload or download. To turn it off, use the commented code below.
// ClientConfiguration config = new ClientConfiguration();
// config.setCrcCheckEnabled(false);
// OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
// putObject/uploadPart/uploadFile are automatically enabled with CRC.
// However, appendObject needs to call AppendObjectRequest.setInitCRC to enable CRC.
ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
ossClient.deleteObject(bucketName, key);
// First data appending
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
// because it's first append, the previous data is empty and thus CRC is 0.
appendObjectRequest.setInitCRC(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
// Second data appending
appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
// the initCRC is the first's CRC.
appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
appendObjectResult = ossClient.appendObject(appendObjectRequest);
ossClient.deleteObject(bucketName, key);
// Upload with checkpoint, it supports CRC as well.
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
// Sets the upload file with a local file.
uploadFileRequest.setUploadFile(uploadFile);
// Sets the concurrent task number to 5 (default is 1).
uploadFileRequest.setTaskNum(5);
// Sets the part size to 1MB (default is 100K)
uploadFileRequest.setPartSize(1024 * 1024 * 1);
// Enable checkpoint (by default is off for checkpoint enabled upload)
uploadFileRequest.setEnableCheckpoint(true);
ossClient.uploadFile(uploadFileRequest);
// Download with CRC. Note that range download does not support CRC.
OSSObject ossObject = ossClient.getObject(bucketName, key);
Assert.assertNull(ossObject.getClientCRC());
Assert.assertNotNull(ossObject.getServerCRC());
InputStream stream = ossObject.getObjectContent();
while (stream.read() != -1) {
}
stream.close();
// Check if CRC is consistent.
OSSUtils.checkChecksum(IOUtils.getCRCValue(stream), ossObject.getServerCRC(), ossObject.getRequestId());
ossClient.deleteObject(bucketName, key);
} 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 (InconsistentException ie) {
System.out.println("Caught an OSSException");
System.out.println("Request ID: " + ie.getRequestId());
} catch (Throwable e) {
e.printStackTrace();
} finally {
ossClient.shutdown();
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ConcurrentGetObjectSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/*
* Upload an object to your bucket
*/
System.out.println("Uploading a new object to OSS from a file\n");
client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
/*
* Get size of the object and pre-create a random access file to hold object data
*/
ObjectMetadata metadata = client.getObjectMetadata(bucketName, key);
long objectSize = metadata.getContentLength();
RandomAccessFile raf = new RandomAccessFile(localFilePath, "rw");
raf.setLength(objectSize);
raf.close();
/*
* Calculate how many blocks to be divided
*/
// 5MB
final long blockSize = 5 * 1024 * 1024L;
int blockCount = (int) (objectSize / blockSize);
if (objectSize % blockSize != 0) {
blockCount++;
}
System.out.println("Total blocks count " + blockCount + "\n");
/*
* Download the object concurrently
*/
System.out.println("Start to download " + key + "\n");
for (int i = 0; i < blockCount; i++) {
long startPos = i * blockSize;
long endPos = (i + 1 == blockCount) ? objectSize : (i + 1) * blockSize;
executorService.execute(new BlockFetcher(startPos, endPos, i + 1));
}
/*
* Waiting for all blocks finished
*/
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
* Verify whether all blocks are finished
*/
if (completedBlocks.intValue() != blockCount) {
throw new IllegalStateException("Download fails due to some blocks are not finished yet");
} else {
System.out.println("Succeed to download object " + key);
}
} 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());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
if (client != null) {
client.shutdown();
}
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class DownloadSample method main.
public static void main(String[] args) throws IOException {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, key);
// Sets the local file to download to
downloadFileRequest.setDownloadFile(downloadFile);
// Sets the concurrent task thread count 5. By default it's 1.
downloadFileRequest.setTaskNum(5);
// Sets the part size, by default it's 100K.
downloadFileRequest.setPartSize(1024 * 1024 * 1);
// Enable checkpoint. By default it's false.
downloadFileRequest.setEnableCheckpoint(true);
DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);
ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
System.out.println(objectMetadata.getETag());
System.out.println(objectMetadata.getLastModified());
System.out.println(objectMetadata.getUserMetadata().get("meta"));
} 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.ClientException in project aliyun-oss-java-sdk by aliyun.
the class ListObjectsSample method main.
public static void main(String[] args) throws IOException {
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
final String content = "Hello OSS";
final String keyPrefix = "MyObjectKey";
if (!client.doesBucketExist(bucketName)) {
client.createBucket(bucketName);
}
// Prepare the environment---inserting 100 test files.
List<String> keys = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
String key = keyPrefix + i;
InputStream instream = new ByteArrayInputStream(content.getBytes());
client.putObject(bucketName, key, instream);
keys.add(key);
}
System.out.println("Put " + keys.size() + " objects completed.");
ObjectListing objectListing = null;
String nextMarker = null;
final int maxKeys = 30;
List<OSSObjectSummary> sums = null;
// use default parameter to list the files. By default up to 100 entries could be listed.
System.out.println("Default paramters:");
objectListing = client.listObjects(bucketName);
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Sets the max keys with 200 (the max value could be 1000).
System.out.println("With max keys:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMaxKeys(200));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified prefix. By default it returns up to 100 entries.
System.out.println("With prefix:");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets the object with specified marker. By default it returns up to 100 entries.
System.out.println("With marker: ");
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(keyPrefix + "11"));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
// Gets all object by paging. Each page will have up to 100 entries.
System.out.println("List all objects:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified prefix by paging. Each page will have up to 100 entries.
System.out.println("List all objects after marker:");
nextMarker = keyPrefix + "11";
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Gets all object with specified marker by paging. Each page will have up to 100 entries.
System.out.println("List all objects with prefix:");
nextMarker = null;
do {
objectListing = client.listObjects(new ListObjectsRequest(bucketName).withPrefix(keyPrefix + "1").withMarker(nextMarker).withMaxKeys(maxKeys));
sums = objectListing.getObjectSummaries();
for (OSSObjectSummary s : sums) {
System.out.println("\t" + s.getKey());
}
nextMarker = objectListing.getNextMarker();
} while (objectListing.isTruncated());
// Clean up the environment----deleting the test files.
System.out.println("Deleting all objects:");
DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
for (String object : deletedObjects) {
System.out.println("\t" + object);
}
} 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());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class OSSMultipartOperation method uploadPart.
/**
* Upload part.
*/
public UploadPartResult uploadPart(UploadPartRequest uploadPartRequest) throws OSSException, ClientException {
assertParameterNotNull(uploadPartRequest, "uploadPartRequest");
String key = uploadPartRequest.getKey();
String bucketName = uploadPartRequest.getBucketName();
String uploadId = uploadPartRequest.getUploadId();
assertParameterNotNull(bucketName, "bucketName");
ensureBucketNameValid(bucketName);
assertParameterNotNull(key, "key");
ensureObjectKeyValid(key);
assertStringNotNullOrEmpty(uploadId, "uploadId");
if (uploadPartRequest.getInputStream() == null) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("MustSetContentStream"));
}
InputStream repeatableInputStream = null;
try {
repeatableInputStream = newRepeatableInputStream(uploadPartRequest.buildPartialStream());
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
int partNumber = uploadPartRequest.getPartNumber();
if (!checkParamRange(partNumber, 0, false, MAX_PART_NUMBER, true)) {
throw new IllegalArgumentException(OSS_RESOURCE_MANAGER.getString("PartNumberOutOfRange"));
}
Map<String, String> headers = new HashMap<String, String>();
populateUploadPartOptionalHeaders(uploadPartRequest, headers);
// Use a LinkedHashMap to preserve the insertion order.
Map<String, String> params = new LinkedHashMap<String, String>();
params.put(PART_NUMBER, Integer.toString(partNumber));
params.put(UPLOAD_ID, uploadId);
RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(key).setParameters(params).setHeaders(headers).setInputStream(repeatableInputStream).setInputSize(uploadPartRequest.getPartSize()).setUseChunkEncoding(uploadPartRequest.isUseChunkEncoding()).setOriginalRequest(uploadPartRequest).build();
final ProgressListener listener = uploadPartRequest.getProgressListener();
ResponseMessage response = null;
try {
publishProgress(listener, ProgressEventType.TRANSFER_PART_STARTED_EVENT);
response = doOperation(request, emptyResponseParser, bucketName, key);
publishProgress(listener, ProgressEventType.TRANSFER_PART_COMPLETED_EVENT);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_PART_FAILED_EVENT);
throw e;
}
UploadPartResult result = new UploadPartResult();
result.setPartNumber(partNumber);
result.setETag(trimQuotes(response.getHeaders().get(OSSHeaders.ETAG)));
result.setRequestId(response.getRequestId());
result.setPartSize(uploadPartRequest.getPartSize());
ResponseParsers.setCRC(result, response);
if (getInnerClient().getClientConfiguration().isCrcCheckEnabled()) {
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
}
return result;
}
Aggregations