javaamazon-s3aws-lambdametadatadatecreated

How to read Last Modified Date in a file on S3


i want to read last modified or created date in a s3 file. i have seen the coding to list all files but i wanted to find a specific file's modified date.

i used this coding to retrive the s3 file

S3Object s3ProfileObject = s3Client.getObject(new GetObjectRequest(srcBucket, fullProfilePath));

InputStream profileData = s3ProfileObject.getObjectContent();

SONObject rawProfileObject = (JSONObject)jsonParser.parse( new InputStreamReader(profileData, "UTF-8"));

is it possible to read it using s3ProfileObject ?


Solution

  • You need to use S3ObjectSummary for that. Here is the documentation for it. Below is the sample code (from here):

    final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
    ListObjectsV2Result result = s3.listObjectsV2(bucket_name);
    List<S3ObjectSummary> objects = result.getObjectSummaries();
    for (S3ObjectSummary os: objects) {
        System.out.println("* " + os.getKey());
    }
    

    From S3ObjectSummary, you can make another request and get S3Object.

    If you want to include a specific file or files with specific prefix then you can use `` object with withPrefix method along with the request, e.g.:

    ListObjectsV2Request listObjectsV2Request = new ListObjectsV2Request()
        .withBucketName("test-bucket")
        .withPrefix("file-prefix");
    

    ListObjectsV2Result result = s3client.listObjectsV2(listObjectsV2Request);

    Here's the javadoc for ListObjectsV2Request.