Search in sources :

Example 6 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method getObjectMetadata.

/**
 * Get object matadata.
 */
public ObjectMetadata getObjectMetadata(GenericRequest genericRequest) throws OSSException, ClientException {
    assertParameterNotNull(genericRequest, "genericRequest");
    String bucketName = genericRequest.getBucketName();
    String key = genericRequest.getKey();
    assertParameterNotNull(bucketName, "bucketName");
    assertParameterNotNull(key, "key");
    ensureBucketNameValid(bucketName);
    ensureObjectKeyValid(key);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.HEAD).setBucket(bucketName).setKey(key).setOriginalRequest(genericRequest).build();
    List<ResponseHandler> reponseHandlers = new ArrayList<ResponseHandler>();
    reponseHandlers.add(new ResponseHandler() {

        @Override
        public void handle(ResponseMessage response) throws ServiceException, ClientException {
            if (response.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
                safeCloseResponse(response);
                throw ExceptionFactory.createOSSException(response.getHeaders().get(OSSHeaders.OSS_HEADER_REQUEST_ID), OSSErrorCode.NO_SUCH_KEY, OSS_RESOURCE_MANAGER.getString("NoSuchKey"));
            }
        }
    });
    return doOperation(request, getObjectMetadataResponseParser, bucketName, key, true, null, reponseHandlers);
}
Also used : ResponseHandler(com.aliyun.oss.common.comm.ResponseHandler) ServiceException(com.aliyun.oss.ServiceException) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) ArrayList(java.util.ArrayList) ResponseMessage(com.aliyun.oss.common.comm.ResponseMessage) ClientException(com.aliyun.oss.ClientException)

Example 7 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method headObject.

/**
 * Check if the object key exists under the specified bucket.
 */
public void headObject(HeadObjectRequest headObjectRequest) throws OSSException, ClientException {
    assertParameterNotNull(headObjectRequest, "headObjectRequest");
    String bucketName = headObjectRequest.getBucketName();
    String key = headObjectRequest.getKey();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    Map<String, String> headers = new HashMap<String, String>();
    addDateHeader(headers, OSSHeaders.HEAD_OBJECT_IF_MODIFIED_SINCE, headObjectRequest.getModifiedSinceConstraint());
    addDateHeader(headers, OSSHeaders.HEAD_OBJECT_IF_UNMODIFIED_SINCE, headObjectRequest.getUnmodifiedSinceConstraint());
    addStringListHeader(headers, OSSHeaders.HEAD_OBJECT_IF_MATCH, headObjectRequest.getMatchingETagConstraints());
    addStringListHeader(headers, OSSHeaders.HEAD_OBJECT_IF_NONE_MATCH, headObjectRequest.getNonmatchingETagConstraints());
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.HEAD).setBucket(bucketName).setKey(key).setHeaders(headers).setOriginalRequest(headObjectRequest).build();
    doOperation(request, emptyResponseParser, bucketName, key);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Example 8 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method setObjectAcl.

public void setObjectAcl(SetObjectAclRequest setObjectAclRequest) throws OSSException, ClientException {
    assertParameterNotNull(setObjectAclRequest, "setObjectAclRequest");
    String bucketName = setObjectAclRequest.getBucketName();
    String key = setObjectAclRequest.getKey();
    CannedAccessControlList cannedAcl = setObjectAclRequest.getCannedACL();
    assertParameterNotNull(bucketName, "bucketName");
    ensureBucketNameValid(bucketName);
    assertParameterNotNull(key, "key");
    ensureObjectKeyValid(key);
    assertParameterNotNull(cannedAcl, "cannedAcl");
    Map<String, String> headers = new HashMap<String, String>();
    headers.put(OSSHeaders.OSS_OBJECT_ACL, cannedAcl.toString());
    Map<String, String> params = new HashMap<String, String>();
    params.put(SUBRESOURCE_ACL, null);
    RequestMessage request = new OSSRequestMessageBuilder(getInnerClient()).setEndpoint(getEndpoint()).setMethod(HttpMethod.PUT).setBucket(bucketName).setKey(key).setParameters(params).setHeaders(headers).setOriginalRequest(setObjectAclRequest).build();
    doOperation(request, emptyResponseParser, bucketName, key);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) RequestMessage(com.aliyun.oss.common.comm.RequestMessage) CannedAccessControlList(com.aliyun.oss.model.CannedAccessControlList)

Example 9 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage 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)

Example 10 with RequestMessage

use of com.aliyun.oss.common.comm.RequestMessage in project aliyun-oss-java-sdk by aliyun.

the class OSSObjectOperation method putObject.

/**
 * Upload input stream to oss by using url signature.
 */
public PutObjectResult putObject(URL signedUrl, InputStream requestContent, long contentLength, Map<String, String> requestHeaders, boolean useChunkEncoding) throws OSSException, ClientException {
    assertParameterNotNull(signedUrl, "signedUrl");
    assertParameterNotNull(requestContent, "requestContent");
    if (requestHeaders == null) {
        requestHeaders = new HashMap<String, String>();
    }
    RequestMessage request = new RequestMessage(null, null);
    request.setMethod(HttpMethod.PUT);
    request.setAbsoluteUrl(signedUrl);
    request.setUseUrlSignature(true);
    request.setContent(requestContent);
    request.setContentLength(determineInputStreamLength(requestContent, contentLength, useChunkEncoding));
    request.setHeaders(requestHeaders);
    request.setUseChunkEncoding(useChunkEncoding);
    PutObjectResult result = null;
    if (requestHeaders.get(OSSHeaders.OSS_HEADER_CALLBACK) == null) {
        result = doOperation(request, putObjectReponseParser, null, null, true);
    } else {
        result = doOperation(request, putObjectProcessReponseParser, null, null, true);
    }
    if (isCrcCheckEnabled()) {
        OSSUtils.checkChecksum(result.getClientCRC(), result.getServerCRC(), result.getRequestId());
    }
    return result;
}
Also used : PutObjectResult(com.aliyun.oss.model.PutObjectResult) RequestMessage(com.aliyun.oss.common.comm.RequestMessage)

Aggregations

RequestMessage (com.aliyun.oss.common.comm.RequestMessage)98 LinkedHashMap (java.util.LinkedHashMap)88 HashMap (java.util.HashMap)78 ByteArrayInputStream (java.io.ByteArrayInputStream)23 ClientException (com.aliyun.oss.ClientException)9 ClientConfiguration (com.aliyun.oss.ClientConfiguration)6 ResponseHandler (com.aliyun.oss.common.comm.ResponseHandler)6 InputStream (java.io.InputStream)6 ResponseMessage (com.aliyun.oss.common.comm.ResponseMessage)5 IOUtils.newRepeatableInputStream (com.aliyun.oss.common.utils.IOUtils.newRepeatableInputStream)5 ProgressListener (com.aliyun.oss.event.ProgressListener)5 ArrayList (java.util.ArrayList)5 ExecutionContext (com.aliyun.oss.common.comm.ExecutionContext)4 ProgressInputStream (com.aliyun.oss.event.ProgressInputStream)4 URI (java.net.URI)4 CheckedInputStream (java.util.zip.CheckedInputStream)4 ServiceException (com.aliyun.oss.ServiceException)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Credentials (com.aliyun.oss.common.auth.Credentials)2