Search in sources :

Example 6 with ResponseHeaderHandlerChain

use of com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method copyPart.

/**
 * Copies a source object to a part of a multipart upload. To copy an
 * object, the caller's account must have read access to the source object
 * and write access to the destination bucket. </p>
 * <p>
 * If constraints are specified in the <code>CopyPartRequest</code> (e.g.
 * {@link CopyPartRequest#setMatchingETagConstraints(List)}) and are not
 * satisfied when Amazon S3 receives the request, this method returns
 * <code>null</code>. This method returns a non-null result under all other
 * circumstances.
 * </p>
 *
 * @param copyPartRequest The request object containing all the options for
 *            copying an Amazon S3 object.
 * @return A {@link CopyPartResult} object containing the information
 *         returned by Amazon S3 about the newly created object, or
 *         <code>null</code> if constraints were specified that weren't met
 *         when Amazon S3 attempted to copy the object.
 * @throws AmazonClientException If any errors are encountered in the client
 *             while making the request or handling the response.
 * @throws AmazonServiceException If any errors occurred in Amazon S3 while
 *             processing the request.
 * @see AmazonS3#copyObject(CopyObjectRequest)
 * @see AmazonS3#initiateMultipartUpload(InitiateMultipartUploadRequest)
 */
@Override
public CopyPartResult copyPart(CopyPartRequest copyPartRequest) {
    assertParameterNotNull(copyPartRequest.getSourceBucketName(), "The source bucket name must be specified when copying a part");
    assertParameterNotNull(copyPartRequest.getSourceKey(), "The source object key must be specified when copying a part");
    assertParameterNotNull(copyPartRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying a part");
    assertParameterNotNull(copyPartRequest.getUploadId(), "The upload id must be specified when copying a part");
    assertParameterNotNull(copyPartRequest.getDestinationKey(), "The destination object key must be specified when copying a part");
    assertParameterNotNull(copyPartRequest.getPartNumber(), "The part number must be specified when copying a part");
    final String destinationKey = copyPartRequest.getDestinationKey();
    final String destinationBucketName = copyPartRequest.getDestinationBucketName();
    final Request<CopyPartRequest> request = createRequest(destinationBucketName, destinationKey, copyPartRequest, HttpMethodName.PUT);
    populateRequestWithCopyPartParameters(request, copyPartRequest);
    request.addParameter("uploadId", copyPartRequest.getUploadId());
    request.addParameter("partNumber", Integer.toString(copyPartRequest.getPartNumber()));
    /*
         * We can't send a non-zero length Content-Length header if the user
         * specified it, otherwise it messes up the HTTP connection when the
         * remote server thinks there's more data to pull.
         */
    setZeroContentLength(request);
    CopyObjectResultHandler copyObjectResultHandler = null;
    try {
        @SuppressWarnings("unchecked") final ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new S3VersionHeaderHandler<CopyObjectResultHandler>());
        copyObjectResultHandler = invoke(request, handler, destinationBucketName, destinationKey);
    } catch (final AmazonS3Exception ase) {
        /*
             * If the request failed because one of the specified constraints
             * was not met (ex: matching ETag, modified since date, etc.), then
             * return null, so that users don't have to wrap their code in
             * try/catch blocks and check for this status code if they want to
             * use constraints.
             */
        if (ase.getStatusCode() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw ase;
    }
    /*
         * CopyPart has two failure modes: 1 - An HTTP error code is returned
         * and the error is processed like any other error response. 2 - An HTTP
         * 200 OK code is returned, but the response content contains an XML
         * error response. This makes it very difficult for the client runtime
         * to cleanly detect this case and handle it like any other error
         * response. We could extend the runtime to have a more
         * flexible/customizable definition of success/error (per request), but
         * it's probably overkill for this one special case.
         */
    if (copyObjectResultHandler.getErrorCode() != null) {
        final String errorCode = copyObjectResultHandler.getErrorCode();
        final String errorMessage = copyObjectResultHandler.getErrorMessage();
        final String requestId = copyObjectResultHandler.getErrorRequestId();
        final String hostId = copyObjectResultHandler.getErrorHostId();
        final AmazonS3Exception ase = new AmazonS3Exception(errorMessage);
        ase.setErrorCode(errorCode);
        ase.setErrorType(ErrorType.Service);
        ase.setRequestId(requestId);
        ase.setExtendedRequestId(hostId);
        ase.setServiceName(request.getServiceName());
        ase.setStatusCode(200);
        throw ase;
    }
    final CopyPartResult copyPartResult = new CopyPartResult();
    copyPartResult.setETag(copyObjectResultHandler.getETag());
    copyPartResult.setPartNumber(copyPartRequest.getPartNumber());
    copyPartResult.setLastModifiedDate(copyObjectResultHandler.getLastModified());
    copyPartResult.setVersionId(copyObjectResultHandler.getVersionId());
    copyPartResult.setSSEAlgorithm(copyObjectResultHandler.getSSEAlgorithm());
    copyPartResult.setSSECustomerAlgorithm(copyObjectResultHandler.getSSECustomerAlgorithm());
    copyPartResult.setSSECustomerKeyMd5(copyObjectResultHandler.getSSECustomerKeyMd5());
    return copyPartResult;
}
Also used : Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) CopyObjectResultHandler(com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Example 7 with ResponseHeaderHandlerChain

use of com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method initiateMultipartUpload.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#initiateMultipartUpload(com.amazonaws
     * .services.s3.model.InitiateMultipartUploadRequest)
     */
@Override
public InitiateMultipartUploadResult initiateMultipartUpload(InitiateMultipartUploadRequest initiateMultipartUploadRequest) throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(initiateMultipartUploadRequest, "The request parameter must be specified when initiating a multipart upload");
    assertParameterNotNull(initiateMultipartUploadRequest.getBucketName(), "The bucket name parameter must be specified when initiating a multipart upload");
    assertParameterNotNull(initiateMultipartUploadRequest.getKey(), "The key parameter must be specified when initiating a multipart upload");
    final Request<InitiateMultipartUploadRequest> request = createRequest(initiateMultipartUploadRequest.getBucketName(), initiateMultipartUploadRequest.getKey(), initiateMultipartUploadRequest, HttpMethodName.POST);
    request.addParameter("uploads", null);
    if (initiateMultipartUploadRequest.getStorageClass() != null) {
        request.addHeader(Headers.STORAGE_CLASS, initiateMultipartUploadRequest.getStorageClass().toString());
    }
    if (initiateMultipartUploadRequest.getRedirectLocation() != null) {
        request.addHeader(Headers.REDIRECT_LOCATION, initiateMultipartUploadRequest.getRedirectLocation());
    }
    if (initiateMultipartUploadRequest.getAccessControlList() != null) {
        addAclHeaders(request, initiateMultipartUploadRequest.getAccessControlList());
    } else if (initiateMultipartUploadRequest.getCannedACL() != null) {
        request.addHeader(Headers.S3_CANNED_ACL, initiateMultipartUploadRequest.getCannedACL().toString());
    }
    if (initiateMultipartUploadRequest.objectMetadata != null) {
        populateRequestMetadata(request, initiateMultipartUploadRequest.objectMetadata);
    }
    addHeaderIfNotNull(request, Headers.S3_TAGGING, urlEncodeTags(initiateMultipartUploadRequest.getTagging()));
    populateRequesterPaysHeader(request, initiateMultipartUploadRequest.isRequesterPays());
    // Populate the SSE-C parameters to the request header
    populateSSE_C(request, initiateMultipartUploadRequest.getSSECustomerKey());
    // Populate the SSE AWS KMS parameters to the request header
    populateSSE_KMS(request, initiateMultipartUploadRequest.getSSEAwsKeyManagementParams());
    // Be careful that we don't send the object's total size as the content
    // length for the InitiateMultipartUpload request.
    setZeroContentLength(request);
    // Set the request content to be empty (but not null) to force the
    // runtime to pass
    // any query params in the query string and not the request body, to
    // keep S3 happy.
    request.setContent(new ByteArrayInputStream(new byte[0]));
    @SuppressWarnings("unchecked") final ResponseHeaderHandlerChain<InitiateMultipartUploadResult> responseHandler = new ResponseHeaderHandlerChain<InitiateMultipartUploadResult>(// xml payload unmarshaller
    new Unmarshallers.InitiateMultipartUploadResultUnmarshaller(), // header handlers
    new ServerSideEncryptionHeaderHandler<InitiateMultipartUploadResult>());
    return invoke(request, responseHandler, initiateMultipartUploadRequest.getBucketName(), initiateMultipartUploadRequest.getKey());
}
Also used : Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) ByteArrayInputStream(java.io.ByteArrayInputStream) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Example 8 with ResponseHeaderHandlerChain

use of com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method copyObject.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#copyObject(com.amazonaws.services.
     * s3.model.CopyObjectRequest)
     */
@Override
public CopyObjectResult copyObject(CopyObjectRequest copyObjectRequest) throws AmazonClientException, AmazonServiceException {
    assertParameterNotNull(copyObjectRequest.getSourceBucketName(), "The source bucket name must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getSourceKey(), "The source object key must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getDestinationBucketName(), "The destination bucket name must be specified when copying an object");
    assertParameterNotNull(copyObjectRequest.getDestinationKey(), "The destination object key must be specified when copying an object");
    final String destinationKey = copyObjectRequest.getDestinationKey();
    final String destinationBucketName = copyObjectRequest.getDestinationBucketName();
    final Request<CopyObjectRequest> request = createRequest(destinationBucketName, destinationKey, copyObjectRequest, HttpMethodName.PUT);
    populateRequestWithCopyObjectParameters(request, copyObjectRequest);
    // Populate the SSE AWS KMS parameters to the request header
    populateSSE_KMS(request, copyObjectRequest.getSSEAwsKeyManagementParams());
    /*
         * We can't send a non-zero length Content-Length header if the user
         * specified it, otherwise it messes up the HTTP connection when the
         * remote server thinks there's more data to pull.
         */
    setZeroContentLength(request);
    CopyObjectResultHandler copyObjectResultHandler = null;
    try {
        @SuppressWarnings("unchecked") final ResponseHeaderHandlerChain<CopyObjectResultHandler> handler = new ResponseHeaderHandlerChain<CopyObjectResultHandler>(// xml payload unmarshaller
        new Unmarshallers.CopyObjectUnmarshaller(), // header handlers
        new ServerSideEncryptionHeaderHandler<CopyObjectResultHandler>(), new S3VersionHeaderHandler<CopyObjectResultHandler>(), new ObjectExpirationHeaderHandler<CopyObjectResultHandler>(), new S3RequesterChargedHeaderHandler<CopyObjectResultHandler>());
        copyObjectResultHandler = invoke(request, handler, destinationBucketName, destinationKey);
    } catch (final AmazonS3Exception ase) {
        /*
             * If the request failed because one of the specified constraints
             * was not met (ex: matching ETag, modified since date, etc.), then
             * return null, so that users don't have to wrap their code in
             * try/catch blocks and check for this status code if they want to
             * use constraints.
             */
        if (ase.getStatusCode() == Constants.FAILED_PRECONDITION_STATUS_CODE) {
            return null;
        }
        throw ase;
    }
    /*
         * CopyObject has two failure modes: 1 - An HTTP error code is returned
         * and the error is processed like any other error response. 2 - An HTTP
         * 200 OK code is returned, but the response content contains an XML
         * error response. This makes it very difficult for the client runtime
         * to cleanly detect this case and handle it like any other error
         * response. We could extend the runtime to have a more
         * flexible/customizable definition of success/error (per request), but
         * it's probably overkill for this one special case.
         */
    if (copyObjectResultHandler.getErrorCode() != null) {
        final String errorCode = copyObjectResultHandler.getErrorCode();
        final String errorMessage = copyObjectResultHandler.getErrorMessage();
        final String requestId = copyObjectResultHandler.getErrorRequestId();
        final String hostId = copyObjectResultHandler.getErrorHostId();
        final AmazonS3Exception ase = new AmazonS3Exception(errorMessage);
        ase.setErrorCode(errorCode);
        ase.setErrorType(ErrorType.Service);
        ase.setRequestId(requestId);
        ase.setExtendedRequestId(hostId);
        ase.setServiceName(request.getServiceName());
        ase.setStatusCode(200);
        throw ase;
    }
    // TODO: Might be nice to create this in our custom
    // S3VersionHeaderHandler
    final CopyObjectResult copyObjectResult = new CopyObjectResult();
    copyObjectResult.setETag(copyObjectResultHandler.getETag());
    copyObjectResult.setLastModifiedDate(copyObjectResultHandler.getLastModified());
    copyObjectResult.setVersionId(copyObjectResultHandler.getVersionId());
    copyObjectResult.setSSEAlgorithm(copyObjectResultHandler.getSSEAlgorithm());
    copyObjectResult.setSSECustomerAlgorithm(copyObjectResultHandler.getSSECustomerAlgorithm());
    copyObjectResult.setSSECustomerKeyMd5(copyObjectResultHandler.getSSECustomerKeyMd5());
    copyObjectResult.setExpirationTime(copyObjectResultHandler.getExpirationTime());
    copyObjectResult.setExpirationTimeRuleId(copyObjectResultHandler.getExpirationTimeRuleId());
    copyObjectResult.setRequesterCharged(copyObjectResultHandler.isRequesterCharged());
    return copyObjectResult;
}
Also used : Unmarshallers(com.amazonaws.services.s3.model.transform.Unmarshallers) CopyObjectResultHandler(com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CopyObjectResultHandler) ResponseHeaderHandlerChain(com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)

Aggregations

ResponseHeaderHandlerChain (com.amazonaws.services.s3.internal.ResponseHeaderHandlerChain)8 Unmarshallers (com.amazonaws.services.s3.model.transform.Unmarshallers)8 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CopyObjectResultHandler (com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CopyObjectResultHandler)2 AbortedException (com.amazonaws.AbortedException)1 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DeleteObjectTaggingHeaderHandler (com.amazonaws.services.s3.internal.DeleteObjectTaggingHeaderHandler)1 DeleteObjectsResponse (com.amazonaws.services.s3.internal.DeleteObjectsResponse)1 GetObjectTaggingResponseHeaderHandler (com.amazonaws.services.s3.internal.GetObjectTaggingResponseHeaderHandler)1 SetObjectTaggingResponseHeaderHandler (com.amazonaws.services.s3.internal.SetObjectTaggingResponseHeaderHandler)1 MultiObjectDeleteXmlFactory (com.amazonaws.services.s3.model.transform.MultiObjectDeleteXmlFactory)1 ObjectTaggingXmlFactory (com.amazonaws.services.s3.model.transform.ObjectTaggingXmlFactory)1 CompleteMultipartUploadHandler (com.amazonaws.services.s3.model.transform.XmlResponsesSaxParser.CompleteMultipartUploadHandler)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1