Codex
Codex

Reputation: 589

AWS S3 : Get Last Modified Timestamp Java

I have written a java code to get the list of files in a AWS S3 bucket's folder as a list of strings. Is there any direct function that I could use to get the last modified timestamp of a file that we see in the s3 buckets.

Upvotes: 10

Views: 28074

Answers (3)

gil.fernandes
gil.fernandes

Reputation: 14601

If you want to know the last modified timestamp of a specific file without listing the whole directory you can use a GetObjectRequest and then retrieve from it the ObjectMetadata. Here is an example:

S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, remoteFilePath));
ObjectMetadata objectMetadata = fullObject.getObjectMetadata();
Date lastModified = objectMetadata.getLastModified();

Upvotes: 6

H6_
H6_

Reputation: 32788

You can get the lastModified as a java.util.Date via the S3ObjectSummary object.

// ...
ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
    .withBucketName("my-bucket")
    .withMaxKeys(1000);

ListObjectsV2Result result = s3client.listObjectsV2(listObjectsV2Request);

for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
    // objectSummary.getLastModified() as java.util.Date
}

Upvotes: 10

koolhead17
koolhead17

Reputation: 1964

You can alternatively use minio-java client library for same. Its Open Source and compatible with AWS S3 API.

Using ListBuckets API, you can easily implement it.

try {
  // List buckets that have read access.
  List bucketList = minioClient.listBuckets();
  for (Bucket bucket : bucketList) {
      System.out.println(bucket.creationDate() + ", " + bucket.name());
  }
} catch (MinioException e) {
  System.out.println("Error occured: " + e);
}

Hope it helps.

Disclaimer: I work for Minio

Upvotes: 0

Related Questions