Example usage for com.amazonaws.services.s3.model GetObjectRequest setMatchingETagConstraints

List of usage examples for com.amazonaws.services.s3.model GetObjectRequest setMatchingETagConstraints

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model GetObjectRequest setMatchingETagConstraints.

Prototype

public void setMatchingETagConstraints(List<String> eTagList) 

Source Link

Document

Sets the optional list of ETag constraints that when present must include a match for the object's current ETag in order for this request to be executed.

Usage

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

@Override
public GetObjectExtendedResponseType getObjectExtended(final GetObjectExtendedType request) throws S3Exception {
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;

    Boolean getMetaData = request.getGetMetaData();
    Long byteRangeStart = request.getByteRangeStart();
    Long byteRangeEnd = request.getByteRangeEnd();
    Date ifModifiedSince = request.getIfModifiedSince();
    Date ifUnmodifiedSince = request.getIfUnmodifiedSince();
    String ifMatch = request.getIfMatch();
    String ifNoneMatch = request.getIfNoneMatch();

    GetObjectRequest getRequest = new GetObjectRequest(request.getBucket(), request.getKey());
    if (byteRangeStart == null) {
        byteRangeStart = 0L;//ww  w.ja  v a 2  s.com
    }
    if (byteRangeEnd != null) {
        getRequest.setRange(byteRangeStart, byteRangeEnd);
    }
    if (getMetaData != null) {
        //Get object metadata
    }
    if (ifModifiedSince != null) {
        getRequest.setModifiedSinceConstraint(ifModifiedSince);
    }
    if (ifUnmodifiedSince != null) {
        getRequest.setUnmodifiedSinceConstraint(ifUnmodifiedSince);
    }
    if (ifMatch != null) {
        List matchList = new ArrayList();
        matchList.add(ifMatch);
        getRequest.setMatchingETagConstraints(matchList);
    }
    if (ifNoneMatch != null) {
        List nonMatchList = new ArrayList();
        nonMatchList.add(ifNoneMatch);
        getRequest.setNonmatchingETagConstraints(nonMatchList);
    }
    try {
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();
        S3Object response = s3Client.getObject(getRequest);

        GetObjectExtendedResponseType reply = request.getReply();
        populateResponseMetadata(reply, response.getObjectMetadata());
        reply.setDataInputStream(response.getObjectContent());
        reply.setByteRangeStart(request.getByteRangeStart());
        reply.setByteRangeEnd(request.getByteRangeEnd());
        return reply;
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
}

From source file:com.smoketurner.pipeline.application.core.AmazonS3Downloader.java

License:Apache License

/**
 * Retrieves a file from S3//w  ww  . j  a v  a 2  s.c  om
 *
 * @param record
 *            S3 event notification record to download
 * @return S3 object
 * @throws AmazonS3ConstraintException
 *             if the etag constraints weren't met
 * @throws AmazonS3ZeroSizeException
 *             if the file size of the object is zero
 */
public S3Object fetch(@Nonnull final S3EventNotificationRecord record)
        throws AmazonS3ConstraintException, AmazonS3ZeroSizeException {
    final AmazonS3Object object = converter.convert(Objects.requireNonNull(record));

    final GetObjectRequest request = new GetObjectRequest(object.getBucketName(), object.getKey());
    object.getVersionId().ifPresent(request::setVersionId);
    object.getETag().ifPresent(etag -> request.setMatchingETagConstraints(Collections.singletonList(etag)));

    LOGGER.debug("Fetching key: {}/{}", object.getBucketName(), object.getKey());

    final S3Object download;
    try {
        download = s3.getObject(request);
    } catch (AmazonServiceException e) {
        LOGGER.error("Service error while fetching object from S3", e);
        throw e;
    } catch (AmazonClientException e) {
        LOGGER.error("Client error while fetching object from S3", e);
        throw e;
    }

    if (download == null) {
        LOGGER.error("eTag from object did not match for key: {}/{}", object.getBucketName(), object.getKey());
        throw new AmazonS3ConstraintException(object.getKey());
    }

    final long contentLength = download.getObjectMetadata().getContentLength();
    if (contentLength < 1) {
        try {
            download.close();
        } catch (IOException e) {
            LOGGER.error(String.format("Failed to close S3 stream for key: %s/%s", download.getBucketName(),
                    download.getKey()), e);
        }

        LOGGER.debug("Object size is zero for key: {}/{}", download.getBucketName(), download.getKey());
        throw new AmazonS3ZeroSizeException(object.getKey());
    }

    LOGGER.debug("Streaming key ({} bytes): {}/{}", contentLength, download.getBucketName(), download.getKey());

    return download;
}