use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class OSSUdfOperation method uploadUdfImage.
/**
* UDF Image
*/
public void uploadUdfImage(UploadUdfImageRequest uploadUdfImageRequest) throws OSSException, ClientException {
assertParameterNotNull(uploadUdfImageRequest, "uploadUdfImageRequest");
assertParameterNotNull(uploadUdfImageRequest.getUdfImage(), "udfImage");
assertParameterNotNull(uploadUdfImageRequest.getName(), "udfImage");
ensureBucketNameValid(uploadUdfImageRequest.getName());
InputStream repeatableInputStream = null;
try {
repeatableInputStream = newRepeatableInputStream(uploadUdfImageRequest.getUdfImage());
} catch (IOException ex) {
logException("Cannot wrap to repeatable input stream: ", ex);
throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
}
Map<String, String> headers = new HashMap<String, String>();
Map<String, String> params = new LinkedHashMap<String, String>();
params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE, null);
params.put(RequestParameters.SUBRESOURCE_UDF_NAME, uploadUdfImageRequest.getName());
if (uploadUdfImageRequest.getUdfImageDesc() != null) {
params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE_DESC, uploadUdfImageRequest.getUdfImageDesc());
}
RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.POST).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, 0)).setOriginalRequest(uploadUdfImageRequest).build();
ProgressListener listener = uploadUdfImageRequest.getProgressListener();
try {
publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
doOperation(httpRequest, emptyResponseParser, null, null, true);
publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
} catch (RuntimeException e) {
publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
throw e;
}
}
use of com.aliyun.oss.ClientException in project aliyun-oss-java-sdk by aliyun.
the class UploadPartCopySample 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 source bucket
*/
System.out.println("Uploading a new object to OSS from a file\n");
client.putObject(new PutObjectRequest(sourceBucketName, sourceKey, createSampleFile()));
/*
* Claim a new upload id for your target bucket
*/
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(targetBucketName, targetKey);
InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
String uploadId = initiateMultipartUploadResult.getUploadId();
/*
* Calculate how many parts to be divided
*/
// 5MB
final long partSize = 5 * 1024 * 1024L;
ObjectMetadata metadata = client.getObjectMetadata(sourceBucketName, sourceKey);
long objectSize = metadata.getContentLength();
int partCount = (int) (objectSize / partSize);
if (objectSize % partSize != 0) {
partCount++;
}
if (partCount > 10000) {
throw new RuntimeException("Total parts count should not exceed 10000");
} else {
System.out.println("Total parts count " + partCount + "\n");
}
/*
* Upload multiparts by copy mode
*/
System.out.println("Begin to upload multiparts by copy mode to OSS\n");
List<PartETag> partETags = new ArrayList<PartETag>();
for (int i = 0; i < partCount; i++) {
long startPos = i * partSize;
long curPartSize = (i + 1 == partCount) ? (objectSize - startPos) : partSize;
;
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucketName, sourceKey, targetBucketName, targetKey);
uploadPartCopyRequest.setUploadId(uploadId);
uploadPartCopyRequest.setPartSize(curPartSize);
uploadPartCopyRequest.setBeginIndex(startPos);
uploadPartCopyRequest.setPartNumber(i + 1);
UploadPartCopyResult uploadPartCopyResult = client.uploadPartCopy(uploadPartCopyRequest);
System.out.println("\tPart#" + uploadPartCopyResult.getPartNumber() + " done\n");
partETags.add(uploadPartCopyResult.getPartETag());
}
/*
* Complete to upload multiparts
*/
System.out.println("Completing to upload multiparts\n");
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucketName, targetKey, uploadId, partETags);
client.completeMultipartUpload(completeMultipartUploadRequest);
/*
* Fetch the object that newly created at the step below.
*/
System.out.println("Fetching an object");
client.getObject(new GetObjectRequest(targetBucketName, targetKey), new File(localFilePath));
} 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 SimpleGetObjectSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/**
* Note that there are two ways of uploading an object to your bucket, the one
* by specifying an input stream as content source, the other by specifying a file.
*/
/*
* Upload an object to your bucket from an input stream
*/
System.out.println("Uploading a new object to OSS from an input stream\n");
String content = "Thank you for using Aliyun Object Storage Service";
client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
/*
* Upload an object to your bucket from a file
*/
System.out.println("Uploading a new object to OSS from a file\n");
client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
/*
* Download an object from your bucket
*/
System.out.println("Downloading an object");
OSSObject object = client.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
displayTextInputStream(object.getObjectContent());
} 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 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.ClientException in project aliyun-oss-java-sdk by aliyun.
the class AppendObjectSample method main.
public static void main(String[] args) throws IOException {
/*
* Constructs a client instance with your account for accessing OSS
*/
OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
/*
* Append an object from specfied input stream, keep in mind that
* position should be set to zero at first time.
*/
String content = "Thank you for using Aliyun Object Storage Service";
InputStream instream = new ByteArrayInputStream(content.getBytes());
Long firstPosition = 0L;
System.out.println("Begin to append object at position(" + firstPosition + ")");
AppendObjectResult appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, instream).withPosition(0L));
System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC() + "\n");
/*
* Continue to append the object from specfied file descriptor at last position
*/
Long nextPosition = appendObjectResult.getNextPosition();
System.out.println("Continue to append object at last position(" + nextPosition + "):");
appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, createTempFile()).withPosition(nextPosition));
System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC());
/*
* View object type of the appendable object
*/
OSSObject object = client.getObject(bucketName, key);
System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
// Do not forget to close object input stream if not use it any more
object.getObjectContent().close();
/*
* Delete the appendable object
*/
System.out.println("Deleting an appendable object");
client.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());
} finally {
/*
* Do not forget to shut down the client finally to release all allocated resources.
*/
client.shutdown();
}
}
Aggregations