Search in sources :

Example 56 with GetObjectRequest

use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project carina by qaprosoft.

the class AmazonS3Manager method get.

/**
 * Get any file from Amazon S3 storage as S3Object.
 *
 * @param bucket
 *            - S3 Bucket name.
 * @param key
 *            - S3 storage path. Example:
 *            DEMO/TestSuiteName/TestMethodName/file.txt
 * @return S3Object
 */
public S3Object get(String bucket, String key) {
    if (bucket == null) {
        throw new RuntimeException("Bucket is null!");
    }
    if (bucket.isEmpty()) {
        throw new RuntimeException("Bucket is empty!");
    }
    if (key == null) {
        throw new RuntimeException("Key is null!");
    }
    if (key.isEmpty()) {
        throw new RuntimeException("Key is empty!");
    }
    try {
        LOGGER.info("Finding an s3object...");
        // TODO investigate possibility to add percentage of completed
        // downloading
        S3Object s3object = s3client.getObject(new GetObjectRequest(bucket, key));
        LOGGER.info("Content-Type: " + s3object.getObjectMetadata().getContentType());
        return s3object;
    /*
             * GetObjectRequest rangeObjectRequest = new GetObjectRequest(
             * bucketName, key); rangeObjectRequest.setRange(0, 10); S3Object
             * objectPortion = s3client.getObject(rangeObjectRequest);
             */
    } catch (AmazonServiceException ase) {
        LOGGER.error("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response for some reason.\n" + "Error Message:    " + ase.getMessage() + "\n" + "HTTP Status Code: " + ase.getStatusCode() + "\n" + "AWS Error Code:   " + ase.getErrorCode() + "\n" + "Error Type:       " + ase.getErrorType() + "\n" + "Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        LOGGER.error("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network.\n" + "Error Message: " + ace.getMessage());
    }
    // TODO investigate pros and cons returning null
    throw new RuntimeException("Unable to download '" + key + "' from Amazon S3 bucket '" + bucket + "'");
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest)

Example 57 with GetObjectRequest

use of software.amazon.awssdk.services.s3.model.GetObjectRequest in project bender by Nextdoor.

the class GeoIpOperationFactory method setConf.

@Override
public void setConf(AbstractConfig config) {
    this.config = (GeoIpOperationConfig) config;
    AmazonS3Client client = this.s3Factory.newInstance();
    AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb());
    GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey());
    S3Object obj = client.getObject(req);
    try {
        this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build();
    } catch (IOException e) {
        throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e);
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ConfigurationException(com.nextdoor.bender.config.ConfigurationException) CHMCache(com.maxmind.db.CHMCache) DatabaseReader(com.maxmind.geoip2.DatabaseReader) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) AmazonS3URI(com.amazonaws.services.s3.AmazonS3URI)

Aggregations

GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)40 IOException (java.io.IOException)24 S3Object (com.amazonaws.services.s3.model.S3Object)23 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)17 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)14 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)13 AmazonClientException (com.amazonaws.AmazonClientException)10 InputStream (java.io.InputStream)9 File (java.io.File)8 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)5 FileOutputStream (java.io.FileOutputStream)5 OutputStream (java.io.OutputStream)5 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)4 Date (java.util.Date)4 S3Client (software.amazon.awssdk.services.s3.S3Client)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 Test (org.junit.jupiter.api.Test)3 SelfServiceConfig (uk.gov.ida.hub.config.configuration.SelfServiceConfig)3 RemoteConfigCollection (uk.gov.ida.hub.config.domain.remoteconfig.RemoteConfigCollection)3