Search in sources :

Example 41 with ClientException

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

the class MultipartUploadSample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
    conf.setIdleConnectionTime(1000);
    client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, conf);
    try {
        /*
             * Claim a upload id firstly
             */
        String uploadId = claimUploadId();
        System.out.println("Claiming a new upload id " + uploadId + "\n");
        /*
             * Calculate how many parts to be divided
             */
        // 5MB
        final long partSize = 5 * 1024 * 1024L;
        final File sampleFile = createSampleFile();
        long fileLength = sampleFile.length();
        int partCount = (int) (fileLength / partSize);
        if (fileLength % partSize != 0) {
            partCount++;
        }
        if (partCount > 10000) {
            throw new RuntimeException("Total parts count should not exceed 10000");
        } else {
            System.out.println("Total parts count " + partCount + "\n");
        }
        /*
             * Upload multiparts to your bucket
             */
        System.out.println("Begin to upload multiparts to OSS from a file\n");
        for (int i = 0; i < partCount; i++) {
            long startPos = i * partSize;
            long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
            executorService.execute(new PartUploader(sampleFile, startPos, curPartSize, i + 1, uploadId));
        }
        /*
             * Waiting for all parts finished
             */
        executorService.shutdown();
        while (!executorService.isTerminated()) {
            try {
                executorService.awaitTermination(5, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        /*
             * Verify whether all parts are finished
             */
        if (partETags.size() != partCount) {
            throw new IllegalStateException("Upload multiparts fail due to some parts are not finished yet");
        } else {
            System.out.println("Succeed to complete multiparts into an object named " + key + "\n");
        }
        /*
             * View all parts uploaded recently
             */
        listAllParts(uploadId);
        /*
             * Complete to upload multiparts
             */
        completeMultipartUpload(uploadId);
        /*
             * Fetch the object that newly created at the step below.
             */
        System.out.println("Fetching an object");
        client.getObject(new GetObjectRequest(bucketName, key), new File(localFilePath));
    } 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.
             */
        if (client != null) {
            client.shutdown();
        }
    }
}
Also used : OSSException(com.aliyun.oss.OSSException) ClientBuilderConfiguration(com.aliyun.oss.ClientBuilderConfiguration) ClientException(com.aliyun.oss.ClientException) File(java.io.File) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 42 with ClientException

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

the class OSSObjectOperation method writeObjectInternal.

private <RequestType extends PutObjectRequest, ResponseType> ResponseType writeObjectInternal(WriteMode mode, RequestType originalRequest, ResponseParser<ResponseType> responseParser) {
    final String bucketName = originalRequest.getBucketName();
    final String key = originalRequest.getKey();
    InputStream originalInputStream = originalRequest.getInputStream();
    ObjectMetadata metadata = originalRequest.getMetadata();
    if (metadata == null) {
        metadata = new ObjectMetadata();
    }
    assertParameterNotNull(bucketName, "bucketName");
    assertParameterNotNull(key, "key");
    ensureBucketNameValid(bucketName);
    ensureObjectKeyValid(key);
    ensureCallbackValid(originalRequest.getCallback());
    InputStream repeatableInputStream = null;
    if (originalRequest.getFile() != null) {
        File toUpload = originalRequest.getFile();
        if (!checkFile(toUpload)) {
            getLog().info("Illegal file path: " + toUpload.getPath());
            throw new ClientException("Illegal file path: " + toUpload.getPath());
        }
        metadata.setContentLength(toUpload.length());
        if (metadata.getContentType() == null) {
            metadata.setContentType(Mimetypes.getInstance().getMimetype(toUpload, key));
        }
        try {
            repeatableInputStream = new RepeatableFileInputStream(toUpload);
        } catch (IOException ex) {
            logException("Cannot locate file to upload: ", ex);
            throw new ClientException("Cannot locate file to upload: ", ex);
        }
    } else {
        assertTrue(originalInputStream != null, "Please specify input stream or file to upload");
        if (metadata.getContentType() == null) {
            metadata.setContentType(Mimetypes.getInstance().getMimetype(key));
        }
        try {
            repeatableInputStream = newRepeatableInputStream(originalInputStream);
        } catch (IOException ex) {
            logException("Cannot wrap to repeatable input stream: ", ex);
            throw new ClientException("Cannot wrap to repeatable input stream: ", ex);
        }
    }
    Map<String, String> headers = new HashMap<String, String>();
    populateRequestMetadata(headers, metadata);
    populateRequestCallback(headers, originalRequest.getCallback());
    Map<String, String> params = new LinkedHashMap<String, String>();
    populateWriteObjectParams(mode, originalRequest, params);
    RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(WriteMode.getMappingMethod(mode)).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, metadata.getContentLength())).setOriginalRequest(originalRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new OSSCallbackErrorResponseHandler());
    final ProgressListener listener = originalRequest.getProgressListener();
    ResponseType result = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        if (originalRequest.getCallback() == null) {
            result = doOperation(httpRequest, responseParser, bucketName, key, true);
        } else {
            result = doOperation(httpRequest, responseParser, bucketName, key, true, null, reponseHandlers);
        }
        publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    return result;
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RepeatableFileInputStream(com.aliyun.oss.common.comm.io.RepeatableFileInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RepeatableFileInputStream(com.aliyun.oss.common.comm.io.RepeatableFileInputStream) LinkedHashMap(java.util.LinkedHashMap) ProgressListener(com.aliyun.oss.event.ProgressListener) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) IOUtils.checkFile(com.aliyun.oss.common.utils.IOUtils.checkFile) File(java.io.File)

Example 43 with ClientException

use of com.aliyun.oss.ClientException 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 44 with ClientException

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

the class CreateFolderSample 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 {
        /*
             * Create an empty folder without request body, note that the key must be 
             * suffixed with a slash
             */
        final String keySuffixWithSlash = "MyObjectKey/";
        client.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
        System.out.println("Creating an empty folder " + keySuffixWithSlash + "\n");
        /*
             * Verify whether the size of the empty folder is zero 
             */
        OSSObject object = client.getObject(bucketName, keySuffixWithSlash);
        System.out.println("Size of the empty folder '" + object.getKey() + "' is " + object.getObjectMetadata().getContentLength());
        object.getObjectContent().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 {
        /*
             * Do not forget to shut down the client finally to release all allocated resources.
             */
        client.shutdown();
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 45 with ClientException

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

ClientException (com.aliyun.oss.ClientException)48 OSSException (com.aliyun.oss.OSSException)27 OSSClientBuilder (com.aliyun.oss.OSSClientBuilder)19 ByteArrayInputStream (java.io.ByteArrayInputStream)17 OSS (com.aliyun.oss.OSS)16 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