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:littleware.apps.fishRunner.FishApp.java

License:LGPL

/**
 * Download the given S3 URI to the given dest file
 * /*  ww w .  j  a v  a2s.c  om*/
 * @param resourcePath path string either starts with s3:// or treated as local path
 * @param destFile
 * @return local file to work with if resource exists, otherwise throws ConfigException
 */
private File downloadHelper(String resourcePath, File destFile) throws ConfigException {
    if (resourcePath.startsWith("s3://")) {
        try {
            final URI s3URI = new java.net.URI(resourcePath);
            final GetObjectRequest req = new GetObjectRequest(s3URI.getHost(),
                    s3URI.getPath().replaceAll("//+", "/").replaceAll("^/+", "").replaceAll("/+$", ""));

            final ObjectMetadata meta = s3.getObject(req, destFile);
            if (null == meta) {
                throw new RuntimeException("Unable to access resource: " + resourcePath);
            }

            if ("gzip".equalsIgnoreCase(meta.getContentEncoding())) {
                // need to unzip the war ...
                final File temp = new File(destFile.getParentFile(), destFile.getName() + ".gz");
                temp.delete();
                destFile.renameTo(temp);
                try (final InputStream gzin = new java.util.zip.GZIPInputStream(new FileInputStream(temp));) {
                    Files.copy(gzin, destFile.toPath());
                }
            }

            return destFile;
        } catch (URISyntaxException | IOException ex) {
            throw new RuntimeException("Failed parsing: " + resourcePath, ex);
        }
    } else {
        final File resourceFile = new File(resourcePath);
        if (!resourceFile.exists()) {
            throw new RuntimeException("Unable to access resource: " + resourcePath);
        }
        return resourceFile;
    }
}

From source file:lumbermill.internal.aws.S3ClientImpl.java

License:Apache License

public File get(String bucket, String key) {

    LOGGER.trace("Getting file from s3://{}/{}", bucket, key);
    File file;// w w w  .  j a v a  2  s. c o m
    try {
        file = File.createTempFile("lumbermill", ".log");
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    try {
        key = URLDecoder.decode(key, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }

    s3Client.getObject(new GetObjectRequest(bucket, key), file);
    return file;
}

From source file:n3phele.agent.repohandlers.S3Large.java

License:Open Source License

private S3Object object() {
    S3Object result;//from w  w w .ja v  a  2s . co  m
    if (this.object == null) {
        this.object = s3().getObject(new GetObjectRequest(this.root, this.key));
        result = this.object;
    } else {
        result = this.object;
        this.object = null;
    }
    return result;
}

From source file:net.geoprism.data.aws.AmazonEndpoint.java

License:Open Source License

@Override
public void copyFiles(File directory, List<String> keys, boolean preserveDirectories) {
    try {/*w  ww.j  av  a 2  s  . c  om*/
        List<File> files = new LinkedList<File>();

        AmazonS3 client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        for (String key : keys) {
            GetObjectRequest request = new GetObjectRequest("geodashboarddata", key);

            S3Object object = client.getObject(request);

            InputStream istream = object.getObjectContent();

            try {
                String targetPath = this.getTargetPath(preserveDirectories, key);

                File file = new File(directory, targetPath);

                FileUtils.copyInputStreamToFile(istream, file);

                files.add(file);
            } finally {
                // Process the objectData stream.
                istream.close();
            }
        }
    } catch (IOException e) {
        throw new ProgrammingErrorException(e);
    }
}

From source file:net.oletalk.hellospringboot.dao.S3Dao.java

public void getObjectData(String bucketName, String key, OutputStream out) throws S3Exception {
    AmazonS3 s3client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
    LOG.info("fetching requested object " + key);
    try {/*from ww  w  .  j av a2s  . c om*/
        S3Object object = s3client.getObject(new GetObjectRequest(bucketName, key));
        LOG.info("fetched, serving up");
        try {
            IOUtils.copy(object.getObjectContent(), out);
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }

    } catch (AmazonS3Exception e) {
        LOG.error("Problem fetching from S3: ", e);
        String errorMsg = "Error fetching document";
        try {
            out.write(errorMsg.getBytes(Charset.defaultCharset()));
            throw new S3Exception("Error fetching document");
        } catch (IOException ioe) {
            LOG.error("Problem writing message: " + ioe.getMessage());
        }
    }
}

From source file:net.smartcosmos.plugin.service.aws.storage.AwsS3StorageService.java

License:Apache License

@Override
public InputStream retrieve(IFile file) throws IOException {
    Preconditions.checkArgument((file != null), "file must not be null");

    AmazonS3 s3 = new AmazonS3Client(credentials, new ClientConfiguration().withProtocol(Protocol.HTTPS));

    GetObjectRequest getObjectRequest = new GetObjectRequest(getBucketName(), file.getFileName());
    S3Object storedObject = s3.getObject(getObjectRequest);

    return storedObject.getObjectContent();
}

From source file:nl.kpmg.lcm.server.data.s3.S3FileAdapter.java

License:Apache License

public S3FileAdapter(S3FileStorage s3Storage, String fileName) {
    String secretAcccessKey;/*w  w  w. jav a  2s.  co  m*/
    secretAcccessKey = s3Storage.getAwsSecretAccessKey();

    AWSCredentials credentials = new BasicAWSCredentials(s3Storage.getAwsAccessKey(), secretAcccessKey);
    AWSStaticCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials);
    s3Client = AmazonS3ClientBuilder.standard().withCredentials(credentialsProvider)
            .withRegion(Regions.EU_WEST_1).build();

    bucketName = s3Storage.getBucketName();
    if (fileName.charAt(0) == '/') { // amazon s3 service don't like "/" in front of the file name
        this.fileName = fileName.substring(1);
    } else {
        this.fileName = fileName;
    }
    object = s3Client.getObject(new GetObjectRequest(bucketName, fileName));
}

From source file:onl.area51.filesystem.s3.S3Retriever.java

License:Apache License

@Override
public void retrieve(char[] path) throws IOException {
    String pathValue = String.valueOf(path);
    try {/*  w w  w .  j a v  a 2s .co m*/
        LOG.log(Level.FINE, () -> "Retrieving " + getBucketName() + ":" + pathValue);
        S3Object obj = getS3().getObject(new GetObjectRequest(getBucketName(), pathValue));
        FileSystemUtils.copyFromRemote(() -> obj.getObjectContent(), getDelegate(), path);
        LOG.log(Level.FINE, () -> "Retrieved " + getBucketName() + ":" + pathValue);
    } catch (AmazonS3Exception ex) {
        LOG.log(Level.FINE, () -> "Error " + ex.getStatusCode() + " " + getBucketName() + ":" + pathValue);
        if (ex.getStatusCode() == 404) {
            throw new FileNotFoundException(pathValue);
        }
        throw new IOException("Cannot access " + pathValue, ex);
    }
}

From source file:org.alanwilliamson.amazon.s3.Read.java

License:Open Source License

private cfData readToFile(AmazonS3 s3Client, String bucket, String key, String localpath, boolean overwrite,
        String aes256key, int retry, int retryseconds) throws Exception {
    File localFile = new File(localpath);
    if (localFile.isFile()) {
        if (!overwrite)
            throw new Exception("The file specified exists: " + localpath);
        else//from ww w .  j  a v  a  2  s. co  m
            localFile.delete();
    }

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {

            GetObjectRequest gor = new GetObjectRequest(bucket, key);
            if (aes256key != null && !aes256key.isEmpty())
                gor.setSSECustomerKey(new SSECustomerKey(aes256key));

            S3Object s3object = s3Client.getObject(gor);

            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(localFile);
                StreamUtil.copyTo(s3object.getObjectContent(), outStream, false);
            } finally {
                StreamUtil.closeStream(outStream);
            }

            return new cfStringData(localFile.toString());

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Read(bucket=" + bucket + "; key=" + key + "; attempt="
                    + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }

    return null; // should never             
}

From source file:org.alanwilliamson.amazon.s3.Read.java

License:Open Source License

private cfData readToMemory(AmazonS3 s3Client, String bucket, String key, String aes256key, int retry,
        int retryseconds) throws Exception {

    // Let us run around the number of attempts
    int attempts = 0;
    while (attempts < retry) {
        try {/*from  w ww.  j  a  v a  2  s. c o m*/

            GetObjectRequest gor = new GetObjectRequest(bucket, key);
            if (aes256key != null && !aes256key.isEmpty())
                gor.setSSECustomerKey(new SSECustomerKey(aes256key));

            S3Object s3object = s3Client.getObject(gor);
            String contentType = s3object.getObjectMetadata().getContentType();

            ByteArrayOutputStream baos = new ByteArrayOutputStream(32000);
            StreamUtil.copyTo(s3object.getObjectContent(), baos, false);

            if (contentType.indexOf("text") != -1 || contentType.indexOf("javascript") != -1) {
                return new cfStringData(baos.toString());
            } else {
                return new cfBinaryData(baos.toByteArray());
            }

        } catch (Exception e) {
            cfEngine.log("Failed: AmazonS3Read(bucket=" + bucket + "; key=" + key + "; attempt="
                    + (attempts + 1) + "; exception=" + e.getMessage() + ")");
            attempts++;

            if (attempts == retry)
                throw e;
            else
                Thread.sleep(retryseconds * 1000);
        }
    }

    return null; // should never 
}