Example usage for com.amazonaws.services.s3 AmazonS3Client getObject

List of usage examples for com.amazonaws.services.s3 AmazonS3Client getObject

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client getObject.

Prototype

@Override
    public ObjectMetadata getObject(final GetObjectRequest getObjectRequest, File destinationFile)
            throws SdkClientException, AmazonServiceException 

Source Link

Usage

From source file:imperial.modaclouds.monitoring.datacollectors.monitors.DetailedCostMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;//from w w w.j  av  a  2  s  .  com

    String secretKey = null;

    ObjectListing objects = null;

    AmazonS3Client s3Client = null;

    String key = null;

    long startTime = 0;

    while (!dcmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {

            cost_nonspot = new HashMap<String, Double>();

            cost_spot = new HashMap<String, Double>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {
                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        bucketName = parameters.get("bucketName");
                        filePath = parameters.get("filePath");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();

            AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
            s3Client = new AmazonS3Client(credentials);

            objects = s3Client.listObjects(bucketName);

            key = "aws-billing-detailed-line-items-with-resources-and-tags-";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String date = sdf.format(new Date());
            key = key + date + ".csv.zip";

        }

        String fileName = null;
        do {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t"
                        + StringUtils.fromDate(objectSummary.getLastModified()));
                if (objectSummary.getKey().contains(key)) {
                    fileName = objectSummary.getKey();
                    s3Client.getObject(new GetObjectRequest(bucketName, fileName),
                            new File(filePath + fileName));
                    break;
                }
            }
            objects = s3Client.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());

        try {
            ZipFile zipFile = new ZipFile(filePath + fileName);
            zipFile.extractAll(filePath);
        } catch (ZipException e) {
            e.printStackTrace();
        }

        String csvFileName = fileName.replace(".zip", "");

        AnalyseFile(filePath + csvFileName);

        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}

From source file:model.data.location.FileAccessFactory.java

License:Apache License

/**
 * Gets the input stream for an S3 file store. This will stream the bytes from S3. Null, or exception will be thrown
 * if an error occurs during acquisition.
 * /*from w w  w .j a  va  2 s. c  o m*/
 * The S3 Credentials MUST be populated using the setCredentials() method before executing this call, or a
 * Credentials exception is likely to be thrown by S3.
 */
@JsonIgnore
public InputStream getS3File(FileLocation fileLocation, String accessKey, String privateKey,
        String s3EncryptKey) {
    // Get the file from S3. Connect to S3 Bucket. Only apply credentials if they are present.
    final AmazonS3Client s3Client;
    S3FileStore fileStore = (S3FileStore) fileLocation;
    if (accessKey.isEmpty() || privateKey.isEmpty()) {
        s3Client = new AmazonS3Client();
    } else {
        // If an encryption key was provided, use the encrypted client
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, privateKey);
        if (s3EncryptKey != null) {
            KMSEncryptionMaterialsProvider materialProvider = new KMSEncryptionMaterialsProvider(s3EncryptKey);
            s3Client = new AmazonS3EncryptionClient(credentials, materialProvider,
                    new CryptoConfiguration().withKmsRegion(Regions.US_EAST_1))
                            .withRegion(Region.getRegion(Regions.US_EAST_1));
        } else {
            s3Client = new AmazonS3Client(credentials);
        }
    }
    S3Object s3Object = s3Client.getObject(fileStore.getBucketName(), fileStore.getFileName());
    return s3Object.getObjectContent();
}

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

public InputStream loadInputStream(String bucketName, String key, boolean canBeNull) throws ApsSystemException {
    InputStream stream = null;//  w  w  w .  ja va2 s. co  m
    try {
        AmazonS3Client client = this.getS3Client();
        S3Object s3Object = client.getObject(bucketName, key);
        InputStream mainIs = s3Object.getObjectContent();
        byte[] bytes = this.getBytes(mainIs);
        stream = new ByteArrayInputStream(bytes);
    } catch (Throwable t) {
        if (!canBeNull) {
            _logger.error("Error loading inputstream : bucket {} - key {}", bucketName, key, t);
            throw new ApsSystemException("Error loading inputstream : bucket " + bucketName + " - key " + key,
                    t);
        }
    }
    return stream;
}

From source file:org.entando.entando.plugins.jps3awsclient.aps.system.services.storage.AmazonS3StorageManager.java

License:Open Source License

public S3Object getS3Object(String bucketName, String key, boolean ignoreErrors) throws ApsSystemException {
    if (!this.isActive()) {
        return null;
    }/*from www.ja va  2s . com*/
    S3Object s3Object = null;
    try {
        AmazonS3Client client = this.getS3Client();
        s3Object = client.getObject(bucketName, key);
    } catch (Throwable t) {
        if (!ignoreErrors) {
            _logger.error("error in getS3Object", t);
            throw new ApsSystemException("Error", t);
        } else {
            return null;
        }
    }
    return s3Object;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  ./*  w  w  w  .  j ava  2s  .  c o  m*/
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getObject(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    Map<String, String> map = new HashMap<String, String>();

    if (!object.getKey().endsWith("/")) {
        String qualifiedPath = "/" + bucketName + "/" + object.getKey();
        map.put("bucketName", object.getBucketName());
        map.put("name", FileUtils.getFilename(qualifiedPath));
        map.put("path", qualifiedPath);
    } else {
        map.put("bucketName", object.getBucketName());
        map.put("name", object.getKey());
        map.put("name", "/" + bucketName + "/" + object.getKey());
    }

    setValue("redirectionLocation", object.getRedirectLocation(), map);
    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return map;
}

From source file:org.openflamingo.fs.s3.S3Utils.java

License:Apache License

/**
 * Object  .//from w  w  w.  j a  v a 2 s. c  om
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 */
public static Map<String, String> getDirectory(AmazonS3Client client, String bucketName, String objectKey) {
    S3Object object = client.getObject(bucketName, objectKey);
    ObjectMetadata objectMetadata = object.getObjectMetadata();

    List<FileInfo> filesList = new ArrayList<FileInfo>();
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(object.getBucketName())
            .withPrefix(objectKey).withDelimiter("/");

    ObjectListing objectListing = null;

    do {
        objectListing = client.listObjects(listObjectsRequest);
        List<String> commonPrefixes = objectListing.getCommonPrefixes();
        List<S3ObjectSummary> summary = objectListing.getObjectSummaries();
        listObjectsRequest.setMarker(objectListing.getNextMarker());
    } while (objectListing.isTruncated());

    Map<String, String> map = new HashMap<String, String>();

    map.put("bucketName", object.getBucketName());
    map.put("name", object.getKey());
    map.put("redirectionLocation", object.getRedirectLocation());

    setValue("version", objectMetadata.getVersionId(), map);
    setValue("contentDisposition", objectMetadata.getContentDisposition(), map);
    setValue("contentType", objectMetadata.getContentType(), map);
    setValue("etag", objectMetadata.getETag(), map);
    setValue("contentEncoding", objectMetadata.getContentEncoding(), map);
    setValue("contentLength", objectMetadata.getContentLength(), map);
    setValue("lastModified", objectMetadata.getLastModified(), map);
    return null;
}

From source file:temp.zAmazonServiceProvider.java

License:BSD License

@Override
public boolean load(final String path, final File file) {

    if (!isAvailable()) {
        return false;
    }/*from   w w  w.j a va 2  s .c  om*/

    try {

        final AmazonS3Client client = amazonConfig.client();
        final String bucket = amazonConfig.bucket();

        final GetObjectRequest request = //
                new GetObjectRequest(bucket, PathHelp.rootLessPath(path));

        final ObjectMetadata result = client.getObject(request, file);

        return true;

    } catch (final Exception e) {

        setAvailable(false);

        log.error("bada-boom", e);

        return false;

    }

}