Search in sources :

Example 16 with PutObjectResult

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

the class HeadObjectTest method testHeadObject.

@Test
public void testHeadObject() {
    final String key = "head-object";
    final long inputStreanLength = 1024;
    try {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, genFixedLengthInputStream(inputStreanLength), null);
        PutObjectResult putObjectResult = ossClient.putObject(putObjectRequest);
        Assert.assertEquals(putObjectResult.getRequestId().length(), REQUEST_ID_LEN);
        HeadObjectRequest headObjectRequest = new HeadObjectRequest(bucketName, key);
        List<String> matchingETags = new LinkedList<String>();
        matchingETags.add(putObjectResult.getETag());
        headObjectRequest.setMatchingETagConstraints(matchingETags);
        ObjectMetadata o = ossClient.headObject(headObjectRequest);
        Assert.assertEquals(o.getETag(), putObjectResult.getETag());
        headObjectRequest = new HeadObjectRequest(bucketName, key);
        List<String> nonmatchingEtags = new LinkedList<String>();
        nonmatchingEtags.add("nonmatching");
        headObjectRequest.setNonmatchingETagConstraints(nonmatchingEtags);
        o = ossClient.headObject(headObjectRequest);
        Assert.assertEquals(o.getETag(), putObjectResult.getETag());
        headObjectRequest = new HeadObjectRequest(bucketName, key);
        headObjectRequest.setModifiedSinceConstraint(new Date(System.currentTimeMillis() - 3600 * 1000));
        o = ossClient.headObject(headObjectRequest);
        Assert.assertEquals(o.getETag(), putObjectResult.getETag());
        headObjectRequest = new HeadObjectRequest(bucketName, key);
        headObjectRequest.setUnmodifiedSinceConstraint(new Date(System.currentTimeMillis() + 3600 * 1000));
        o = ossClient.headObject(headObjectRequest);
        Assert.assertEquals(o.getETag(), putObjectResult.getETag());
        o = ossClient.headObject(bucketName, key);
        Assert.assertEquals(o.getETag(), putObjectResult.getETag());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) HeadObjectRequest(com.aliyun.oss.model.HeadObjectRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 17 with PutObjectResult

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

the class ObjectVersionTest method testPutObject.

@Test
public void testPutObject() {
    String key = "version-test-put-object";
    long inputStreamLength = 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        Assert.assertEquals(64, putResult.getVersionId().length());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 18 with PutObjectResult

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

the class ObjectVersionTest method testGetObjectMetadata.

@Test
public void testGetObjectMetadata() {
    String key = "version-test-get-object-metadata";
    long inputStreamLength1 = 512;
    long inputStreamLength2 = 1024;
    try {
        // Put object verison1
        InputStream instream = genFixedLengthInputStream(inputStreamLength1);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        Assert.assertEquals(64, putResult.getVersionId().length());
        String versionId1 = putResult.getVersionId();
        // Put object version2
        instream = genFixedLengthInputStream(inputStreamLength2);
        putResult = ossClient.putObject(bucketName, key, instream);
        Assert.assertNotNull(putResult.getVersionId());
        Assert.assertEquals(64, putResult.getVersionId().length());
        String versionId2 = putResult.getVersionId();
        // Test getObjectMetadata
        GenericRequest getObjectMetadataRequest = new GenericRequest(bucketName, key, versionId1);
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(getObjectMetadataRequest);
        Assert.assertNotNull(objectMetadata.getVersionId());
        Assert.assertEquals(64, objectMetadata.getVersionId().length());
        Assert.assertEquals(objectMetadata.getContentLength(), inputStreamLength1);
        // Test headObject
        HeadObjectRequest HeadObjectRequest = new HeadObjectRequest(bucketName, key, versionId1);
        objectMetadata = ossClient.headObject(HeadObjectRequest);
        Assert.assertNotNull(objectMetadata.getVersionId());
        Assert.assertEquals(64, objectMetadata.getVersionId().length());
        Assert.assertEquals(objectMetadata.getContentLength(), inputStreamLength1);
        // Test getSimplifiedObjectMeta
        GenericRequest getSimplifiedObjectMetaRequest = new GenericRequest(bucketName, key, versionId1);
        SimplifiedObjectMeta simplifiedObjectMeta = ossClient.getSimplifiedObjectMeta(getSimplifiedObjectMetaRequest);
        Assert.assertNotNull(simplifiedObjectMeta.getVersionId());
        Assert.assertEquals(64, simplifiedObjectMeta.getVersionId().length());
        Assert.assertEquals(simplifiedObjectMeta.getVersionId(), versionId1);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SimplifiedObjectMeta(com.aliyun.oss.model.SimplifiedObjectMeta) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) HeadObjectRequest(com.aliyun.oss.model.HeadObjectRequest) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GenericRequest(com.aliyun.oss.model.GenericRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 19 with PutObjectResult

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

the class ObjectVersionTest method testObjectAcl.

@Test
public void testObjectAcl() {
    String key = "version-test-set-object-acl";
    long inputStreamLength = 1024;
    try {
        InputStream instream = genFixedLengthInputStream(inputStreamLength);
        ObjectMetadata metadata = new ObjectMetadata();
        PutObjectResult putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        String versionId1 = putResult.getVersionId();
        instream = genFixedLengthInputStream(inputStreamLength);
        putResult = ossClient.putObject(bucketName, key, instream, metadata);
        Assert.assertNotNull(putResult.getVersionId());
        String versionId2 = putResult.getVersionId();
        // Set version1 'PublicRead'
        SetObjectAclRequest setObjectAclRequest = new SetObjectAclRequest(bucketName, key, versionId1, CannedAccessControlList.PublicRead);
        ossClient.setObjectAcl(setObjectAclRequest);
        // Set version2 'Private'
        setObjectAclRequest = new SetObjectAclRequest(bucketName, key, versionId2).withCannedACL(CannedAccessControlList.Private);
        ossClient.setObjectAcl(setObjectAclRequest);
        // Check acl of object version1
        GenericRequest genericRequest = new GenericRequest(bucketName, key, versionId1);
        ObjectAcl objectAcl = ossClient.getObjectAcl(genericRequest);
        Assert.assertEquals(ObjectPermission.PublicRead, objectAcl.getPermission());
        Assert.assertNotNull(objectAcl.getVersionId());
        Assert.assertEquals(64, objectAcl.getVersionId().length());
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SetObjectAclRequest(com.aliyun.oss.model.SetObjectAclRequest) ObjectAcl(com.aliyun.oss.model.ObjectAcl) PutObjectResult(com.aliyun.oss.model.PutObjectResult) InputStream(java.io.InputStream) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GenericRequest(com.aliyun.oss.model.GenericRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Example 20 with PutObjectResult

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

the class ObjectVersionTest method testObjectTagging.

@Test
public void testObjectTagging() {
    String key = "version-test-object-tagging";
    try {
        InputStream instream = genFixedLengthInputStream(1024);
        PutObjectResult putObjectResult = ossClient.putObject(bucketName, key, instream);
        String versionId = putObjectResult.getVersionId();
        Map<String, String> tags = new HashMap<String, String>(1);
        tags.put("tag1", "balabala");
        tags.put("tag2", "haha");
        SetObjectTaggingRequest setObjectTaggingRequest = new SetObjectTaggingRequest(bucketName, key, tags);
        setObjectTaggingRequest.setVersionId(versionId);
        ossClient.setObjectTagging(setObjectTaggingRequest);
        GenericRequest genericRequest = new GenericRequest(bucketName, key, versionId);
        TagSet tagSet = ossClient.getObjectTagging(genericRequest);
        Assert.assertEquals(tagSet.getAllTags().size(), 2);
        ossClient.deleteObjectTagging(genericRequest);
        tagSet = ossClient.getObjectTagging(genericRequest);
        Assert.assertEquals(tagSet.getAllTags().size(), 0);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) HashMap(java.util.HashMap) InputStream(java.io.InputStream) TagSet(com.aliyun.oss.model.TagSet) SetObjectTaggingRequest(com.aliyun.oss.model.SetObjectTaggingRequest) GenericRequest(com.aliyun.oss.model.GenericRequest) OSSException(com.aliyun.oss.OSSException) Test(org.junit.Test)

Aggregations

PutObjectResult (com.aliyun.oss.model.PutObjectResult)62 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 IOException (java.io.IOException)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)7 Date (java.util.Date)7 OSS (com.aliyun.oss.OSS)6 CopyObjectResult (com.aliyun.oss.model.CopyObjectResult)6 Ignore (org.junit.Ignore)6 CompleteMultipartUploadRequest (com.aliyun.oss.model.CompleteMultipartUploadRequest)5