Search in sources :

Example 1 with GetObjectResponseParser

use of com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method getObject.

/**
 * Pull an object from oss.
 */
public OSSObject getObject(GetObjectRequest getObjectRequest) throws OSSException, ClientException {
    assertParameterNotNull(getObjectRequest, "getObjectRequest");
    String bucketName = null;
    String key = null;
    RequestMessage request = null;
    if (!getObjectRequest.isUseUrlSignature()) {
        assertParameterNotNull(getObjectRequest, "getObjectRequest");
        bucketName = getObjectRequest.getBucketName();
        key = getObjectRequest.getKey();
        assertParameterNotNull(bucketName, "bucketName");
        assertParameterNotNull(key, "key");
        ensureBucketNameValid(bucketName);
        ensureObjectKeyValid(key);
        Map<String, String> headers = new HashMap<String, String>();
        populateGetObjectRequestHeaders(getObjectRequest, headers);
        Map<String, String> params = new HashMap<String, String>();
        populateResponseHeaderParameters(params, getObjectRequest.getResponseHeaders());
        String process = getObjectRequest.getProcess();
        if (process != null) {
            params.put(RequestParameters.SUBRESOURCE_PROCESS, process);
        }
        request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.GET).setBucket(bucketName).setKey(key).setHeaders(headers).setParameters(params).setOriginalRequest(getObjectRequest).build();
    } else {
        request = new RequestMessage(getObjectRequest, bucketName, key);
        request.setMethod(HttpMethod.GET);
        request.setAbsoluteUrl(getObjectRequest.getAbsoluteUri());
        request.setUseUrlSignature(true);
        request.setHeaders(getObjectRequest.getHeaders());
    }
    final ProgressListener listener = getObjectRequest.getProgressListener();
    OSSObject ossObject = null;
    try {
        publishProgress(listener, ProgressEventType.TRANSFER_STARTED_EVENT);
        ossObject = doOperation(request, new GetObjectResponseParser(bucketName, key), bucketName, key, true);
        InputStream instream = ossObject.getObjectContent();
        ProgressInputStream progressInputStream = new ProgressInputStream(instream, listener) {

            @Override
            protected void onEOF() {
                publishProgress(getListener(), ProgressEventType.TRANSFER_COMPLETED_EVENT);
            }
        };
        CRC64 crc = new CRC64();
        CheckedInputStream checkedInputstream = new CheckedInputStream(progressInputStream, crc);
        ossObject.setObjectContent(checkedInputstream);
    } catch (RuntimeException e) {
        publishProgress(listener, ProgressEventType.TRANSFER_FAILED_EVENT);
        throw e;
    }
    return ossObject;
}
Also used : OSSObject(com.aliyun.oss.model.OSSObject) CRC64(com.aliyun.oss.common.utils.CRC64) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ProgressInputStream(com.aliyun.oss.event.ProgressInputStream) 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) CheckedInputStream(java.util.zip.CheckedInputStream) ProgressListener(com.aliyun.oss.event.ProgressListener) GetObjectResponseParser(com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Aggregations

RequestMessage (com.aliyun.oss.common.comm.RequestMessage)1 RepeatableFileInputStream (com.aliyun.oss.common.comm.io.RepeatableFileInputStream)1 CRC64 (com.aliyun.oss.common.utils.CRC64)1 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)1 ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)1 ProgressListener (com.aliyun.oss.event.ProgressListener)1 GetObjectResponseParser (com.aliyun.oss.internal.ResponseParsers.GetObjectResponseParser)1 OSSObject (com.aliyun.oss.model.OSSObject)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 CheckedInputStream (java.util.zip.CheckedInputStream)1