Example usage for com.amazonaws.services.s3.model ObjectMetadata getLastModified

List of usage examples for com.amazonaws.services.s3.model ObjectMetadata getLastModified

Introduction

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

Prototype

public Date getLastModified() 

Source Link

Document

Gets the value of the Last-Modified header, indicating the date and time at which Amazon S3 last recorded a modification to the associated object.

Usage

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

License:Apache License

/**
 * {@inheritDoc}/*  w  ww.  ja  v a  2s .c om*/
 */
@Override
public long getLastModifiedTime(final String path) throws GenieException {
    final long start = System.nanoTime();
    try {
        final S3Key s3Key = new S3Key(path);
        try {
            final ObjectMetadata o = s3Client.getObjectMetadata(s3Key.getBucket(), s3Key.getKey());
            return o.getLastModified().getTime();
        } catch (final Exception ase) {
            final String message = String.format("Failed getting the metadata of the s3 file %s", path);
            log.error(message);
            throw new GenieServerException(message, ase);
        }
    } finally {
        this.getTimer.record(System.nanoTime() - start, TimeUnit.NANOSECONDS);
    }
}

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

License:Apache License

/**
 * {@inheritDoc}/*from ww w. j  a  v a2 s .  co  m*/
 */
@Override
public long getLastModifiedTime(final String path) throws GenieException {
    final long start = System.nanoTime();
    final long lastModTime;
    final Set<Tag> tags = Sets.newHashSet();
    try {
        final AmazonS3URI s3Uri = this.getS3Uri(path);
        try {
            final ObjectMetadata o = this.s3ClientFactory.getClient(s3Uri).getObjectMetadata(s3Uri.getBucket(),
                    s3Uri.getKey());
            lastModTime = o.getLastModified().getTime();
        } catch (final Exception ase) {
            final String message = String.format("Failed getting the metadata of the s3 file %s", path);
            log.error(message);
            throw new GenieServerException(message, ase);
        }
        MetricsUtils.addSuccessTags(tags);
    } catch (Throwable t) {
        MetricsUtils.addFailureTagsWithException(tags, t);
        throw t;
    } finally {
        this.registry.timer(GET_METADATA_TIMER_NAME, tags).record(System.nanoTime() - start,
                TimeUnit.NANOSECONDS);
    }
    return lastModTime;
}

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

License:Apache License

public static boolean downloadFileIfChangedSince(String bucketName, String bucketFilePrefix, File file,
        long milles, String accountId, String assumeRole, String externalId) {
    AmazonS3Client s3Client = AwsUtils.s3Client;

    try {/*  w  w w.  j  a v  a 2  s.  co m*/
        if (!StringUtils.isEmpty(accountId) && !StringUtils.isEmpty(assumeRole)) {
            Credentials assumedCredentials = getAssumedCredentials(accountId, assumeRole, externalId);
            s3Client = new AmazonS3Client(
                    new BasicSessionCredentials(assumedCredentials.getAccessKeyId(),
                            assumedCredentials.getSecretAccessKey(), assumedCredentials.getSessionToken()),
                    clientConfig);
        }

        ObjectMetadata metadata = s3Client.getObjectMetadata(bucketName, bucketFilePrefix + file.getName());
        boolean download = !file.exists() || metadata.getLastModified().getTime() > milles;

        if (download) {
            return download(s3Client, bucketName, bucketFilePrefix + file.getName(), file);
        } else
            return download;
    } finally {
        if (s3Client != AwsUtils.s3Client)
            s3Client.shutdown();
    }
}

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

License:Apache License

public static boolean downloadFileIfChangedSince(String bucketName, String bucketFilePrefix, File file,
        long milles) {
    ObjectMetadata metadata = s3Client.getObjectMetadata(bucketName, bucketFilePrefix + file.getName());
    boolean download = !file.exists() || metadata.getLastModified().getTime() > milles;

    if (download) {
        return download(bucketName, bucketFilePrefix + file.getName(), file);
    } else//  w w w  .  ja  v  a  2 s  . co  m
        return download;
}

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

License:Apache License

public static boolean downloadFileIfChanged(String bucketName, String bucketFilePrefix, File file,
        long milles) {
    ObjectMetadata metadata = s3Client.getObjectMetadata(bucketName, bucketFilePrefix + file.getName());
    boolean download = !file.exists() || metadata.getLastModified().getTime() > file.lastModified() + milles;
    logger.info("downloadFileIfChanged " + file + " " + metadata.getLastModified().getTime() + " "
            + (file.lastModified() + milles));

    if (download) {
        return download(bucketName, bucketFilePrefix + file.getName(), file);
    } else//from   ww  w.  jav a  2s  .c om
        return false;
}

From source file:com.proofpoint.event.collector.combiner.S3StorageHelper.java

License:Apache License

public static StoredObject updateStoredObject(URI location, ObjectMetadata metadata) {
    Preconditions.checkNotNull(location, "location is null");
    Preconditions.checkNotNull(metadata, "metadata is null");

    return new StoredObject(location, metadata.getETag(), metadata.getContentLength(),
            metadata.getLastModified().getTime());
}

From source file:com.sangupta.urn.service.impl.AmazonS3UrnStorageServiceImpl.java

License:Apache License

@Override
protected UrnObject get(String objectKey) {
    S3Object object = this.client.getObject(this.bucketName, objectKey);
    if (object == null) {
        return null;
    }/*from   w  ww .  j a  v  a2s .c o m*/

    try {
        InputStream stream = object.getObjectContent();

        byte[] bytes = IOUtils.toByteArray(stream);

        UrnObject urnObject = new UrnObject(objectKey, bytes);

        // TODO: read and populate metadata
        ObjectMetadata metadata = object.getObjectMetadata();
        if (metadata != null) {
            if (metadata.getHttpExpiresDate() != null) {
                urnObject.expiry = metadata.getHttpExpiresDate().getTime();
            }

            urnObject.mime = metadata.getContentType();
            urnObject.stored = metadata.getLastModified().getTime();

            // TODO:parse the value to extract the filename if available
            urnObject.name = metadata.getContentDisposition();
        }

        // return the object
        return urnObject;
    } catch (IOException e) {
        // happens when we cannot read data from S3
        LOGGER.debug("Exception reading data from S3 for object key: " + objectKey, e);
        return null;
    } finally {
        if (object != null) {
            try {
                object.close();
            } catch (IOException e) {
                LOGGER.warn("Unable to close S3 object during/after reading the object");
            }
        }
    }
}

From source file:com.upplication.s3fs.util.S3ObjectSummaryLookup.java

License:Open Source License

/**
 * Get the {@link com.amazonaws.services.s3.model.S3ObjectSummary} that represent this Path or her first child if this path not exists
 * @param s3Path {@link com.upplication.s3fs.S3Path}
 * @return {@link com.amazonaws.services.s3.model.S3ObjectSummary}
 * @throws java.nio.file.NoSuchFileException if not found the path and any child
 *//*from  ww w .  ja  va2 s. c o  m*/
public S3ObjectSummary lookup(S3Path s3Path) throws NoSuchFileException {

    /*
     * check is object summary has been cached
     */
    S3ObjectSummary summary = s3Path.fetchObjectSummary();
    if (summary != null) {
        return summary;
    }

    final AmazonS3Client client = s3Path.getFileSystem().getClient();

    /*
     * when `key` is an empty string retrieve the object meta-data of the bucket
     */
    if ("".equals(s3Path.getKey())) {
        ObjectMetadata meta = client.getObjectMetadata(s3Path.getBucket(), "");
        if (meta == null)
            throw new NoSuchFileException("s3://" + s3Path.getBucket());

        summary = new S3ObjectSummary();
        summary.setBucketName(s3Path.getBucket());
        summary.setETag(meta.getETag());
        summary.setKey(s3Path.getKey());
        summary.setLastModified(meta.getLastModified());
        summary.setSize(meta.getContentLength());
        // TODO summary.setOwner(?);
        // TODO summary.setStorageClass(?);
        return summary;
    }

    /*
     * Lookup for the object summary for the specified object key
     * by using a `listObjects` request
     */
    String marker = null;
    while (true) {
        ListObjectsRequest request = new ListObjectsRequest();
        request.setBucketName(s3Path.getBucket());
        request.setPrefix(s3Path.getKey());
        request.setMaxKeys(250);
        if (marker != null)
            request.setMarker(marker);

        ObjectListing listing = client.listObjects(request);
        List<S3ObjectSummary> results = listing.getObjectSummaries();

        if (results.isEmpty()) {
            break;
        }

        for (S3ObjectSummary item : results) {
            if (matchName(s3Path.getKey(), item)) {
                return item;
            }
        }

        if (listing.isTruncated())
            marker = listing.getNextMarker();
        else
            break;
    }

    throw new NoSuchFileException("s3://" + s3Path.getBucket() + "/" + s3Path.toString());
}

From source file:eu.stratosphere.nephele.fs.s3.S3FileSystem.java

License:Apache License

/**
 * {@inheritDoc}//  www.j  a  v a  2 s  .c o  m
 */
@Override
public FileStatus getFileStatus(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);

    // This is the S3:/// base directory
    if (!bop.hasBucket() && !bop.hasObject()) {
        return new S3FileStatus(f, 0L, true, 0L, 0L);
    }

    try {
        if (bop.hasBucket() && !bop.hasObject()) {

            final List<Bucket> buckets = this.s3Client.listBuckets();
            final Iterator<Bucket> it = buckets.iterator();

            // Iterator throw list of buckets to find out creation date
            while (it.hasNext()) {

                final Bucket bucket = it.next();
                if (bop.getBucket().equals(bucket.getName())) {

                    final long creationDate = dateToLong(bucket.getCreationDate());
                    // S3 does not track access times, so this implementation always sets it to 0
                    return new S3FileStatus(f, 0L, true, creationDate, 0L);
                }
            }

            throw new FileNotFoundException("Cannot find " + f.toUri());
        }

        try {
            final ObjectMetadata om = this.s3Client.getObjectMetadata(bop.getBucket(), bop.getObject());
            final long modificationDate = dateToLong(om.getLastModified());
            // S3 does not track access times, so this implementation always sets it to 0
            if (objectRepresentsDirectory(bop.getObject(), om.getContentLength())) {
                return new S3FileStatus(f, 0L, true, modificationDate, 0L);
            } else {
                return new S3FileStatus(f, om.getContentLength(), false, modificationDate, 0L);
            }

        } catch (AmazonServiceException e) {
            if (e.getStatusCode() == HTTP_RESOURCE_NOT_FOUND_CODE) {
                throw new FileNotFoundException("Cannot find " + f.toUri());
            } else {
                throw e;
            }
        }
    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}

From source file:eu.stratosphere.nephele.fs.s3.S3FileSystem.java

License:Apache License

/**
 * {@inheritDoc}/* w w  w .  jav  a 2s  .  com*/
 */
@Override
public FileStatus[] listStatus(final Path f) throws IOException {

    final S3BucketObjectPair bop = this.directoryStructure.toBucketObjectPair(f);

    try {

        if (!bop.hasBucket()) {

            final List<Bucket> list = this.s3Client.listBuckets();
            final S3FileStatus[] array = new S3FileStatus[list.size()];
            final Iterator<Bucket> it = list.iterator();
            int i = 0;
            while (it.hasNext()) {
                final Bucket bucket = it.next();
                final long creationDate = dateToLong(bucket.getCreationDate());
                // S3 does not track access times, so this implementation always sets it to 0
                final S3FileStatus status = new S3FileStatus(
                        extendPath(f, bucket.getName() + S3_DIRECTORY_SEPARATOR), 0, true, creationDate, 0L);
                array[i++] = status;
            }

            return array;
        }

        if (bop.hasBucket() && !bop.hasObject()) {

            // Check if the bucket really exists
            if (!this.s3Client.doesBucketExist(bop.getBucket())) {
                throw new FileNotFoundException("Cannot find " + f.toUri());
            }

            return listBucketContent(f, bop);

        } else {

            final ObjectMetadata omd = this.s3Client.getObjectMetadata(bop.getBucket(), bop.getObject());
            if (objectRepresentsDirectory(bop.getObject(), omd.getContentLength())) {

                return listBucketContent(f, bop);

            } else {
                final S3FileStatus fileStatus = new S3FileStatus(f, omd.getContentLength(), false,
                        dateToLong(omd.getLastModified()), 0L);

                return new FileStatus[] { fileStatus };
            }

        }

    } catch (AmazonClientException e) {
        throw new IOException(StringUtils.stringifyException(e));
    }
}