Search in sources :

Example 21 with PutObjectRequest

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

the class GetObjectTest method testGetObjectMetadataWithIllegalExpires.

@Test
public void testGetObjectMetadataWithIllegalExpires() {
    final String key = "get-object-with-illegal-expires";
    // 128KB
    final long inputStreamLength = 128 * 1024;
    final String illegalExpires = "2015-10-01 00:00:00";
    try {
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.EXPIRES, illegalExpires);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, genFixedLengthInputStream(inputStreamLength), metadata);
        ossClient.putObject(putObjectRequest);
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        OSSObject o = ossClient.getObject(getObjectRequest);
        try {
            o.getObjectMetadata().getExpirationTime();
            Assert.fail("Get expiration time should not be successful.");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ParseException);
            Assert.assertEquals("Unparseable date: \"2015-10-01 00:00:00\"", e.getMessage());
        }
        String rawExpiresValue = o.getObjectMetadata().getRawExpiresValue();
        Assert.assertEquals(illegalExpires, rawExpiresValue);
        metadata = ossClient.getObjectMetadata(bucketName, key);
        try {
            metadata.getExpirationTime();
            Assert.fail("Get expiration time should not be successful.");
        } catch (Exception e) {
            Assert.assertTrue(e instanceof ParseException);
            Assert.assertEquals("Unparseable date: \"2015-10-01 00:00:00\"", e.getMessage());
        }
        rawExpiresValue = o.getObjectMetadata().getRawExpiresValue();
        Assert.assertEquals(illegalExpires, rawExpiresValue);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ParseException(java.text.ParseException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) OSSException(com.aliyun.oss.OSSException) ParseException(java.text.ParseException) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest 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());
    } 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 23 with PutObjectRequest

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

the class CallbackSample method main.

public static void main(String[] args) throws IOException {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        String content = "Hello OSS";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, "key", new ByteArrayInputStream(content.getBytes()));
        Callback callback = new Callback();
        callback.setCallbackUrl(callbackUrl);
        callback.setCallbackHost("oss-cn-hangzhou.aliyuncs.com");
        callback.setCallbackBody("{\\\"bucket\\\":${bucket},\\\"object\\\":${object}," + "\\\"mimeType\\\":${mimeType},\\\"size\\\":${size}," + "\\\"my_var1\\\":${x:var1},\\\"my_var2\\\":${x:var2}}");
        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[1024];
        putObjectResult.getResponse().getContent().read(buffer);
        putObjectResult.getResponse().getContent().close();
    } 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 {
        ossClient.shutdown();
    }
}
Also used : Callback(com.aliyun.oss.model.Callback) ByteArrayInputStream(java.io.ByteArrayInputStream) PutObjectResult(com.aliyun.oss.model.PutObjectResult) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 24 with PutObjectRequest

use of com.aliyun.oss.model.PutObjectRequest 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

PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)24 OSSException (com.aliyun.oss.OSSException)18 Test (org.junit.Test)14 OSSObject (com.aliyun.oss.model.OSSObject)13 PutObjectResult (com.aliyun.oss.model.PutObjectResult)9 TestUtils.genFixedLengthInputStream (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthInputStream)8 Callback (com.aliyun.oss.model.Callback)8 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)8 InputStream (java.io.InputStream)8 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)7 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)7 IOException (java.io.IOException)6 ClientException (com.aliyun.oss.ClientException)5 OSS (com.aliyun.oss.OSS)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 TestUtils.genFixedLengthFile (com.aliyun.oss.integrationtests.TestUtils.genFixedLengthFile)3 ParseException (java.text.ParseException)3 TestUtils.genRandomLengthFile (com.aliyun.oss.integrationtests.TestUtils.genRandomLengthFile)2 TestUtils.removeFile (com.aliyun.oss.integrationtests.TestUtils.removeFile)2