Search in sources :

Example 46 with ClientException

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

the class ObjectMetaSample 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);
    try {
        ObjectMetadata meta = new ObjectMetadata();
        // Sets the content type.
        meta.setContentType("text/plain");
        // Sets the MD5 data---please update it with the actual value.
        meta.setContentMD5("");
        // Sets the custom metadata.
        meta.addUserMetadata("meta", "meta-value");
        // Uploads the file
        ossClient.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()), meta);
        // Gets the object metadata information.
        ObjectMetadata metadata = ossClient.getObjectMetadata(bucketName, key);
        System.out.println(metadata.getContentType());
        System.out.println(metadata.getLastModified());
        System.out.println(metadata.getUserMetadata().get("meta"));
        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 : ByteArrayInputStream(java.io.ByteArrayInputStream) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 47 with ClientException

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

the class DeleteObjectsSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        /*
             * Batch put objects into the bucket
             */
        final String content = "Thank you for using Aliyun Object Storage Service";
        final String keyPrefix = "MyObjectKey";
        List<String> keys = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            String key = keyPrefix + i;
            InputStream instream = new ByteArrayInputStream(content.getBytes());
            client.putObject(bucketName, key, instream);
            System.out.println("Succeed to put object " + key);
            keys.add(key);
        }
        System.out.println();
        /*
             * Delete all objects uploaded recently under the bucket
             */
        System.out.println("\nDeleting all objects:");
        DeleteObjectsResult deleteObjectsResult = client.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
        for (String object : deletedObjects) {
            System.out.println("\t" + object);
        }
        System.out.println();
    } 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.
             */
        client.shutdown();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) DeleteObjectsResult(com.aliyun.oss.model.DeleteObjectsResult) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) DeleteObjectsRequest(com.aliyun.oss.model.DeleteObjectsRequest)

Aggregations

ClientException (com.aliyun.oss.ClientException)47 OSSException (com.aliyun.oss.OSSException)27 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 OSS (com.aliyun.oss.OSS)15 IOException (java.io.IOException)12 InputStream (java.io.InputStream)12 Test (org.junit.Test)11 RequestMessage (com.aliyun.oss.common.comm.RequestMessage)9 OSSObject (com.aliyun.oss.model.OSSObject)9 ArrayList (java.util.ArrayList)9 ObjectMetadata (com.aliyun.oss.model.ObjectMetadata)8 File (java.io.File)7 GetObjectRequest (com.aliyun.oss.model.GetObjectRequest)6 ObjectListing (com.aliyun.oss.model.ObjectListing)5 PutObjectRequest (com.aliyun.oss.model.PutObjectRequest)5 ClientConfiguration (com.aliyun.oss.ClientConfiguration)4 ServiceException (com.aliyun.oss.ServiceException)4 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)4