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.intuit.s3encrypt.S3Encrypt.java

License:Open Source License

public static void saveS3ObjectContent(S3Object obj, String path, String filename) throws IOException {
    InputStream stream = obj.getObjectContent();
    FileOutputStream fos = new FileOutputStream(path + "/" + filename);
    byte[] buffer = new byte[1024];
    int len;//w  w w.  j a  va 2 s.c  o m
    while ((len = stream.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
    }
    fos.close();
}

From source file:com.intuit.tank.vmManager.environment.amazon.AmazonS3.java

License:Open Source License

/**
 * //from w w w .  ja va 2  s. c om
 * @param key
 * @return
 */
public InputStream getFile(String bucketName, String path) {
    InputStream ret = null;
    S3ObjectInputStream objectContent = null;
    try {
        S3Object object = s3Client.getObject(bucketName, path);
        if (object != null) {
            ByteArrayOutputStream temp = new ByteArrayOutputStream();
            objectContent = object.getObjectContent();
            IOUtils.copy(objectContent, temp);
            ret = new ByteArrayInputStream(temp.toByteArray());
        }
    } catch (Exception e) {
        LOG.error("Error getting File: " + e, e);
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(objectContent);
    }
    return ret;
}

From source file:com.jeet.s3.AmazonS3ClientWrapper.java

License:Open Source License

public InputStream getObjectStream(String path) {
    try {/* ww w  .j  a  v a 2  s .  co  m*/
        S3Object object = s3Client.getObject(new GetObjectRequest(Constants.BUCKET_NAME, path));
        return object.getObjectContent();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.jfixby.scarabei.red.aws.test.S3Sample.java

License:Open Source License

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

    /*/*from   ww  w  . j  av a  2  s. c  om*/
     * The ProfileCredentialsProvider will return your [default] credential profile by reading from the credentials file located
     * at (C:\\Users\\JCode\\.aws\\credentials).
     */
    AWSCredentials credentials = null;
    try {
        credentials = new ProfileCredentialsProvider("default").getCredentials();
    } catch (final 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 (C:\\Users\\%USERNAME%\\.aws\\credentials), and is in valid format.", e);
    }

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

    final String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    final 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 (final 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");
        final 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");
        final ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (final 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 (final 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 (final 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.jktsoftware.amazondownloader.download.S3TypeObject.java

License:Open Source License

public InputStream getObjectContent() {
    String repoid = getRepoId();/*  www . j  av  a  2 s  . c  o m*/
    System.out.println("Attempting to get object content for :" + getObjectKey());
    System.out.println(getRepoId());
    System.out.println(getObjectKey());

    S3Object object = s3.getObject(getRepoId(), getObjectKey());
    return (InputStream) object.getObjectContent();
}

From source file:com.liferay.portal.store.s3.S3FileCacheImpl.java

License:Open Source License

@Override
public File getCacheFile(S3Object s3Object, String fileName) throws IOException {

    StringBundler sb = new StringBundler(4);

    sb.append(getCacheDirName());//from  w w w .j  a  va 2  s. co  m
    sb.append(DateUtil.getCurrentDate(_CACHE_DIR_PATTERN, LocaleUtil.getDefault()));
    sb.append(_s3KeyTransformer.getNormalizedFileName(fileName));

    ObjectMetadata objectMetadata = s3Object.getObjectMetadata();

    Date lastModifiedDate = objectMetadata.getLastModified();

    sb.append(lastModifiedDate.getTime());

    String cacheFileName = sb.toString();

    File cacheFile = new File(cacheFileName);

    try (InputStream inputStream = s3Object.getObjectContent()) {
        if (cacheFile.exists() && (cacheFile.lastModified() >= lastModifiedDate.getTime())) {

            return cacheFile;
        }

        if (inputStream == null) {
            throw new IOException("S3 object input stream is null");
        }

        File parentFile = cacheFile.getParentFile();

        FileUtil.mkdirs(parentFile);

        try (OutputStream outputStream = new FileOutputStream(cacheFile)) {
            StreamUtil.transfer(inputStream, outputStream);
        }
    }

    return cacheFile;
}

From source file:com.mateusz.mateuszsqs.SQSConfig.java

public static void processFile(String key, AmazonS3 s3, String bucketName) throws IOException {
    System.out.println("Downloading an object");
    S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
    System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
    System.out.println("Deleting an object\n");
    s3.deleteObject(bucketName, key);/* w ww  . j  a va  2s  . c o m*/
    System.out.println("Processing...");
    System.out.println("Uploading a new object to S3 from a file\n");
    InputStream changedStream = procesIamge(object.getObjectContent());
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(changedStream.available());
    metadata.setLastModified(new Date(System.currentTimeMillis()));
    s3.putObject(new PutObjectRequest(bucketName, key, changedStream, metadata));
}

From source file:com.mesosphere.dcos.cassandra.executor.backup.S3StorageDriver.java

License:Apache License

@Override
public String downloadSchema(BackupRestoreContext ctx) throws Exception {
    final String nodeId = ctx.getNodeId();
    final AmazonS3Client amazonS3Client = getAmazonS3Client(ctx);
    final String key = getPrefixKey(ctx) + "/" + nodeId + "/" + StorageUtil.SCHEMA_FILE;

    S3Object object = amazonS3Client.getObject(new GetObjectRequest(getBucketName(ctx), key));
    InputStream objectData = object.getObjectContent();
    String schema = IOUtils.toString(objectData, "UTF-8");
    objectData.close();/*from www  .j  a v  a  2 s  .c  om*/
    return schema;
}

From source file:com.miovision.oss.awsbillingtools.s3.loader.S3BillingRecordLoader.java

License:Open Source License

public Stream<T> load(S3BillingRecordFile billingRecordFile) throws IOException {
    S3Object s3Object = amazonS3.getObject(billingRecordFile.getBucketName(), billingRecordFile.getKey());
    try {//  ww  w.j ava  2  s  . c  om
        InputStream inputStream = s3Object.getObjectContent();
        if (billingRecordFile.isZip()) {
            ZipInputStream zipInputStream = new ZipInputStream(inputStream);
            zipInputStream.getNextEntry();
            inputStream = zipInputStream;
        }
        return billingRecordParser.parse(new InputStreamReader(inputStream, Charset.defaultCharset()));
    } catch (Exception e) {
        s3Object.close();
        throw e;
    }
}

From source file:com.moxtra.S3StorageManager.java

License:Open Source License

/**
 * Loads the raw object data from S3 storage
 * @param bucketname/* ww w  .  j ava 2 s. c om*/
 * @param key
 * @return input stream for reading in the raw object
 * @throws IOException
 */
public InputStream loadStream(String bucketname, String key) throws IOException {
    S3Object obj = s3Client.getObject(bucketname, key);
    InputStream is = obj.getObjectContent();
    return is;
}