Example usage for com.amazonaws.services.s3.model S3Object getObjectMetadata

List of usage examples for com.amazonaws.services.s3.model S3Object getObjectMetadata

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model S3Object getObjectMetadata.

Prototype

public ObjectMetadata getObjectMetadata() 

Source Link

Document

Gets the metadata stored by Amazon S3 for this object.

Usage

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

License:Apache License

@Override
public synchronized S3Object getObject(String bucket, String key) throws Exception {
    S3Object s3Object = uploads.get(key);
    if (s3Object != null) {
        S3Object copy = new S3Object();
        copy.setKey(key);//from w ww .j ava2  s.  c o m
        copy.setObjectMetadata(s3Object.getObjectMetadata());

        String bytesIndexStr = s3Object.getObjectMetadata().getUserMetadata().get(BYTES_HEADER);
        if (bytesIndexStr != null) {
            S3ObjectInputStream objectContent = new S3ObjectInputStream(
                    new ByteArrayInputStream(uploadedBytes.get(Integer.parseInt(bytesIndexStr))), null);
            copy.setObjectContent(objectContent);
        }

        return copy;
    }
    return s3Object;
}

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

License:Apache License

@Override
public ObjectMetadata getObjectMetadata(String bucket, String key) throws Exception {
    S3Object s3Object = uploads.get(key);
    if (s3Object != null) {
        return s3Object.getObjectMetadata();
    }//w  ww  .j a  v  a 2 s .  c  o  m
    return null;
}

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  .j  a va 2  s . co 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.exhibitor.core.config.s3.S3ConfigProvider.java

License:Apache License

private S3Object getConfigObject() throws Exception {
    try {/*from   w  w  w. j  av  a2  s  .c o m*/
        S3Object object = s3Client.getObject(arguments.getBucket(), arguments.getKey());
        if (object.getObjectMetadata().getContentLength() > 0) {
            return object;
        }
    } catch (AmazonS3Exception e) {
        if (!isNotFoundError(e)) {
            throw e;
        }
    }
    return null;
}

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 {// w  w  w. java  2  s .  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 {//w w w . j  av  a 2  s. co m
        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);
}

From source file:com.netflix.spinnaker.front50.model.S3StorageService.java

License:Apache License

@Override
public <T extends Timestamped> T loadObject(ObjectType objectType, String objectKey) throws NotFoundException {
    try {/*  w  w  w  . j a v  a 2s. co  m*/
        S3Object s3Object = amazonS3.getObject(bucket,
                buildS3Key(objectType.group, objectKey, objectType.defaultMetadataFilename));
        T item = deserialize(s3Object, (Class<T>) objectType.clazz);
        item.setLastModified(s3Object.getObjectMetadata().getLastModified().getTime());
        return item;
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            throw new NotFoundException("Object not found (key: " + objectKey + ")");
        }
        throw e;
    } catch (IOException e) {
        throw new IllegalStateException("Unable to deserialize object (key: " + objectKey + ")", e);
    }
}

From source file:com.netflix.spinnaker.front50.model.S3StorageService.java

License:Apache License

@Override
public <T extends Timestamped> Collection<T> listObjectVersions(ObjectType objectType, String objectKey,
        int maxResults) throws NotFoundException {
    try {/*from www .  ja va2  s.  com*/
        VersionListing versionListing = amazonS3.listVersions(new ListVersionsRequest(bucket,
                buildS3Key(objectType.group, objectKey, objectType.defaultMetadataFilename), null, null, null,
                maxResults));
        return versionListing.getVersionSummaries().stream().map(s3VersionSummary -> {
            try {
                S3Object s3Object = amazonS3.getObject(new GetObjectRequest(bucket,
                        buildS3Key(objectType.group, objectKey, objectType.defaultMetadataFilename),
                        s3VersionSummary.getVersionId()));
                T item = deserialize(s3Object, (Class<T>) objectType.clazz);
                item.setLastModified(s3Object.getObjectMetadata().getLastModified().getTime());
                return item;
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }).collect(Collectors.toList());
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            throw new NotFoundException(String.format("No item found with id of %s", objectKey.toLowerCase()));
        }

        throw e;
    }
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

License:Apache License

public T findById(String id) throws NotFoundException {
    try {//w  ww .j  av  a2 s  . co m
        S3Object s3Object = amazonS3.getObject(bucket, buildS3Key(id));
        T item = deserialize(s3Object);
        item.setLastModified(s3Object.getObjectMetadata().getLastModified().getTime());
        return item;
    } catch (IOException e) {
        throw new IllegalStateException(e);
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            throw new NotFoundException(String.format("No item found with id of %s", id.toLowerCase()));
        }

        throw e;
    }
}

From source file:com.netflix.spinnaker.front50.model.S3Support.java

License:Apache License

public Collection<T> allVersionsOf(String id, int limit) throws NotFoundException {
    try {//from w w w .  j av a  2s . co m
        VersionListing versionListing = amazonS3
                .listVersions(new ListVersionsRequest(bucket, buildS3Key(id), null, null, null, limit));
        return versionListing.getVersionSummaries().stream().map(s3VersionSummary -> {
            try {
                S3Object s3Object = amazonS3.getObject(
                        new GetObjectRequest(bucket, buildS3Key(id), s3VersionSummary.getVersionId()));
                T item = deserialize(s3Object);
                item.setLastModified(s3Object.getObjectMetadata().getLastModified().getTime());
                return item;
            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }).collect(Collectors.toList());
    } catch (AmazonS3Exception e) {
        if (e.getStatusCode() == 404) {
            throw new NotFoundException(String.format("No item found with id of %s", id.toLowerCase()));
        }

        throw e;
    }
}