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.mrbjoern.blog.api.service.s3.S3Wrapper.java

License:Open Source License

@Override
public byte[] download(final String key) throws IOException {
    GetObjectRequest getObjectRequest = new GetObjectRequest(bucket, key);

    S3Object s3Object = amazonS3Client.getObject(getObjectRequest);

    S3ObjectInputStream objectInputStream = s3Object.getObjectContent();

    return IOUtils.toByteArray(objectInputStream);
}

From source file:com.mycompany.rproject.runnableClass.java

public static void use() throws IOException {
    AWSCredentials awsCreds = new PropertiesCredentials(
            new File("/Users/paulamontojo/Desktop/AwsCredentials.properties"));

    AmazonSQS sqs = new AmazonSQSClient(awsCreds);

    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    sqs.setRegion(usWest2);/*from  ww w. ja v  a 2  s  . co m*/
    String myQueueUrl = "https://sqs.us-west-2.amazonaws.com/711690152696/MyQueue";

    System.out.println("Receiving messages from MyQueue.\n");

    ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
    List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    while (messages.isEmpty()) {

        messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
    }

    String messageRecieptHandle = messages.get(0).getReceiptHandle();

    String a = messages.get(0).getBody();

    sqs.deleteMessage(new DeleteMessageRequest(myQueueUrl, messageRecieptHandle));

    //aqui opero y cuando acabe llamo para operar el siguiente.

    String n = "";
    String dbName = "mydb";
    String userName = "pmontojo";
    String password = "pmontojo";
    String hostname = "mydb.cued7orr1q2t.us-west-2.rds.amazonaws.com";
    String port = "3306";
    String jdbcUrl = "jdbc:mysql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password="
            + password;
    Connection conn = null;
    Statement setupStatement = null;
    Statement readStatement = null;
    ResultSet resultSet = null;
    String results = "";
    int numresults = 0;
    String statement = null;

    try {

        conn = DriverManager.getConnection(jdbcUrl);

        setupStatement = conn.createStatement();

        String insertUrl = "select video_name from metadata where id = " + a + ";";
        String checkUrl = "select url from metadata where id = " + a + ";";

        ResultSet rs = setupStatement.executeQuery(insertUrl);

        rs.next();

        System.out.println("este es el resultdo " + rs.getString(1));

        String names = rs.getString(1);
        ResultSet ch = setupStatement.executeQuery(checkUrl);
        ch.next();
        System.out.println("este es la url" + ch.getString(1));
        String urli = ch.getString(1);

        while (urli == null) {
            ResultSet sh = setupStatement.executeQuery(checkUrl);
            sh.next();
            System.out.println("este es la url" + sh.getString(1));
            urli = sh.getString(1);

        }
        setupStatement.close();
        AmazonS3 s3Client = new AmazonS3Client(awsCreds);

        S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, names));

        IOUtils.copy(object.getObjectContent(),
                new FileOutputStream(new File("/Users/paulamontojo/Desktop/download.avi")));

        putOutput();
        write();
        putInDb(sbu.toString(), a);

    } catch (SQLException ex) {
        // handle any errors
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } finally {
        System.out.println("Closing the connection.");
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }

    use();

}

From source file:com.naryx.tagfusion.cfm.tag.cfCONTENT.java

License:Open Source License

/**
 * Fetchs a remote object from S3; datasource, bucket, key, aes256key supported
 * /*from   w  ww . ja va  2  s  . co m*/
 * @param props
 * @param _Session
 * @throws cfmRunTimeException
 */
private void remoteFetchS3(cfStructData props, cfSession _Session) throws cfmRunTimeException {

    if (!props.containsKey("datasource") || !props.containsKey("bucket") || !props.containsKey("key"))
        throw newRunTimeException("'remote'.type=s3; minimum keys are datasource, bucket and key");

    String datasource = props.getData("datasource").getString();
    String bucket = props.getData("bucket").getString();
    String key = props.getData("key").getString();

    // Get the Amazon datasource
    AmazonKey amazonKey = AmazonKeyFactory.getDS(datasource);
    if (amazonKey == null)
        throw newRunTimeException("Amazon Datasource [" + datasource
                + "] has not been registered; use AmazonRegisterDataSource()");

    amazonKey.setDataSource(datasource);

    AmazonS3 s3Client = new AmazonBase().getAmazonS3(amazonKey);

    GetObjectRequest gor = new GetObjectRequest(bucket, key);
    if (props.containsKey("aes256key")) {
        String aes256key = props.getData("aes256key").getString();

        if (!aes256key.isEmpty())
            gor.setSSECustomerKey(new SSECustomerKey(aes256key));
    }

    // Get the object
    try {

        S3Object s3object = s3Client.getObject(gor);

        _Session.setContentType(s3object.getObjectMetadata().getContentType());

        InputStream in = s3object.getObjectContent();

        byte[] buffer = new byte[65536];
        int readCount = 0;

        while ((readCount = in.read(buffer)) != -1) {
            _Session.write(buffer, 0, readCount);
            _Session.pageFlush();
        }

    } catch (Exception e) {

        if (e.getMessage().indexOf("404") != -1) {
            _Session.setStatus(404);
            return;
        } else {
            cfEngine.log(e.getMessage());
            throw newRunTimeException(e.getMessage() + "; key=" + key + "; bucket=" + bucket);
        }
    }
}

From source file:com.netflix.dynomitemanager.sidecore.backup.S3Restore.java

License:Apache License

/**
  * Uses the Amazon S3 API to restore from S3
  *//*from   ww w  .  j a v  a2s . com*/
@Override
public boolean restoreData(String dateString) {
    long time = restoreTime(dateString);
    if (time > -1) {
        logger.info("Restoring data from S3.");
        AmazonS3Client s3Client = new AmazonS3Client(cred.getAwsCredentialProvider());

        try {
            /* construct the key for the backup data */
            String keyName = config.getBackupLocation() + "/" + iid.getInstance().getDatacenter() + "/"
                    + iid.getInstance().getRack() + "/" + iid.getInstance().getToken() + "/" + time;

            logger.info("S3 Bucket Name: " + config.getBucketName());
            logger.info("Key in Bucket: " + keyName);

            // Checking if the S3 bucket exists, and if does not, then we create it
            if (!(s3Client.doesBucketExist(config.getBucketName()))) {
                logger.error("Bucket with name: " + config.getBucketName() + " does not exist");
            } else {
                S3Object s3object = s3Client.getObject(new GetObjectRequest(config.getBucketName(), keyName));

                logger.info("Content-Type: " + s3object.getObjectMetadata().getContentType());

                String filepath = null;

                if (config.isAof()) {
                    filepath = config.getPersistenceLocation() + "/appendonly.aof";
                } else {
                    filepath = config.getPersistenceLocation() + "/nfredis.rdb";
                }

                IOUtils.copy(s3object.getObjectContent(), new FileOutputStream(new File(filepath)));
            }
            return true;
        } catch (AmazonServiceException ase) {

            logger.error("AmazonServiceException;"
                    + " request made it to Amazon S3, but was rejected with an error ");
            logger.error("Error Message:    " + ase.getMessage());
            logger.error("HTTP Status Code: " + ase.getStatusCode());
            logger.error("AWS Error Code:   " + ase.getErrorCode());
            logger.error("Error Type:       " + ase.getErrorType());
            logger.error("Request ID:       " + ase.getRequestId());

        } catch (AmazonClientException ace) {
            logger.error("AmazonClientException;" + " the client encountered "
                    + "an internal error while trying to " + "communicate with S3, ");
            logger.error("Error Message: " + ace.getMessage());
        } catch (IOException io) {
            logger.error("File storing error: " + io.getMessage());
        }
    } else {
        logger.error("Date in FP: " + dateString);
    }
    return false;
}

From source file:com.netflix.exhibitor.core.backup.s3.MockS3Client.java

License:Apache License

public MockS3Client(S3Object object, ObjectListing listing) {
    if (object != null) {
        S3Object value = new S3Object();
        value.setKey(object.getKey());//w  w w. j  a  v  a  2 s. c o m
        value.setObjectMetadata(object.getObjectMetadata());
        value.setObjectContent(object.getObjectContent());
        uploads.put(object.getKey(), value);
    }
    this.listing = listing;
}

From source file:com.netflix.exhibitor.core.backup.s3.S3BackupProvider.java

License:Apache License

@Override
public BackupStream getBackupStream(Exhibitor exhibitor, BackupMetaData backup,
        Map<String, String> configValues) throws Exception {
    long startMs = System.currentTimeMillis();
    RetryPolicy retryPolicy = makeRetryPolicy(configValues);
    S3Object object = null;
    int retryCount = 0;
    while (object == null) {
        try {/*from  w w  w  .j ava 2 s. com*/
            object = s3Client.getObject(configValues.get(CONFIG_BUCKET.getKey()), toKey(backup, configValues));
        } catch (AmazonS3Exception e) {
            if (e.getErrorType() == AmazonServiceException.ErrorType.Client) {
                exhibitor.getLog().add(ActivityLog.Type.ERROR,
                        "Amazon client error: " + ActivityLog.getExceptionMessage(e));
                return null;
            }

            if (!retryPolicy.allowRetry(retryCount++, System.currentTimeMillis() - startMs,
                    RetryLoop.getDefaultRetrySleeper())) {
                exhibitor.getLog().add(ActivityLog.Type.ERROR,
                        "Retries exhausted: " + ActivityLog.getExceptionMessage(e));
                return null;
            }
        }
    }

    final Throttle throttle = makeThrottle(configValues);
    final InputStream in = object.getObjectContent();
    final InputStream wrappedstream = new InputStream() {
        @Override
        public void close() throws IOException {
            in.close();
        }

        @Override
        public int read() throws IOException {
            throttle.throttle(1);
            return in.read();
        }

        @Override
        public int read(byte[] b) throws IOException {
            int bytesRead = in.read(b);
            if (bytesRead > 0) {
                throttle.throttle(bytesRead);
            }
            return bytesRead;
        }

        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            int bytesRead = in.read(b, off, len);
            if (bytesRead > 0) {
                throttle.throttle(bytesRead);
            }
            return bytesRead;
        }
    };

    return new BackupStream() {
        @Override
        public InputStream getStream() {
            return wrappedstream;
        }

        @Override
        public void close() throws IOException {
            in.close();
        }
    };
}

From source file:com.netflix.exhibitor.core.backup.s3.S3BackupProvider.java

License:Apache License

@Override
public void downloadBackup(Exhibitor exhibitor, BackupMetaData backup, OutputStream destination,
        Map<String, String> configValues) throws Exception {
    byte[] buffer = new byte[MIN_S3_PART_SIZE];

    long startMs = System.currentTimeMillis();
    RetryPolicy retryPolicy = makeRetryPolicy(configValues);
    int retryCount = 0;
    boolean done = false;

    while (!done) {
        Throttle throttle = makeThrottle(configValues);
        InputStream in = null;/*from  w  w w  . j  av a  2 s . co  m*/
        try {
            S3Object object = s3Client.getObject(configValues.get(CONFIG_BUCKET.getKey()),
                    toKey(backup, configValues));
            in = object.getObjectContent();

            for (;;) {
                int bytesRead = in.read(buffer);
                if (bytesRead < 0) {
                    break;
                }

                throttle.throttle(bytesRead);
                destination.write(buffer, 0, bytesRead);
            }

            done = true;
        } catch (Exception e) {
            if (!retryPolicy.allowRetry(retryCount++, System.currentTimeMillis() - startMs,
                    RetryLoop.getDefaultRetrySleeper())) {
                done = true;
            }
        } finally {
            CloseableUtils.closeQuietly(in);
        }
    }
}

From source file:com.netflix.exhibitor.core.config.s3.S3ConfigProvider.java

License:Apache License

@Override
public LoadedInstanceConfig loadConfig() throws Exception {
    Date lastModified;//from ww  w .  jav a  2 s  .c  o m
    Properties properties = new Properties();
    S3Object object = getConfigObject();
    if (object != null) {
        try {
            lastModified = object.getObjectMetadata().getLastModified();
            properties.load(object.getObjectContent());
        } finally {
            CloseableUtils.closeQuietly(object.getObjectContent());
        }
    } else {
        lastModified = new Date(0L);
    }

    PropertyBasedInstanceConfig config = new PropertyBasedInstanceConfig(properties, defaults);
    return new LoadedInstanceConfig(config, lastModified.getTime());
}

From source file:com.netflix.hollow.example.consumer.infrastructure.S3BlobRetriever.java

License:Apache License

private File downloadFile(String objectName) throws IOException {
    for (int retryCount = 0; retryCount < 3; retryCount++) {
        try {//from  w ww . j ava  2s . c  o  m
            File tempFile = new File(System.getProperty("java.io.tmpdir"), objectName.replace('/', '-'));

            S3Object s3Object = s3.getObject(bucketName, objectName);

            MessageDigest md = MessageDigest.getInstance("MD5");
            try (InputStream is = new DigestInputStream(s3Object.getObjectContent(), md);
                    OutputStream os = new FileOutputStream(tempFile)) {
                IOUtils.copy(is, os);
            }

            String expectedMD5 = s3Object.getObjectMetadata().getETag();
            String actualMD5 = Base16Lower.encodeAsString(md.digest());

            if (!actualMD5.equals(expectedMD5))
                throw new IOException("MD5 sum did not match expected!");

            return tempFile;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    throw new IOException("Unable to successfully retrieve stream from S3 after 3 retries");
}

From source file:com.netflix.ice.common.AwsUtils.java

License:Apache License

private static boolean download(AmazonS3Client s3Client, String bucketName, String fileKey, File file) {
    do {/*from ww w . jav a 2 s  .  c om*/
        S3Object s3Object = s3Client.getObject(bucketName, fileKey);
        InputStream input = s3Object.getObjectContent();
        long targetSize = s3Object.getObjectMetadata().getContentLength();
        FileOutputStream output = null;

        boolean downloaded = false;
        long size = 0;
        try {
            output = new FileOutputStream(file);
            byte buf[] = new byte[1024000];
            int len;
            while ((len = input.read(buf)) > 0) {
                output.write(buf, 0, len);
                size += len;
            }
            downloaded = true;
        } catch (IOException e) {
            logger.error("error in downloading " + file, e);
        } finally {
            if (input != null)
                try {
                    input.close();
                } catch (IOException e) {
                }
            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                }
        }

        if (downloaded) {
            long contentLenth = s3Client.getObjectMetadata(bucketName, fileKey).getContentLength();
            if (contentLenth != size) {
                logger.warn("size does not match contentLenth=" + contentLenth + " downloadSize=" + size
                        + "targetSize=" + targetSize + " ... re-downlaoding " + fileKey);
            } else
                return true;
        }
        try {
            Thread.sleep(2000L);
        } catch (Exception e) {
        }
    } while (true);
}