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

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

Introduction

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

Prototype

public String getKey() 

Source Link

Document

Gets the key under which the object to be downloaded is stored.

Usage

From source file:com.davidsoergel.s3napback.StreamingTransferManager.java

License:Apache License

private Download download(final GetObjectRequest getObjectRequest, final BufferedOutputStream os,
        final TransferStateChangeListener stateListener) {

    appendUserAgent(getObjectRequest, USER_AGENT);

    String description = "Downloading from " + getObjectRequest.getBucketName() + "/"
            + getObjectRequest.getKey();

    // Add our own transfer progress listener
    TransferProgressImpl transferProgress = new TransferProgressImpl();
    ProgressListenerChain listenerChain = new ProgressListenerChain(
            new TransferProgressUpdatingListener(transferProgress), getObjectRequest.getProgressListener());
    getObjectRequest.setProgressListener(listenerChain);

    final S3Object s3Object = s3.getObject(getObjectRequest);
    final DownloadImpl download = new DownloadImpl(description, transferProgress, listenerChain, s3Object,
            stateListener);//from  w w  w . jav a2 s . c  o  m

    // null is returned when constraints aren't met
    if (s3Object == null) {
        download.setState(Transfer.TransferState.Canceled);
        download.setMonitor(new DownloadMonitor(download, null));
        return download;
    }

    long contentLength = s3Object.getObjectMetadata().getContentLength();
    if (getObjectRequest.getRange() != null && getObjectRequest.getRange().length == 2) {
        long startingByte = getObjectRequest.getRange()[0];
        long lastByte = getObjectRequest.getRange()[1];
        contentLength = lastByte - startingByte;
    }
    transferProgress.setTotalBytesToTransfer(contentLength);

    Future<?> future = threadPool.submit(new Callable<Object>() {
        //@Override
        public Object call() throws Exception {
            try {
                download.setState(Transfer.TransferState.InProgress);
                StreamingServiceUtils.downloadObjectToStream(s3Object, os);
                download.setState(Transfer.TransferState.Completed);
                return true;
            } catch (Exception e) {
                // Downloads aren't allowed to move from canceled to failed
                if (download.getState() != Transfer.TransferState.Canceled) {
                    download.setState(Transfer.TransferState.Failed);
                }
                throw e;
            }
        }
    });
    download.setMonitor(new DownloadMonitor(download, future));

    return download;
}

From source file:com.nike.cerberus.config.CmsEnvPropertiesLoader.java

License:Apache License

private String getObject(String path) {
    final GetObjectRequest request = new GetObjectRequest(bucketName, path);

    try {/*  w w  w  . j a v  a  2s .  c o  m*/
        S3Object s3Object = s3Client.getObject(request);
        InputStream object = s3Object.getObjectContent();
        return IOUtils.toString(object, Charset.defaultCharset());
    } catch (AmazonServiceException ase) {
        if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
            final String errorMessage = String.format("The S3 object doesn't exist. Bucket: %s, Key: %s",
                    bucketName, request.getKey());
            logger.debug(errorMessage);
            throw new IllegalStateException(errorMessage);
        } else {
            logger.error("Unexpected error communicating with AWS.", ase);
            throw ase;
        }
    } catch (IOException e) {
        String errorMessage = String.format(
                "Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", bucketName,
                request.getKey(), Charset.defaultCharset());
        logger.error(errorMessage);
        throw new IllegalStateException(errorMessage, e);
    }
}

From source file:com.nike.cerberus.service.S3StoreService.java

License:Apache License

public Optional<String> get(String path) {
    GetObjectRequest request = new GetObjectRequest(s3Bucket, getFullPath(path));

    try {/* w w w  .j  a va 2  s.c  om*/
        S3Object s3Object = s3Client.getObject(request);
        InputStream object = s3Object.getObjectContent();
        return Optional.of(IOUtils.toString(object, ConfigConstants.DEFAULT_ENCODING));
    } catch (AmazonServiceException ase) {
        if (StringUtils.equalsIgnoreCase(ase.getErrorCode(), "NoSuchKey")) {
            logger.debug(String.format("The S3 object doesn't exist. Bucket: %s, Key: %s", s3Bucket,
                    request.getKey()));
            return Optional.empty();
        } else {
            logger.error("Unexpected error communicating with AWS.", ase);
            throw ase;
        }
    } catch (IOException e) {
        String errorMessage = String.format(
                "Unable to read contents of S3 object. Bucket: %s, Key: %s, Expected Encoding: %s", s3Bucket,
                request.getKey(), ConfigConstants.DEFAULT_ENCODING);
        logger.error(errorMessage);
        throw new UnexpectedDataEncodingException(errorMessage, e);
    }
}

From source file:edu.si.services.beans.cameratrap.AmazonS3ClientMock.java

License:Apache License

@Override
public S3Object getObject(GetObjectRequest getObjectRequest)
        throws AmazonClientException, AmazonServiceException {
    return getObject(getObjectRequest.getBucketName(), getObjectRequest.getKey());
}

From source file:org.elasticsearch.cloud.aws.blobstore.MockAmazonS3.java

License:Apache License

@Override
public S3Object getObject(GetObjectRequest getObjectRequest)
        throws AmazonClientException, AmazonServiceException {
    // in ESBlobStoreContainerTestCase.java, the prefix is empty,
    // so the key and blobName are equivalent to each other
    String blobName = getObjectRequest.getKey();

    if (!blobs.containsKey(blobName)) {
        throw new AmazonS3Exception("[" + blobName + "] does not exist.");
    }/*from  www . j  ava2  s.c  om*/

    // the HTTP request attribute is irrelevant for reading
    S3ObjectInputStream stream = new S3ObjectInputStream(blobs.get(blobName), null, false);
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(stream);
    return s3Object;
}

From source file:org.elasticsearch.repositories.s3.MockAmazonS3.java

License:Apache License

@Override
public S3Object getObject(GetObjectRequest getObjectRequest)
        throws AmazonClientException, AmazonServiceException {
    simulateS3SocketConnection();//from   w ww.  ja  v a2  s  . c  o  m
    // in ESBlobStoreContainerTestCase.java, the prefix is empty,
    // so the key and blobName are equivalent to each other
    String blobName = getObjectRequest.getKey();

    if (!blobs.containsKey(blobName)) {
        throw new AmazonS3Exception("[" + blobName + "] does not exist.");
    }

    // the HTTP request attribute is irrelevant for reading
    S3ObjectInputStream stream = new S3ObjectInputStream(blobs.get(blobName), null, false);
    S3Object s3Object = new S3Object();
    s3Object.setObjectContent(stream);
    return s3Object;
}

From source file:org.finra.dm.dao.impl.MockS3OperationsImpl.java

License:Apache License

@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3) {
    String bucketName = getObjectRequest.getBucketName();
    String key = getObjectRequest.getKey();

    if (MOCK_S3_BUCKET_NAME_NO_SUCH_BUCKET_EXCEPTION.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_NO_SUCH_BUCKET);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_BUCKET);
        throw amazonServiceException;
    }/*  w  ww  . j a  v a  2s .  co m*/

    if (MOCK_S3_BUCKET_NAME_ACCESS_DENIED.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_ACCESS_DENIED);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_ACCESS_DENIED);
        throw amazonServiceException;
    }

    if (MOCK_S3_BUCKET_NAME_INTERNAL_ERROR.equals(bucketName)) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_INTERNAL_ERROR);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_INTERNAL_ERROR);
        throw amazonServiceException;
    }

    MockS3Bucket mockS3Bucket = getOrCreateBucket(bucketName);
    MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key);

    if (mockS3Object == null) {
        AmazonServiceException amazonServiceException = new AmazonServiceException(
                S3Operations.ERROR_CODE_NO_SUCH_KEY);
        amazonServiceException.setErrorCode(S3Operations.ERROR_CODE_NO_SUCH_KEY);
        throw amazonServiceException;
    }

    S3Object s3Object = new S3Object();
    s3Object.setBucketName(bucketName);
    s3Object.setKey(key);
    s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData()));
    s3Object.setObjectMetadata(mockS3Object.getObjectMetadata());
    return s3Object;
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    AmazonS3Client s3 = getAmazonS3(s3FileTransferRequestParamsDto);
    try {/*from w  w  w  .ja  v a 2s .co  m*/
        return s3Operations.getS3Object(getObjectRequest, s3);
    } catch (AmazonServiceException amazonServiceException) {
        String errorCode = amazonServiceException.getErrorCode();

        switch (errorCode) {
        case S3Operations.ERROR_CODE_ACCESS_DENIED:
            throw new ObjectNotFoundException(
                    "Application does not have access to the specified S3 object at bucket '"
                            + getObjectRequest.getBucketName() + "' and key '" + getObjectRequest.getKey()
                            + "'.",
                    amazonServiceException);
        case S3Operations.ERROR_CODE_NO_SUCH_BUCKET:
            throw new ObjectNotFoundException(
                    "Specified S3 bucket '" + getObjectRequest.getBucketName() + "' does not exist.",
                    amazonServiceException);
        case S3Operations.ERROR_CODE_NO_SUCH_KEY:
            throw new ObjectNotFoundException(
                    "Specified S3 object key '" + getObjectRequest.getKey() + "' does not exist.",
                    amazonServiceException);
        default:
            throw amazonServiceException;
        }
    }
}

From source file:org.finra.herd.dao.impl.MockS3OperationsImpl.java

License:Apache License

@Override
public S3Object getS3Object(GetObjectRequest getObjectRequest, AmazonS3 s3) {
    MockS3Object mockS3Object = getMockS3Object(getObjectRequest.getBucketName(), getObjectRequest.getKey());

    S3Object s3Object = new S3Object();
    s3Object.setBucketName(getObjectRequest.getBucketName());
    s3Object.setKey(getObjectRequest.getKey());
    s3Object.setObjectContent(new ByteArrayInputStream(mockS3Object.getData()));
    s3Object.setObjectMetadata(mockS3Object.getObjectMetadata());

    return s3Object;
}

From source file:ws.salient.aws.s3.AmazonS3Wagon.java

License:Apache License

private boolean getIfModifiedSince(String path, File file, Date lastModified)
        throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
    GetObjectRequest getObject = new GetObjectRequest(repository.getHost(), getKey(path));
    try {/*  w ww .  j  av  a 2  s.  co  m*/
        if (lastModified != null) {
            getObject.setModifiedSinceConstraint(lastModified);
        }
        S3Object s3Object = s3.getObject(getObject);

        if (s3Object != null) {
            log.info(file.getAbsolutePath() + ": "
                    + String.valueOf(s3Object.getObjectMetadata().getContentLength()));
            try (InputStream in = s3Object.getObjectContent();
                    FileOutputStream fileOut = new FileOutputStream(file)) {
                IOUtils.copy(in, fileOut);
            }
            return true;
        }
        return false;
    } catch (AmazonS3Exception ex) {
        if (ex.getErrorCode().equals("NoSuchKey") || ex.getErrorCode().equals("AccessDenied")) {
            log.warn("s3://" + getObject.getBucketName() + "/" + getObject.getKey(), ex);
            throw new ResourceDoesNotExistException(
                    "s3://" + getObject.getBucketName() + "/" + getObject.getKey(), ex);
        }
        throw new TransferFailedException(ex.getMessage(), ex);
    } catch (IOException ex) {
        throw new TransferFailedException(ex.getMessage(), ex);
    }
}