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:com.liferay.portal.store.s3.S3Store.java

License:Open Source License

protected S3Object getS3Object(long companyId, long repositoryId, String fileName, String versionLabel)
        throws NoSuchFileException {

    try {//  w w  w. j ava  2  s  .c  o m
        if (Validator.isNull(versionLabel)) {
            versionLabel = getHeadVersionLabel(companyId, repositoryId, fileName);
        }

        String key = _s3KeyTransformer.getFileVersionKey(companyId, repositoryId, fileName, versionLabel);

        GetObjectRequest getObjectRequest = new GetObjectRequest(_bucketName, key);

        S3Object s3Object = _amazonS3.getObject(getObjectRequest);

        if (s3Object == null) {
            throw new NoSuchFileException(companyId, repositoryId, fileName, versionLabel);
        } else {
            return s3Object;
        }
    } catch (AmazonClientException ace) {
        if (isFileNotFound(ace)) {
            throw new NoSuchFileException(companyId, repositoryId, fileName, versionLabel);
        }

        throw transform(ace);
    }
}

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 w w  .  j a v  a2  s  .c  om
    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  w w  w. ja v a  2  s  .  com
    return schema;
}

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);//w  w w  .  j  a  v  a 2s. c  o 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
 * // w  w w  .  j av a2 s.  c o 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  a2 s  . c  om
@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.genie.core.services.impl.S3FileTransferImpl.java

License:Apache License

/**
 * {@inheritDoc}/*ww  w  .  ja  v a  2s.com*/
 */
@Override
public void getFile(@NotBlank(message = "Source file path cannot be empty.") final String srcRemotePath,
        @NotBlank(message = "Destination local path cannot be empty") final String dstLocalPath)
        throws GenieException {
    final long start = System.nanoTime();
    try {
        log.debug("Called with src path {} and destination path {}", srcRemotePath, dstLocalPath);

        final S3Key s3Key = new S3Key(srcRemotePath);
        try {
            this.s3Client.getObject(new GetObjectRequest(s3Key.getBucket(), s3Key.getKey()),
                    new File(dstLocalPath));
        } catch (AmazonS3Exception ase) {
            log.error("Error fetching file {} from s3 due to exception {}", srcRemotePath, ase);
            throw new GenieServerException("Error downloading file from s3. Filename: " + srcRemotePath);
        }
    } finally {
        this.downloadTimer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    }
}

From source file:com.netflix.genie.web.services.impl.S3FileTransferImpl.java

License:Apache License

/**
 * {@inheritDoc}//  www  .ja v  a 2 s  .c o  m
 */
@Override
public void getFile(@NotBlank(message = "Source file path cannot be empty.") final String srcRemotePath,
        @NotBlank(message = "Destination local path cannot be empty") final String dstLocalPath)
        throws GenieException {
    final long start = System.nanoTime();
    final Set<Tag> tags = Sets.newHashSet();
    try {
        log.debug("Called with src path {} and destination path {}", srcRemotePath, dstLocalPath);

        final AmazonS3URI s3Uri = getS3Uri(srcRemotePath);
        try {
            this.s3ClientFactory.getClient(s3Uri)
                    .getObject(new GetObjectRequest(s3Uri.getBucket(), s3Uri.getKey()), new File(dstLocalPath));
        } catch (final AmazonS3Exception ase) {
            log.error("Error fetching file {} from s3 due to exception {}", srcRemotePath, ase.toString());
            throw new GenieServerException("Error downloading file from s3. Filename: " + srcRemotePath, ase);
        }
        MetricsUtils.addSuccessTags(tags);
    } catch (Throwable t) {
        MetricsUtils.addFailureTagsWithException(tags, t);
        throw t;
    } finally {
        this.registry.timer(DOWNLOAD_TIMER_NAME, tags).record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    }
}

From source file:com.nextdoor.bender.handler.s3.S3EventIterator.java

License:Apache License

private void updateCursor() {
    if (this.currentIndex == 0 || (this.currentIndex < this.records.size() && !this.lineIterator.hasNext())) {
        /*//from  w  ww.j a v a 2s .c o  m
         * The previous reader must be closed in order to prevent S3 connection leaking
         */
        closeCurrentReader();

        /*
         * Use the S3 trigger event time for arrival time of records in file. This is less precise but
         * avoids making a call to the S3 api to find file creation time. Note that if the
         * deserializer creates a {@link com.nextdoor.bender.deserializer.DeserializedTimeSeriesEvent}
         * then this arrival time is not used.
         */
        S3EventNotificationRecord event = this.records.get(currentIndex);
        this.arrivalTime = event.getEventTime().toDate().getTime();
        this.currentS3Entity = event.getS3();

        /*
         * The S3 Object key is URL encoded and must be decoded before it can be used by the
         * AmazonS3Client
         */
        String key;
        try {
            key = URLDecoder.decode(this.currentS3Entity.getObject().getKey(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        /*
         * Stream object back from S3 into a reader
         */
        String bucketName = this.currentS3Entity.getBucket().getName();
        logger.debug("opening s3://" + bucketName + "/" + key);
        GetObjectRequest req = new GetObjectRequest(bucketName, key);
        S3Object obj = client.getObject(req);
        logger.trace("s3 get request id: " + client.getCachedResponseMetadata(req).getRequestId() + " host: "
                + client.getCachedResponseMetadata(req).getHostId() + " cloudfrontid: "
                + client.getCachedResponseMetadata(req).getCloudFrontId());

        /*
         * If the file is compressed run it through the GZIP decompressor
         */
        // TODO: support different types of compressions
        if (key.endsWith(".gz")) {
            GZIPInputStream gzip;
            try {
                gzip = new GZIPInputStream(obj.getObjectContent());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            reader = new BufferedReader(new InputStreamReader(gzip));
        } else {
            reader = new BufferedReader(new InputStreamReader(obj.getObjectContent()));
        }

        /*
         * Note the BufferedReader is lazy and so is the iterator. The object is directly streamed
         * from S3, fed into an input stream and consumed line by line by the iterator.
         */
        this.lineIterator = reader.lines().iterator();

        currentIndex++;
    }
}