Search in sources :

Example 1 with ObjectAcl

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

the class ObjectAclTest method testNormalSetObjectAcl.

@Test
public void testNormalSetObjectAcl() {
    final String key = "normal-set-object-acl";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ossClient.putObject(bucketName, key, instream);
        for (CannedAccessControlList acl : ACLS) {
            ossClient.setObjectAcl(bucketName, key, acl);
            ObjectAcl returnedAcl = ossClient.getObjectAcl(bucketName, key);
            Assert.assertEquals(acl.toString(), returnedAcl.getPermission().toString());
            Assert.assertEquals(returnedAcl.getRequestId().length(), REQUEST_ID_LEN);
            OSSObject object = ossClient.getObject(bucketName, key);
            Assert.assertEquals(inputStreamLength, object.getObjectMetadata().getContentLength());
            Assert.assertEquals(object.getRequestId().length(), REQUEST_ID_LEN);
            object.getObjectContent().close();
        }
        // Set to default acl again
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
        ObjectAcl returnedAcl = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.Default, returnedAcl.getPermission());
        Assert.assertEquals(returnedAcl.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 2 with ObjectAcl

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

the class ObjectAclTest method testPutObjectWithACLHeader.

@Test
public void testPutObjectWithACLHeader() throws IOException {
    final String key = "put-object-with-acl-header";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        ossClient.putObject(bucketName, key, instream, metadata);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
        Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ObjectAcl

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

the class ObjectAclTest method testUploadMultipartsWithAclHeader.

@Test
public void testUploadMultipartsWithAclHeader() {
    final String key = "normal-upload-multiparts-with-acl-header";
    // 128KB
    final int partSize = 128 * 1024;
    final int partCount = 10;
    try {
        // Initial multipart upload
        String uploadId = claimUploadId(ossClient, bucketName, key);
        // Upload parts
        List<PartETag> partETags = new ArrayList<PartETag>();
        for (int i = 0; i < partCount; i++) {
            InputStream instream = genFixedLengthInputStream(partSize);
            UploadPartRequest uploadPartRequest = new UploadPartRequest();
            uploadPartRequest.setBucketName(bucketName);
            uploadPartRequest.setKey(key);
            uploadPartRequest.setInputStream(instream);
            uploadPartRequest.setPartNumber(i + 1);
            uploadPartRequest.setPartSize(partSize);
            uploadPartRequest.setUploadId(uploadId);
            UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
            Assert.assertEquals(uploadPartResult.getRequestId().length(), REQUEST_ID_LEN);
            partETags.add(uploadPartResult.getPartETag());
        }
        // Complete multipart upload
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, partETags);
        completeMultipartUploadRequest.setObjectACL(CannedAccessControlList.PublicRead);
        CompleteMultipartUploadResult completeMultipartUploadResult = ossClient.completeMultipartUpload(completeMultipartUploadRequest);
        Assert.assertEquals(composeLocation(ossClient, OSS_TEST_ENDPOINT, bucketName, key), completeMultipartUploadResult.getLocation());
        Assert.assertEquals(bucketName, completeMultipartUploadResult.getBucketName());
        Assert.assertEquals(key, completeMultipartUploadResult.getKey());
        Assert.assertEquals(calcMultipartsETag(partETags), completeMultipartUploadResult.getETag());
        Assert.assertEquals(completeMultipartUploadResult.getRequestId().length(), REQUEST_ID_LEN);
        // Get uploaded object
        OSSObject o = ossClient.getObject(bucketName, key);
        final long objectSize = partCount * partSize;
        Assert.assertEquals(objectSize, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(calcMultipartsETag(partETags), o.getObjectMetadata().getETag());
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.PublicRead, returnedACL.getPermission());
        Assert.assertEquals(returnedACL.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UploadPartRequest(com.aliyun.oss.model.UploadPartRequest) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) PartETag(com.aliyun.oss.model.PartETag) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) UploadPartResult(com.aliyun.oss.model.UploadPartResult) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest) Test(org.junit.Test)

Example 4 with ObjectAcl

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

the class ObjectAclTest method testIgnoredObjectAclHeader.

@Test
public void testIgnoredObjectAclHeader() {
    final String dummyKey = "test-ignored-object-acl-header";
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setObjectAcl(null);
        ossClient.putObject(bucketName, dummyKey, new ByteArrayInputStream(new byte[0]), metadata);
        ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, dummyKey);
        Assert.assertEquals(ObjectPermission.Default, objectAcl.getPermission());
        Assert.assertEquals(objectAcl.getRequestId().length(), REQUEST_ID_LEN);
        metadata.setObjectAcl(CannedAccessControlList.Private);
        ossClient.putObject(bucketName, dummyKey, new ByteArrayInputStream(new byte[0]), metadata);
        objectAcl = ossClient.getObjectAcl(bucketName, dummyKey);
        Assert.assertEquals(ObjectPermission.Private, objectAcl.getPermission());
        Assert.assertEquals(objectAcl.getRequestId().length(), REQUEST_ID_LEN);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with ObjectAcl

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

the class ObjectAclTest method testUnormalGetObjectAcl.

@Test
public void testUnormalGetObjectAcl() {
    // Get non-existent object acl
    final String nonexistentObject = "unormal-get-object-acl";
    try {
        ossClient.getObjectAcl(bucketName, nonexistentObject);
        Assert.fail("Get object acl should not be successful");
    } catch (OSSException e) {
        Assert.assertEquals(OSSErrorCode.NO_SUCH_KEY, e.getErrorCode());
        Assert.assertTrue(e.getMessage().startsWith(NO_SUCH_KEY_ERR));
    }
    // Get object using default acl
    final String objectUsingDefaultAcl = "object-using-default-acl";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ossClient.putObject(bucketName, objectUsingDefaultAcl, instream);
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, objectUsingDefaultAcl);
        Assert.assertEquals(ObjectPermission.Default, returnedACL.getPermission());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) ByteArrayInputStream(java.io.ByteArrayInputStream) TestUtils.genFixedLengthInputStream(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream) InputStream(java.io.InputStream) OSSException(com.aliyun.oss.OSSException) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ObjectAcl (com.aliyun.oss.model.ObjectAcl)9 OSSException (com.aliyun.oss.OSSException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 IOException (java.io.IOException)7 Test (org.junit.Test)7 OSSObject (com.aliyun.oss.model.OSSObject)6 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)5 InputStream (java.io.InputStream)5 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)4 ClientException (com.aliyun.oss.ClientException)1 OSS (com.aliyun.oss.OSS)1 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)1 ResponseParseException (com.aliyun.oss.common.parser.ResponseParseException)1 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)1 AppendObjectRequest (com.aliyun.oss.model.AppendObjectRequest)1 AppendObjectResult (com.aliyun.oss.model.AppendObjectResult)1 Bucket (com.aliyun.oss.model.Bucket)1 CannedAccessControlList (com.aliyun.oss.model.CannedAccessControlList)1 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)1 CompleteMultipartUploadResult (com.aliyun.oss.model.CompleteMultipartUploadResult)1