use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testGeneratePresignedUrlWithCallback.
@Test
public void testGeneratePresignedUrlWithCallback() {
String key = "generate-presigned-url-callback";
try {
// callback
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("bucket=${bucket}&object=${object}&etag=${etag}&size=${size}&" + "mimeType=${mimeType}&my_var1=${x:var1}&my_var2=${x:var2}");
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
callback.setCalbackBodyType(CalbackBodyType.URL);
// generate put url
Map<String, String> cbHeaders = new HashMap<String, String>();
OSSUtils.populateRequestCallback(cbHeaders, callback);
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT);
request.setExpiration(expiration);
request.setContentType("text/plain");
request.setHeaders(cbHeaders);
request.addHeader("x-oss-meta-author", "mingdi");
URL signedUrl = ossClient.generatePresignedUrl(request);
System.out.println("SignedUrl:" + signedUrl);
// put with url
Map<String, String> customHeaders = new HashMap<String, String>(cbHeaders);
customHeaders.put("Content-Type", "text/plain");
customHeaders.put("x-oss-meta-author", "mingdi");
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectResult putResult = ossClient.putObject(signedUrl, instream, instreamLength, customHeaders);
// check callback body
byte[] buffer = new byte[bufferLength];
int nRead = putResult.getCallbackResponseBody().read(buffer);
putResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
// get object and check
OSSObject ossObject = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, ossObject.getKey());
Assert.assertEquals(instreamLength, ossObject.getObjectMetadata().getContentLength());
Assert.assertEquals("mingdi", ossObject.getObjectMetadata().getUserMetadata().get("author"));
ossObject.getObjectContent().close();
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbackBodyType.
/**
* callback body type,type of callback body is json
*/
@Test
public void testPutObjectCallbackBodyType() throws Exception {
String key = "put-callback-body-type";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
putObjectResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbacURLChar.
/**
* callback body var has special characters, type is json.
*/
@Test
public void testPutObjectCallbacURLChar() throws Exception {
String key = "put-callback-url-char";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("春水碧于天,画船听雨眠。垆边人似月,皓腕凝霜雪。");
callback.setCalbackBodyType(CalbackBodyType.JSON);
callback.addCallbackVar("x:键值1", "值1:凌波不过横塘路,但目送,芳尘去。");
callback.addCallbackVar("x:键值2", "值2:长记曾携手处,千树压、西湖寒碧。");
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getCallbackResponseBody().read(buffer);
putObjectResult.getCallbackResponseBody().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.
the class CallbackTest method testPutObjectCallbackVar.
/**
* callback body type,type of callback body is json
*/
@Test
public void testPutObjectCallbackVar() throws Exception {
String key = "put-callback-var";
try {
InputStream instream = genFixedLengthInputStream(instreamLength);
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
Callback callback = new Callback();
callback.setCallbackUrl(callbackUrl);
callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
callback.setCallbackBody("{\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}}");
callback.setCalbackBodyType(CalbackBodyType.JSON);
callback.addCallbackVar("x:var1", "value1");
callback.addCallbackVar("x:var2", "value2");
putObjectRequest.setCallback(callback);
PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
byte[] buffer = new byte[bufferLength];
int nRead = putObjectResult.getResponse().getContent().read(buffer);
putObjectResult.getResponse().getContent().close();
Assert.assertEquals(callbackResponse, new String(buffer, 0, nRead));
OSSObject obj = ossClient.getObject(bucketName, key);
Assert.assertEquals(key, obj.getKey());
Assert.assertEquals(instreamLength, obj.getObjectMetadata().getContentLength());
} catch (Exception ex) {
Assert.fail(ex.getMessage());
}
}
use of com.aliyun.oss.model.PutObjectResult 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());
}
}
Aggregations