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

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

Introduction

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

Prototype

public GetObjectRequest(String bucketName, String key) 

Source Link

Document

Constructs a new GetObjectRequest with all the required parameters.

Usage

From source file:org.ecocean.media.S3AssetStore.java

License:Open Source License

public S3Object getS3Object(MediaAsset ma) {
    JSONObject params = ma.getParameters();
    Object bp = getParameter(params, "bucket");
    Object kp = getParameter(params, "key");
    if ((bp == null) || (kp == null))
        return null;
    S3Object s3object = getS3Client().getObject(new GetObjectRequest(bp.toString(), kp.toString()));
    return s3object;
}

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

License:Apache License

/**
 * Downloads objects with the given prefix into a destination directory.
 * <p/>//from   w  ww  .  j  a  v  a  2 s  .  c o m
 * Creates any directory that does not exist in the path to the destination directory.
 */
@Override
public MultipleFileDownload downloadDirectory(String bucketName, String keyPrefix, File destinationDirectory,
        TransferManager transferManager) {
    LOGGER.debug("downloadDirectory(): bucketName = " + bucketName + ", keyPrefix = " + keyPrefix
            + ", destinationDirectory = " + destinationDirectory);

    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucketName);

    List<Download> downloads = new ArrayList<>();
    long totalBytes = 0;

    if (mockS3Bucket != null) {
        for (MockS3Object mockS3Object : mockS3Bucket.getObjects().values()) {
            if (mockS3Object.getKey().startsWith(keyPrefix)) {
                String filePath = destinationDirectory.getAbsolutePath() + "/" + mockS3Object.getKey();
                File file = new File(filePath);
                file.getParentFile().mkdirs(); // Create any directory in the path that does not exist.
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    LOGGER.debug("downloadDirectory(): Writing file " + file);
                    fileOutputStream.write(mockS3Object.getData());
                    totalBytes += mockS3Object.getData().length;
                    downloads.add(new DownloadImpl(null, null, null, null, null,
                            new GetObjectRequest(bucketName, mockS3Object.getKey()), file));
                } catch (IOException e) {
                    throw new RuntimeException("Error writing to file " + file, e);
                }
            }
        }
    }

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(totalBytes);
    progress.updateProgress(totalBytes);

    MultipleFileDownloadImpl multipleFileDownload = new MultipleFileDownloadImpl(null, progress, null,
            keyPrefix, bucketName, downloads);
    multipleFileDownload.setState(TransferState.Completed);
    return multipleFileDownload;
}

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

License:Apache License

/**
 * Downloads an object.//from   w w  w .  j  ava 2s  .c  om
 */
@Override
public Download download(String bucket, String key, File file, TransferManager transferManager) {
    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucket);
    MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key);
    try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
        fileOutputStream.write(mockS3Object.getData());
    } catch (IOException e) {
        throw new RuntimeException("Error writing to file " + file, e);
    }

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(mockS3Object.getData().length);
    progress.updateProgress(mockS3Object.getData().length);

    DownloadImpl download = new DownloadImpl(null, progress, null, null, null,
            new GetObjectRequest(bucket, key), file);
    download.setState(TransferState.Completed);

    return download;
}

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

License:Apache License

@Override
public Properties getProperties(String bucketName, String key,
        S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto) {
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
    S3Object s3Object = getS3Object(getObjectRequest, s3FileTransferRequestParamsDto);
    try (S3ObjectInputStream s3ObjectInputStream = s3Object.getObjectContent()) {
        return javaPropertiesHelper.getProperties(s3ObjectInputStream);
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException(
                "The properties file in S3 bucket '" + bucketName + "' and key '" + key + "' is invalid.", e);
    } catch (IOException e) {
        // This exception can come when the try-with-resources attempts to close the stream.
        // Not covered as JUnit, unfortunately no way of producing an IO exception.
        throw new IllegalStateException("Error closing S3 object input stream. See cause for details.", e);
    }/* ww  w .  j a v  a2  s  .  c  o  m*/
}

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

License:Apache License

@Override
public Download download(String bucket, String key, File file, TransferManager transferManager) {
    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucket);
    MockS3Object mockS3Object = mockS3Bucket.getObjects().get(key);
    try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
        fileOutputStream.write(mockS3Object.getData());
    } catch (IOException e) {
        throw new RuntimeException("Error writing to file " + file, e);
    }//  w  w w . jav  a2  s  .c  o  m

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(mockS3Object.getData().length);
    progress.updateProgress(mockS3Object.getData().length);

    DownloadImpl download = new DownloadImpl(null, progress, null, null, null,
            new GetObjectRequest(bucket, key), file, mockS3Object.getObjectMetadata(), false);
    download.setState(TransferState.Completed);

    return download;
}

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

License:Apache License

/**
 * {@inheritDoc}/*from  w  w  w .  j a  v  a  2s.  c  o m*/
 * <p/>
 * This implementation creates any directory that does not exist in the path to the destination directory.
 */
@Override
public MultipleFileDownload downloadDirectory(String bucketName, String keyPrefix, File destinationDirectory,
        TransferManager transferManager) {
    LOGGER.debug("downloadDirectory(): bucketName = " + bucketName + ", keyPrefix = " + keyPrefix
            + ", destinationDirectory = " + destinationDirectory);

    MockS3Bucket mockS3Bucket = mockS3Buckets.get(bucketName);

    List<Download> downloads = new ArrayList<>();
    long totalBytes = 0;

    if (mockS3Bucket != null) {
        for (MockS3Object mockS3Object : mockS3Bucket.getObjects().values()) {
            if (mockS3Object.getKey().startsWith(keyPrefix)) {
                String filePath = destinationDirectory.getAbsolutePath() + "/" + mockS3Object.getKey();
                File file = new File(filePath);
                file.getParentFile().mkdirs(); // Create any directory in the path that does not exist.
                try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
                    LOGGER.debug("downloadDirectory(): Writing file " + file);
                    fileOutputStream.write(mockS3Object.getData());
                    totalBytes += mockS3Object.getData().length;
                    downloads.add(new DownloadImpl(null, null, null, null, null,
                            new GetObjectRequest(bucketName, mockS3Object.getKey()), file,
                            mockS3Object.getObjectMetadata(), false));
                } catch (IOException e) {
                    throw new RuntimeException("Error writing to file " + file, e);
                }
            }
        }
    }

    TransferProgress progress = new TransferProgress();
    progress.setTotalBytesToTransfer(totalBytes);
    progress.updateProgress(totalBytes);

    MultipleFileDownloadImpl multipleFileDownload = new MultipleFileDownloadImpl(null, progress, null,
            keyPrefix, bucketName, downloads);
    multipleFileDownload.setState(TransferState.Completed);
    return multipleFileDownload;
}

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

License:Apache License

/**
 * Retrieves an S3 object.//  w  w w.j  a va 2s  .c  om
 *
 * @param s3Client the S3 client
 * @param bucketName the S3 bucket name
 * @param key the S3 object key
 * @param errorOnNoSuchKey true to throw an error when the object key is not found, otherwise return null
 *
 * @return the S3 object
 * @throws ObjectNotFoundException when specified bucket or key does not exist or access to bucket or key is denied
 */
private S3Object getS3Object(AmazonS3Client s3Client, String bucketName, String key, boolean errorOnNoSuchKey) {
    try {
        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, key);
        return s3Operations.getS3Object(getObjectRequest, s3Client);
    } catch (AmazonServiceException amazonServiceException) {
        String errorCode = amazonServiceException.getErrorCode();

        if (S3Operations.ERROR_CODE_ACCESS_DENIED.equals(errorCode)) {
            throw new ObjectNotFoundException(
                    "Application does not have access to the specified S3 object at bucket '" + bucketName
                            + "' and key '" + key + "'.",
                    amazonServiceException);
        } else if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(errorCode)) {
            throw new ObjectNotFoundException("Specified S3 bucket '" + bucketName + "' does not exist.",
                    amazonServiceException);
        } else if (S3Operations.ERROR_CODE_NO_SUCH_KEY.equals(errorCode)) {
            if (errorOnNoSuchKey) {
                throw new ObjectNotFoundException("Specified S3 object key '" + key + "' does not exist.",
                        amazonServiceException);
            } else {
                return null;
            }
        } else {
            throw amazonServiceException;
        }
    }
}

From source file:org.force66.aws.utils.S3Utils.java

License:Apache License

public static Properties loadProperties(String bucketName, String key) throws IOException {
    Properties props = new Properties();
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
    S3Object s3object = s3Client.getObject(new GetObjectRequest(bucketName, key));
    props.load(s3object.getObjectContent());
    return props;
}

From source file:org.geoserver.taskmanager.external.impl.S3FileServiceImpl.java

License:Open Source License

@Override
public InputStream read(String filePath) throws IOException {
    if (filePath == null) {
        throw new IllegalArgumentException("Name of a file can not be null.");
    }/*from ww  w .  j  av a2s . c o m*/
    GetObjectRequest objectRequest = new GetObjectRequest(rootFolder, filePath);
    try {
        return getS3Client().getObject(objectRequest).getObjectContent();
    } catch (AmazonClientException e) {
        throw new IOException(e);
    }
}

From source file:org.gradle.internal.resource.transport.aws.s3.S3Client.java

License:Apache License

private S3Object doGetS3Object(URI uri, boolean isLightWeight) {
    S3RegionalResource s3RegionalResource = new S3RegionalResource(uri);
    String bucketName = s3RegionalResource.getBucketName();
    String s3BucketKey = s3RegionalResource.getKey();
    configureClient(s3RegionalResource);

    GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, s3BucketKey);
    if (isLightWeight) {
        //Skip content download
        getObjectRequest.setRange(0, 0);
    }/*from  ww w  . ja va 2  s.  c  om*/

    try {
        return amazonS3Client.getObject(getObjectRequest);
    } catch (AmazonServiceException e) {
        String errorCode = e.getErrorCode();
        if (null != errorCode && errorCode.equalsIgnoreCase("NoSuchKey")) {
            return null;
        }
        throw ResourceExceptions.getFailed(uri, e);
    }
}