Search in sources :

Example 41 with PutObjectResult

use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.

the class ObjectAclTest method testCopyObjectWithACLHeader.

@Test
public void testCopyObjectWithACLHeader() throws IOException {
    final String sourceBucket = "copy-existing-object-source-bucket";
    final String targetBucket = "copy-existing-object-target-bucket";
    final String sourceKey = "copy-existing-object-source-object";
    final String targetKey = "copy-existing-object-target-object";
    final String userMetaKey0 = "user";
    final String userMetaValue0 = "aliy";
    final String userMetaKey1 = "tag";
    final String userMetaValue1 = "copy-object";
    final String contentType = "application/txt";
    try {
        ossClient.createBucket(sourceBucket);
        ossClient.createBucket(targetBucket);
        byte[] content = { 'A', 'l', 'i', 'y', 'u', 'n' };
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(content.length);
        metadata.setContentType(DEFAULT_OBJECT_CONTENT_TYPE);
        metadata.addUserMetadata(userMetaKey0, userMetaValue0);
        PutObjectResult putObjectResult = ossClient.putObject(sourceBucket, sourceKey, new ByteArrayInputStream(content), metadata);
        ObjectMetadata newObjectMetadata = new ObjectMetadata();
        newObjectMetadata.setContentLength(content.length);
        newObjectMetadata.setContentType(contentType);
        newObjectMetadata.addUserMetadata(userMetaKey1, userMetaValue1);
        newObjectMetadata.setObjectAcl(CannedAccessControlList.PublicRead);
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest(sourceBucket, sourceKey, targetBucket, targetKey);
        copyObjectRequest.setNewObjectMetadata(newObjectMetadata);
        CopyObjectResult copyObjectResult = ossClient.copyObject(copyObjectRequest);
        String sourceETag = putObjectResult.getETag();
        String targetETag = copyObjectResult.getETag();
        Assert.assertEquals(sourceETag, targetETag);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        OSSObject ossObject = ossClient.getObject(targetBucket, targetKey);
        newObjectMetadata = ossObject.getObjectMetadata();
        Assert.assertEquals(contentType, newObjectMetadata.getContentType());
        Assert.assertEquals(userMetaValue1, newObjectMetadata.getUserMetadata().get(userMetaKey1));
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(targetBucket, targetKey);
        Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
        Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(ossObject.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    } finally {
        waitForCacheExpiration(5);
        deleteBucketWithObjects(ossClient, sourceBucket);
        deleteBucketWithObjects(ossClient, targetBucket);
    }
}
Also used : CopyObjectRequest(com.aliyun.oss.model.CopyObjectRequest) ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) ByteArrayInputStream(java.io.ByteArrayInputStream) CopyObjectResult(com.aliyun.oss.model.CopyObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 42 with PutObjectResult

use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.

the class ObjectVersionTest method testRestoreObject.

@Test
public void testRestoreObject() {
    String key = "version-test-restore-object";
    long inputStreamLength = 1024;
    try {
        // put
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader("x-oss-storage-class", "Archive");
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        Assert.assertEquals(64, putResult.getVersionId().length());
        // restore
        GenericRequest genericRequest = new GenericRequest(bucketName, key, putResult.getVersionId());
        RestoreObjectResult restoreObjectResult = ossClient.restoreObject(genericRequest);
        Assert.assertEquals(202, restoreObjectResult.getStatusCode());
        Assert.assertNotNull(restoreObjectResult.getVersionId());
        Assert.assertEquals(64, restoreObjectResult.getVersionId().length());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) RestoreObjectResult(com.aliyun.oss.model.RestoreObjectResult) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GenericRequest(com.aliyun.oss.model.GenericRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 43 with PutObjectResult

use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.

the class GetSimplifiedObjectMetaTest method testNormalGetSimplifiedObjectMeta.

@Test
public void testNormalGetSimplifiedObjectMeta() {
    final String key = "normal-get-simplified-object-meta";
    final long inputStreamLength = 1024;
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, genFixedLengthInputStream(inputStreamLength), null);
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        OSSObject o = ossClient.getObject(getObjectRequest);
        Assert.assertEquals(bucketName, o.getBucketName());
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        o.getObjectContent().close();
        SimplifiedObjectMeta objectMeta = ossClient.getSimplifiedObjectMeta(bucketName, key);
        Assert.assertEquals(inputStreamLength, objectMeta.getSize());
        Assert.assertEquals(putObjectResult.getETag(), objectMeta.getETag());
        Assert.assertNotNull(objectMeta.getLastModified());
        Assert.assertNotNull(objectMeta.toString());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 44 with PutObjectResult

use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbackDefault.

/**
 * Testing default value settings. Only url and body are specified, others use default values.
 */
@Test
public void testPutObjectCallbackDefault() throws Exception {
    String key = "put-callback-default";
    try {
        InputStream instream = genFixedLengthInputStream(instreamLength);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, instream);
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackBody("put-object-callback");
        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());
        obj.forcedClose();
    } catch (Exception ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 45 with PutObjectResult

use of com.aliyun.oss.model.PutObjectResult in project aliyun-oss-java-sdk by aliyun.

the class CallbackTest method testPutObjectCallbacJsonChar.

/**
 * callback body var has special characters, type is json.
 */
@Test
public void testPutObjectCallbacJsonChar() throws Exception {
    String key = "put-callback-json-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());
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) OSSObject(com.aliyun.oss.model.OSSObject) PutObjectResult(com.aliyun.oss.model.PutObjectResult) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)56 Test (org.junit.Test)31 OSSException (com.aliyun.oss.OSSException)30 InputStream (java.io.InputStream)24 OSSObject (com.aliyun.oss.model.OSSObject)20 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)19 File (java.io.File)15 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)13 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)10 FileInputStream (java.io.FileInputStream)9 Callback (com.aliyun.oss.model.Callback)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)7 Date (java.util.Date)7 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)6 IOException (java.io.IOException)6 Ignore (org.junit.Ignore)6 OSS (com.aliyun.oss.OSS)5 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)5