Search in sources :

Example 6 with ClientException

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

the class OSSUdfOperation method uploadUdfImage.

/**
 * UDF Image
 */
public void uploadUdfImage(UploadUdfImageRequest uploadUdfImageRequest) throws OSSException, ClientException {
    assertParameterNotNull(uploadUdfImageRequest, "uploadUdfImageRequest");
    assertParameterNotNull(uploadUdfImageRequest.getUdfImage(), "udfImage");
    assertParameterNotNull(uploadUdfImageRequest.getName(), "udfImage");
    ensureBucketNameValid(uploadUdfImageRequest.getName());
    InputStream repeatableInputStream = null;
    try {
        repeatableInputStream = newRepeatableInputStream(uploadUdfImageRequest.getUdfImage());
    } 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>();
    Map<String, String> params = new LinkedHashMap<String, String>();
    params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE, null);
    params.put(RequestParameters.SUBRESOURCE_UDF_NAME, uploadUdfImageRequest.getName());
    if (uploadUdfImageRequest.getUdfImageDesc() != null) {
        params.put(RequestParameters.SUBRESOURCE_UDF_IMAGE_DESC, uploadUdfImageRequest.getUdfImageDesc());
    }
    RequestMessage httpRequest = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.POST).setHeaders(headers).setParameters(params).setInputStream(repeatableInputStream).setInputSize(determineInputStreamLength(repeatableInputStream, 0)).setOriginalRequest(uploadUdfImageRequest).build();
    ProgressListener listener = uploadUdfImageRequest.getProgressListener();
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        doOperation(httpRequest, emptyResponseParser, null, null, true);
        publishProgress(listener, ProgressEventType.TRANSFER_COMPLETED_EVENT);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
}
Also used : ProgressListener(com.aliyun.oss.event.ProgressListener) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) CheckedInputStream(java.util.zip.CheckedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) IOUtils.newRepeatableInputStream(com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream) InputStream(java.io.InputStream) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) IOException(java.io.IOException) ClientException(com.aliyun.oss.ClientException) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with ClientException

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

the class UploadPartCopySample method main.

public static void main(String[] args) throws IOException {
    /*
         * Constructs a client instance with your account for accessing OSS
         */
    client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        /*
             * Upload an object to your source bucket
             */
        System.out.println("Uploading a new object to OSS from a file\n");
        client.putObject(new PutObjectRequest(sourceBucketName, sourceKey, createSampleFile()));
        /*
             * Claim a new upload id for your target bucket
             */
        InitiateMultipartUploadRequest initiateMultipartUploadRequest = new InitiateMultipartUploadRequest(targetBucketName, targetKey);
        InitiateMultipartUploadResult initiateMultipartUploadResult = client.initiateMultipartUpload(initiateMultipartUploadRequest);
        String uploadId = initiateMultipartUploadResult.getUploadId();
        /*
             * Calculate how many parts to be divided
             */
        // 5MB
        final long partSize = 5 * 1024 * 1024L;
        ObjectMetadata metadata = client.getObjectMetadata(sourceBucketName, sourceKey);
        long objectSize = metadata.getContentLength();
        int partCount = (int) (objectSize / partSize);
        if (objectSize % 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 by copy mode
             */
        System.out.println("Begin to upload multiparts by copy mode to OSS\n");
        List<PartETag> partETags = new ArrayList<PartETag>();
        for (int i = 0; i < partCount; i++) {
            long startPos = i * partSize;
            long curPartSize = (i + 1 == partCount) ? (objectSize - startPos) : partSize;
            ;
            UploadPartCopyRequest uploadPartCopyRequest = new UploadPartCopyRequest(sourceBucketName, sourceKey, targetBucketName, targetKey);
            uploadPartCopyRequest.setUploadId(uploadId);
            uploadPartCopyRequest.setPartSize(curPartSize);
            uploadPartCopyRequest.setBeginIndex(startPos);
            uploadPartCopyRequest.setPartNumber(i + 1);
            UploadPartCopyResult uploadPartCopyResult = client.uploadPartCopy(uploadPartCopyRequest);
            System.out.println("\tPart#" + uploadPartCopyResult.getPartNumber() + " done\n");
            partETags.add(uploadPartCopyResult.getPartETag());
        }
        /*
             * Complete to upload multiparts
             */
        System.out.println("Completing to upload multiparts\n");
        CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(targetBucketName, targetKey, uploadId, partETags);
        client.completeMultipartUpload(completeMultipartUploadRequest);
        /*
             * Fetch the object that newly created at the step below.
             */
        System.out.println("Fetching an object");
        client.getObject(new GetObjectRequest(targetBucketName, targetKey), 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.
             */
        client.shutdown();
    }
}
Also used : InitiateMultipartUploadResult(com.aliyun.oss.model.InitiateMultipartUploadResult) InitiateMultipartUploadRequest(com.aliyun.oss.model.InitiateMultipartUploadRequest) ArrayList(java.util.ArrayList) OSSException(com.aliyun.oss.OSSException) PartETag(com.aliyun.oss.model.PartETag) UploadPartCopyResult(com.aliyun.oss.model.UploadPartCopyResult) UploadPartCopyRequest(com.aliyun.oss.model.UploadPartCopyRequest) ClientException(com.aliyun.oss.ClientException) ObjectMetadata(com.aliyun.oss.model.ObjectMetadata) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) File(java.io.File) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest) CompleteMultipartUploadRequest(com.aliyun.oss.model.CompleteMultipartUploadRequest)

Example 8 with ClientException

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

the class SimpleGetObjectSample 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 {
        /**
         * Note that there are two ways of uploading an object to your bucket, the one
         * by specifying an input stream as content source, the other by specifying a file.
         */
        /*
             * Upload an object to your bucket from an input stream
             */
        System.out.println("Uploading a new object to OSS from an input stream\n");
        String content = "Thank you for using Aliyun Object Storage Service";
        client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
        /*
             * Upload an object to your bucket from a file
             */
        System.out.println("Uploading a new object to OSS from a file\n");
        client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
        /*
             * Download an object from your bucket
             */
        System.out.println("Downloading an object");
        OSSObject object = client.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());
    } 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) GetObjectRequest(com.aliyun.oss.model.GetObjectRequest) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) PutObjectRequest(com.aliyun.oss.model.PutObjectRequest)

Example 9 with ClientException

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

the class UploadSample method main.

public static void main(String[] args) throws IOException {
    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    try {
        UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName, key);
        // The local file to upload---it must exist.
        uploadFileRequest.setUploadFile(uploadFile);
        // Sets the concurrent upload task number to 5.
        uploadFileRequest.setTaskNum(5);
        // Sets the part size to 1MB.
        uploadFileRequest.setPartSize(1024 * 1024 * 1);
        // Enables the checkpoint file. By default it's off.
        uploadFileRequest.setEnableCheckpoint(true);
        UploadFileResult uploadResult = ossClient.uploadFile(uploadFileRequest);
        CompleteMultipartUploadResult multipartUploadResult = uploadResult.getMultipartUploadResult();
        System.out.println(multipartUploadResult.getETag());
    } 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());
    } catch (Throwable e) {
        e.printStackTrace();
    } finally {
        ossClient.shutdown();
    }
}
Also used : UploadFileRequest(com.aliyun.oss.model.UploadFileRequest) OSSException(com.aliyun.oss.OSSException) UploadFileResult(com.aliyun.oss.model.UploadFileResult) CompleteMultipartUploadResult(com.aliyun.oss.model.CompleteMultipartUploadResult) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 10 with ClientException

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

the class AppendObjectSample 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 {
        /*
             * Append an object from specfied input stream, keep in mind that
             * position should be set to zero at first time.
             */
        String content = "Thank you for using Aliyun Object Storage Service";
        InputStream instream = new ByteArrayInputStream(content.getBytes());
        Long firstPosition = 0L;
        System.out.println("Begin to append object at position(" + firstPosition + ")");
        AppendObjectResult appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, instream).withPosition(0L));
        System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC() + "\n");
        /*
             * Continue to append the object from specfied file descriptor at last position
             */
        Long nextPosition = appendObjectResult.getNextPosition();
        System.out.println("Continue to append object at last position(" + nextPosition + "):");
        appendObjectResult = client.appendObject(new AppendObjectRequest(bucketName, key, createTempFile()).withPosition(nextPosition));
        System.out.println("\tNext position=" + appendObjectResult.getNextPosition() + ", CRC64=" + appendObjectResult.getObjectCRC());
        /*
             * View object type of the appendable object
             */
        OSSObject object = client.getObject(bucketName, key);
        System.out.println("\tObject type=" + object.getObjectMetadata().getObjectType() + "\n");
        // Do not forget to close object input stream if not use it any more
        object.getObjectContent().close();
        /*
             * Delete the appendable object
             */
        System.out.println("Deleting an appendable object");
        client.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.
             */
        client.shutdown();
    }
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AppendObjectRequest(com.aliyun.oss.model.AppendObjectRequest) OSSException(com.aliyun.oss.OSSException) ClientException(com.aliyun.oss.ClientException) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) AppendObjectResult(com.aliyun.oss.model.AppendObjectResult)

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