Example usage for com.amazonaws.services.s3.model S3Object getObjectContent

List of usage examples for com.amazonaws.services.s3.model S3Object getObjectContent

Introduction

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

Prototype

public S3ObjectInputStream getObjectContent() 

Source Link

Document

Gets the input stream containing the contents of this object.

Usage

From source file:com.amazon.photosharing.utils.ContentHelper.java

License:Open Source License

public S3ObjectInputStream downloadContent(String p_bucket_name, String p_s3_key) {

    _logger.debug("Downloading file " + p_s3_key + " from bucket " + p_bucket_name);

    S3ObjectInputStream stream = null;

    if (s3Client.doesObjectExist(p_bucket_name, p_s3_key)) {
        S3Object object = s3Client.getObject(p_bucket_name, p_s3_key);
        stream = object.getObjectContent();
    }//w  w  w  . j a  va2s .co  m

    return stream;
}

From source file:com.amazon.sqs.javamessaging.AmazonSQSExtendedClient.java

License:Open Source License

private String getTextFromS3(String s3BucketName, String s3Key) {
    GetObjectRequest getObjectRequest = new GetObjectRequest(s3BucketName, s3Key);
    String embeddedText = null;//from  ww  w .jav a  2 s.c o  m
    S3Object obj = null;
    try {
        obj = clientConfiguration.getAmazonS3Client().getObject(getObjectRequest);
    } catch (AmazonServiceException e) {
        String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonServiceException(errorMessage, e);
    } catch (AmazonClientException e) {
        String errorMessage = "Failed to get the S3 object which contains the message payload. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
    try {
        InputStream objContent = obj.getObjectContent();
        java.util.Scanner objContentScanner = new java.util.Scanner(objContent, "UTF-8");
        objContentScanner.useDelimiter("\\A");
        embeddedText = objContentScanner.hasNext() ? objContentScanner.next() : "";
        objContentScanner.close();
        objContent.close();
    } catch (IOException e) {
        String errorMessage = "Failure when handling the message which was read from S3 object. Message was not received.";
        LOG.error(errorMessage, e);
        throw new AmazonClientException(errorMessage, e);
    }
    return embeddedText;
}

From source file:com.amediamanager.config.S3ConfigurationProvider.java

License:Apache License

@Override
public void loadProperties() {
    this.properties = null;

    // Load properties if there is a bucket and key
    if (bucket != null && key != null) {
        AWSCredentialsProvider creds = new AWSCredentialsProviderChain(new InstanceProfileCredentialsProvider(),
                new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider());
        AmazonS3 s3Client = new AmazonS3Client(creds);
        try {//from   w w  w .  java2s  . c  o  m
            S3Object object = s3Client.getObject(this.bucket, this.key);
            if (object != null) {
                this.properties = new Properties();
                try {
                    this.properties.load(object.getObjectContent());
                } catch (IOException e) {
                    this.properties = null;
                    LOG.warn("Found configuration file in S3 but failed to load properties (s3://{}/{})",
                            new Object[] { this.bucket, this.key, e });
                } finally {
                    try {
                        object.close();
                    } catch (IOException e) {
                        // Don't care
                    }
                }
            }
        } catch (AmazonS3Exception ase) {
            LOG.error("Error loading config from s3://{}/{}", new Object[] { this.bucket, this.key, ase });
        }
    }
}

From source file:com.arc.cloud.aws.s3.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {

    /*/* w  w w  .  j a  v a2 s  . c o  m*/
     * The ProfileCredentialsProvider will return your [default]
     * credential profile by reading from the credentials file located at
     * (~/.aws/credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
         * so once a bucket name has been taken by any user, you can't create
         * another bucket with that same name.
         *
         * You can optionally specify a location for your bucket if you want to
         * keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to
         * S3, or upload directly an InputStream if you know the length of
         * the data in the stream. You can also specify your own metadata
         * when uploading to S3, which allows you set a variety of options
         * like content-type and content-encoding, plus additional metadata
         * specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

        /*
         * Download an object - When you download an object, you get all of
         * the object's metadata and a stream from which to read the contents.
         * It's important to read the contents of the stream as quickly as
         * possibly since the data is streamed directly from Amazon S3 and your
         * network connection will remain open until you read all the data or
         * close the input stream.
         *
         * GetObjectRequest also supports several other options, including
         * conditional downloading of objects based on modification times,
         * ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for
         * listing the objects in your bucket.  Keep in mind that buckets with
         * many objects might truncate their results when listing their objects,
         * so be sure to check if the returned object listing is truncated, and
         * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
         * additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket,
         * there is no way to undelete an object, so use caution when deleting objects.
         */
        System.out.println("Deleting an object\n");
        s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be
         * deleted, so remember to delete any objects from your buckets before
         * you try to delete them.
         */
        System.out.println("Deleting bucket " + bucketName + "\n");
        s3.deleteBucket(bucketName);
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:com.atlantbh.jmeter.plugins.aws.s3.AWSS3Downloader.java

License:Apache License

@Override
public SampleResult sample(Entry arg0) {
    LOGGER.info("Download started....");
    SampleResult result = new SampleResult();
    result.setSampleLabel(getName());/*w  w  w.ja  va 2 s  .  c  o m*/
    result.setDataType(SampleResult.TEXT);
    result.sampleStart();
    try {
        BasicAWSCredentials creds = new BasicAWSCredentials(getKey(), getSecret());
        AmazonS3 client = new AmazonS3Client(creds);
        S3Object s3Object = client.getObject(new GetObjectRequest(new S3ObjectId(getBucket(), getObject())));
        InputStream is = s3Object.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(getDestination())));
        char[] buffer = new char[1024 * 1024];
        while (true) {
            int c = reader.read(buffer);
            if (c == -1) {
                break;
            }
            bufferedWriter.write(buffer);
        }
        reader.close();
        bufferedWriter.close();
        LOGGER.info("Download finished.");
        result.setResponseData("Download finished".getBytes());
        result.setSuccessful(!false);
        result.setResponseCode("200");
        result.setResponseMessage("Downloaded");
    } catch (Exception e) {
        LOGGER.info("Download error.");
        result.setResponseData(("Download error: " + e.getMessage()).getBytes());
        result.setSuccessful(false);
        result.setResponseCode("500");
        result.setResponseMessage("Error");
    }
    result.sampleEnd();
    return result;
}

From source file:com.atlantbh.jmeter.plugins.aws.s3.AWSS3Reader.java

License:Apache License

@Override
public SampleResult sample(Entry arg0) {
    LOGGER.info("Read started....");
    SampleResult result = new SampleResult();
    result.setSampleLabel(getName());/*w ww .  ja v  a2 s . c  o  m*/
    result.setDataType(SampleResult.TEXT);
    result.sampleStart();
    try {
        BasicAWSCredentials creds = new BasicAWSCredentials(getKey(), getSecret());
        AmazonS3 client = new AmazonS3Client(creds);
        S3Object s3Object = client.getObject(new GetObjectRequest(new S3ObjectId(getBucket(), getObject())));
        InputStream is = s3Object.getObjectContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        result.setResponseData(sb.toString().getBytes());
        reader.close();
        LOGGER.info("Read finished.");
        result.setSuccessful(!false);
        result.setResponseCode("200");
        result.setResponseMessage("Read done");
        result.setContentType("text/plain");
    } catch (Exception e) {
        LOGGER.info("Read error.");
        result.setResponseData(("Read error: " + e.getMessage()).getBytes());
        result.setSuccessful(false);
        result.setResponseCode("500");
        result.setResponseMessage("Error");
    }
    result.sampleEnd();
    return result;
}

From source file:com.atlassian.localstack.sample.S3Sample.java

License:Open Source License

public static void runTest(AWSCredentials credentials) throws IOException {

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);//w ww  .  j  ava2  s .  c om
    s3.setEndpoint(LocalstackTestRunner.getEndpointS3());

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    /*
     * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
     * so once a bucket name has been taken by any user, you can't create
     * another bucket with that same name.
     *
     * You can optionally specify a location for your bucket if you want to
     * keep your data closer to your applications or users.
     */
    System.out.println("Creating bucket " + bucketName + "\n");
    s3.createBucket(bucketName);

    /*
     * List the buckets in your account
     */
    System.out.println("Listing buckets");
    for (Bucket bucket : s3.listBuckets()) {
        System.out.println(" - " + bucket.getName());
    }
    System.out.println();

    /*
     * Upload an object to your bucket - You can easily upload a file to
     * S3, or upload directly an InputStream if you know the length of
     * the data in the stream. You can also specify your own metadata
     * when uploading to S3, which allows you set a variety of options
     * like content-type and content-encoding, plus additional metadata
     * specific to your applications.
     */
    System.out.println("Uploading a new object to S3 from a file\n");
    s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

    /*
     * Download an object - When you download an object, you get all of
     * the object's metadata and a stream from which to read the contents.
     * It's important to read the contents of the stream as quickly as
     * possibly since the data is streamed directly from Amazon S3 and your
     * network connection will remain open until you read all the data or
     * close the input stream.
     *
     * GetObjectRequest also supports several other options, including
     * conditional downloading of objects based on modification times,
     * ETags, and selectively downloading a range of an object.
     */
    System.out.println("Downloading an object");
    S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
    System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
    displayTextInputStream(object.getObjectContent());

    /*
     * List objects in your bucket by prefix - There are many options for
     * listing the objects in your bucket.  Keep in mind that buckets with
     * many objects might truncate their results when listing their objects,
     * so be sure to check if the returned object listing is truncated, and
     * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
     * additional results.
     */
    System.out.println("Listing objects");
    ObjectListing objectListing = s3
            .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
    for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
        System.out.println(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
    }
    System.out.println();

    /*
     * Delete an object - Unless versioning has been turned on for your bucket,
     * there is no way to undelete an object, so use caution when deleting objects.
     */
    System.out.println("Deleting an object\n");
    s3.deleteObject(bucketName, key);

    /*
     * Delete a bucket - A bucket must be completely empty before it can be
     * deleted, so remember to delete any objects from your buckets before
     * you try to delete them.
     */
    System.out.println("Deleting bucket " + bucketName + "\n");
    s3.deleteBucket(bucketName);
}

From source file:com.awscrud.aws.S3StorageManager.java

License:Open Source License

public InputStream loadInputStream(AwscrudStorageObject s3Store) throws IOException {
    S3Object s3 = getS3Object(s3Store);
    this.lastUpdate = s3.getObjectMetadata().getLastModified();
    return s3.getObjectContent();
}

From source file:com.awscrud.aws.S3StorageManager.java

License:Open Source License

/**
 * Loads the raw object data from S3 storage
 * @param s3Store the s3 object to be loaded from the store
 * @return input stream for reading in the raw object
 * @throws IOException// ww  w .  java  2 s.  co  m
 */
public InputStream loadStream(AwscrudStorageObject s3Store) throws IOException {
    S3Object obj = getS3Object(s3Store);
    InputStream is = obj.getObjectContent();
    return is;
}

From source file:com.bigstep.S3Sampler.java

License:Apache License

@Override
public SampleResult runTest(JavaSamplerContext context) {
    // pull parameters
    String bucket = context.getParameter("bucket");
    String object = context.getParameter("object");
    String method = context.getParameter("method");
    String local_file_path = context.getParameter("local_file_path");
    String key_id = context.getParameter("key_id");
    String secret_key = context.getParameter("secret_key");
    String proxy_host = context.getParameter("proxy_host");
    String proxy_port = context.getParameter("proxy_port");
    String endpoint = context.getParameter("endpoint");

    log.debug("runTest:method=" + method + " local_file_path=" + local_file_path + " bucket=" + bucket
            + " object=" + object);

    SampleResult result = new SampleResult();
    result.sampleStart(); // start stopwatch

    try {//from   w  ww  .  ja v a2 s  . co  m
        ClientConfiguration config = new ClientConfiguration();
        if (proxy_host != null && !proxy_host.isEmpty()) {
            config.setProxyHost(proxy_host);
        }
        if (proxy_port != null && !proxy_port.isEmpty()) {
            config.setProxyPort(Integer.parseInt(proxy_port));
        }
        //config.setProtocol(Protocol.HTTP);

        AWSCredentials credentials = new BasicAWSCredentials(key_id, secret_key);

        AmazonS3 s3Client = new AmazonS3Client(credentials, config);
        if (endpoint != null && !endpoint.isEmpty()) {
            s3Client.setEndpoint(endpoint);
        }
        ObjectMetadata meta = null;

        if (method.equals("GET")) {
            File file = new File(local_file_path);
            //meta= s3Client.getObject(new GetObjectRequest(bucket, object), file);
            S3Object s3object = s3Client.getObject(bucket, object);
            S3ObjectInputStream stream = s3object.getObjectContent();
            //while(stream.skip(1024*1024)>0);
            stream.close();
        } else if (method.equals("PUT")) {
            File file = new File(local_file_path);
            s3Client.putObject(bucket, object, file);
        }

        result.sampleEnd(); // stop stopwatch
        result.setSuccessful(true);
        if (meta != null) {
            result.setResponseMessage(
                    "OK on url:" + bucket + "/" + object + ". Length=" + meta.getContentLength());
        } else {
            result.setResponseMessage("OK on url:" + bucket + "/" + object + ".No metadata");
        }
        result.setResponseCodeOK(); // 200 code

    } catch (Exception e) {
        result.sampleEnd(); // stop stopwatch
        result.setSuccessful(false);
        result.setResponseMessage("Exception: " + e);

        // get stack trace as a String to return as document data
        java.io.StringWriter stringWriter = new java.io.StringWriter();
        e.printStackTrace(new java.io.PrintWriter(stringWriter));
        result.setResponseData(stringWriter.toString());
        result.setDataType(org.apache.jmeter.samplers.SampleResult.TEXT);
        result.setResponseCode("500");
    }

    return result;
}