use of com.aliyun.oss.model.AppendObjectResult in project aliyun-oss-java-sdk by aliyun.
the class OSSObjectOperation method appendObject.
/**
* Upload input stream or file to oss by append mode.
*/
public AppendObjectResult appendObject(AppendObjectRequest appendObjectRequest) throws OSSException, ClientException {
assertParameterNotNull(appendObjectRequest, "appendObjectRequest");
AppendObjectResult result = writeObjectInternal(WriteMode.APPEND, appendObjectRequest, appendObjectResponseParser);
if (appendObjectRequest.getInitCRC() != null && result.getClientCRC() != null) {
result.setClientCRC(CRC64.combine(appendObjectRequest.getInitCRC(), result.getClientCRC(), (result.getNextPosition() - appendObjectRequest.getPosition())));
}
if (isCrcCheckEnabled() && appendObjectRequest.getInitCRC() != null) {
OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
}
return result;
}
use of com.aliyun.oss.model.AppendObjectResult 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();
}
}
use of com.aliyun.oss.model.AppendObjectResult 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.model.AppendObjectResult in project aliyun-oss-java-sdk by aliyun.
the class GetRequestIdTest method testNormalGetRequestId.
@Test
public void testNormalGetRequestId() {
final String key = "normal-get-request-id";
final long inputStreamLength = 1024;
final int requestIdLength = "572BF2F2207FB3397648E9F1".length();
try {
// put object
PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, genFixedLengthInputStream(inputStreamLength));
Assert.assertEquals(putObjectResult.getRequestId().length(), requestIdLength);
// get object
OSSObject ossObject = ossClient.getObject(bucketName, key);
ossObject.getObjectContent().close();
Assert.assertEquals(ossObject.getRequestId().length(), requestIdLength);
File file = new File("tmp");
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
ObjectMetadata objectMeta = ossClient.getObject(getObjectRequest, file);
Assert.assertEquals(objectMeta.getRequestId().length(), requestIdLength);
// delete object
ossClient.deleteObject(bucketName, key);
// append object
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, file);
appendObjectRequest.setPosition(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
Assert.assertEquals(appendObjectResult.getRequestId().length(), requestIdLength);
// getSimplifiedObjectMeta
SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
Assert.assertEquals(simplifiedObjectMeta.getRequestId().length(), requestIdLength);
// getObjectMetadata
ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, key);
Assert.assertEquals(objectMetadata.getRequestId().length(), requestIdLength);
// delete objects
DeleteObjectsRequest deleteObjectsRequest = new DeleteObjectsRequest(bucketName);
ArrayList<String> keys = new ArrayList<String>();
keys.add(key);
deleteObjectsRequest.setKeys(keys);
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(deleteObjectsRequest);
Assert.assertEquals(deleteObjectsResult.getRequestId().length(), requestIdLength);
// initiate multipart upload
InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(initiateMultipartUploadRequest);
Assert.assertEquals(initiateMultipartUploadResult.getRequestId().length(), requestIdLength);
// upload part
UploadPartRequest uploadPartRequest = new UploadPartRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), 1, new FileInputStream(file), inputStreamLength);
UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
Assert.assertEquals(uploadPartResult.getRequestId().length(), requestIdLength);
// complete multipart upload
List<PartETag> partETags = new ArrayList<PartETag>();
partETags.add(new PartETag(1, uploadPartResult.getETag()));
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, initiateMultipartUploadResult.getUploadId(), partETags);
CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), requestIdLength);
// copy object
CopyObjectResult CopyObjectResult = ossClient.copyObject(bucketName, key, bucketName, key);
Assert.assertEquals(CopyObjectResult.getRequestId().length(), requestIdLength);
// initiate multipart copy
InitiateMultipartUploadRequest initiateMultipartCopyRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initiateMultipartCopyResult = ossClient.initiateMultipartUpload(initiateMultipartCopyRequest);
Assert.assertEquals(initiateMultipartCopyResult.getRequestId().length(), requestIdLength);
// upload part copy
UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(bucketName, key, bucketName, key, initiateMultipartCopyResult.getUploadId(), 1, 0L, inputStreamLength);
UploadPartCopyResult uploadPartCopyResult = ossClient.uploadPartCopy(uploadPartCopyRequest);
Assert.assertEquals(uploadPartCopyResult.getRequestId().length(), requestIdLength);
// abort multipart upload
AbortMultipartUploadRequest AbortMultipartUploadRequest = new AbortMultipartUploadRequest(bucketName, key, initiateMultipartCopyResult.getUploadId());
ossClient.abortMultipartUpload(AbortMultipartUploadRequest);
ossClient.deleteObject(bucketName, key);
file.delete();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
use of com.aliyun.oss.model.AppendObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CRCChecksumTest method testAppendObjectCRC.
@Test
public void testAppendObjectCRC() {
String key = "append-object-crc";
String content = "Hello OSS, Hi OSS.";
try {
AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes())).withPosition(0L);
// 第一次追加
appendObjectRequest.setInitCRC(0L);
AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
checkCRC(appendObjectResult);
// 第二次追加
appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
appendObjectResult = ossClient.appendObject(appendObjectRequest);
checkCRC(appendObjectResult);
// 第三次追加
appendObjectRequest = new AppendObjectRequest(bucketName, key, new ByteArrayInputStream(content.getBytes()));
appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
appendObjectRequest.setInitCRC(appendObjectResult.getClientCRC());
appendObjectResult = ossClient.appendObject(appendObjectRequest);
checkCRC(appendObjectResult);
ossClient.deleteObject(bucketName, key);
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
Aggregations