Search in sources :

Example 6 with ObjectAcl

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

the class ObjectAclTest method testAppendObjectWithACLHeader.

@Test
public void testAppendObjectWithACLHeader() throws IOException {
    final String key = "append-object-with-acl-header";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    try {
        // Append at first
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setObjectAcl(CannedAccessControlList.PublicReadWrite);
        AppendObjectRequest appendObjectRequest = new AppendObjectRequest(bucketName, key, instream, metadata);
        appendObjectRequest.setPosition(0L);
        AppendObjectResult appendObjectResult = ossClient.appendObject(appendObjectRequest);
        OSSObject o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(key, o.getKey());
        Assert.assertEquals(inputStreamLength, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(o.getRequestId().length(), REQUEST_ID_LEN);
        Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
        if (appendObjectResult.getNextPosition() != null) {
            Assert.assertEquals(inputStreamLength, appendObjectResult.getNextPosition().longValue());
        }
        // Append at twice
        final String filePath = genFixedLengthFile(inputStreamLength);
        appendObjectRequest = new AppendObjectRequest(bucketName, key, new File(filePath));
        appendObjectRequest.setPosition(appendObjectResult.getNextPosition());
        appendObjectResult = ossClient.appendObject(appendObjectRequest);
        o = ossClient.getObject(bucketName, key);
        Assert.assertEquals(inputStreamLength * 2, o.getObjectMetadata().getContentLength());
        Assert.assertEquals(APPENDABLE_OBJECT_TYPE, o.getObjectMetadata().getObjectType());
        if (appendObjectResult.getNextPosition() != null) {
            Assert.assertEquals(inputStreamLength * 2, appendObjectResult.getNextPosition().longValue());
        }
        // Verify uploaded objects acl
        ObjectAcl returnedACL = ossClient.getObjectAcl(bucketName, key);
        Assert.assertEquals(ObjectPermission.PublicReadWrite, 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) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) TestUtils.genFixedLengthFile(com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile) File(java.io.File) OSSException(com.aliyun.oss.OSSException) IOException(java.io.IOException) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult) Test(org.junit.Test)

Example 7 with ObjectAcl

use of com.aliyun.oss.model.ObjectAcl 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 8 with ObjectAcl

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

the class ResponseParsers method parseGetObjectAcl.

/**
 * Unmarshall object acl response body to object ACL.
 */
public static ObjectAcl parseGetObjectAcl(InputStream responseBody) throws ResponseParseException {
    try {
        Element root = getXmlRootElement(responseBody);
        ObjectAcl acl = new ObjectAcl();
        String id = root.getChild("Owner").getChildText("ID");
        String displayName = root.getChild("Owner").getChildText("DisplayName");
        Owner owner = new Owner(id, displayName);
        acl.setOwner(owner);
        String grantString = root.getChild("AccessControlList").getChildText("Grant");
        acl.setPermission(ObjectPermission.parsePermission(grantString));
        return acl;
    } catch (JDOMParseException e) {
        throw new ResponseParseException(e.getPartialDocument() + ": " + e.getMessage(), e);
    } catch (Exception e) {
        throw new ResponseParseException(e.getMessage(), e);
    }
}
Also used : JDOMParseException(org.jdom.input.JDOMParseException) Owner(com.aliyun.oss.model.Owner) ObjectAcl(com.aliyun.oss.model.ObjectAcl) Element(org.jdom.Element) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException) ParseException(java.text.ParseException) JDOMParseException(org.jdom.input.JDOMParseException) ResponseParseException(com.aliyun.oss.common.parser.ResponseParseException)

Example 9 with ObjectAcl

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

the class GetStartedSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    System.out.println("Getting Started with OSS SDK for Java\n");
    try {
        /*
             * Determine whether the bucket exists
             */
        if (!ossClient.doesBucketExist(bucketName)) {
            /*
                 * Create a new OSS bucket
                 */
            System.out.println("Creating bucket " + bucketName + "\n");
            ossClient.createBucket(bucketName);
            CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
            createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
            ossClient.createBucket(createBucketRequest);
        }
        /*
             * List the buckets in your account
             */
        System.out.println("Listing buckets");
        ListBucketsRequest listBucketsRequest = new ListBucketsRequest();
        listBucketsRequest.setMaxKeys(500);
        for (Bucket bucket : ossClient.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();
        /*
             * Upload an object to your bucket
             */
        System.out.println("Uploading a new object to OSS from a file\n");
        ossClient.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        /*
             * Determine whether an object residents in your bucket
             */
        boolean exists = ossClient.doesObjectExist(bucketName, key);
        System.out.println("Does object " + bucketName + " exist? " + exists + "\n");
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.PublicRead);
        ossClient.setObjectAcl(bucketName, key, CannedAccessControlList.Default);
        ObjectAcl objectAcl = ossClient.getObjectAcl(bucketName, key);
        System.out.println("ACL:" + objectAcl.getPermission().toString());
        /*
             * Download an object from your bucket
             */
        System.out.println("Downloading an object");
        OSSObject object = ossClient.getObject(bucketName, key);
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());
        /*
             * List objects in your bucket by prefix
             */
        System.out.println("Listing objects");
        ObjectListing objectListing = ossClient.listObjects(bucketName, "My");
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();
        /*
             * Delete an object
             */
        System.out.println("Deleting an object\n");
        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());
    } finally {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        ossClient.shutdown();
    }
}
Also used : ObjectAcl(com.aliyun.oss.model.ObjectAcl) OSSObject(com.aliyun.oss.model.OSSObject) CreateBucketRequest(com.aliyun.oss.model.CreateBucketRequest) ObjectListing(com.aliyun.oss.model.ObjectListing) OSSException(com.aliyun.oss.OSSException) OSS(com.aliyun.oss.OSS) OSSObjectSummary(com.aliyun.oss.model.OSSObjectSummary) Bucket(com.aliyun.oss.model.Bucket) ListBucketsRequest(com.aliyun.oss.model.ListBucketsRequest) ClientException(com.aliyun.oss.ClientException) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

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