Search code examples
javaamazon-s3java-8java-7jets3t

Reading LastModifiedDate for aws-s3 via Jets3t fails in Java-8 but succeeds in Java-7


JetS3t fails to get LastModifiedDate for existing S3 object in Java 8 but succeeds in Java 7.

package JsTest

import org.jets3t.service.S3Service;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.model.S3Object;
import org.jets3t.service.security.AWSCredentials;

public class Jetsettester {

public static void main(String[] args) {
    String awsAccessKey = "<access_key>";
    String awsSecretKey = "<secret>";
    AWSCredentials awsCredentials = new AWSCredentials(awsAccessKey, awsSecretKey);
    S3Service s3Service = new RestS3Service(awsCredentials);
    try {
        S3Object objectDetailsOnly = (S3Object) s3Service.getObjectDetails("ENTER_BUCKET_NAME", "EXISTING_SAMPLE_KEY.TXT");
        Object md = objectDetailsOnly.getMetadata("Last-Modified"); // Object type is 'String' in Java 8 - and 'Date' in Java 7
        Date dt = objectDetailsOnly.getLastModifiedDate(); // Hence, raises ClassCastException only in Java 8 . In Java 7 - it is correct Date class
    } catch (ServiceException e) {
        e.printStackTrace();
    }
}
}
  1. Ensure a bucket and key exists in Amazon S3. Add it in the code below along with AWS secret and key
  2. Run the above code using Java 7 it runs successfully. More specifically, the return type of Object md is type String and return type Date dt is Date.
  3. Repeat with Java 8 it fails with a ClassCastException. More specifically, JetS3t is is returning a String date - instead of Date object.

I think there are two header values - one for 'Last-Modified' and other for lowercase 'last-modified' but I dont know more than that. Can someone help?


Solution

  • This was a bug in JetS3t 0.9.3. I raised the bug and James Murty graciously fixed it last year in release 0.9.4 Posting it here in case anyone else experiences it. details bitbucket bug link for jets3t