skip to Main Content

I am using com.emc.object.s3. I am trying to list all the key in a S3 bucket, but when I call the listObjects method in S3Client, it raises the following error.

javax.xml.bind.JAXBException: Implementation of JAXB-API has not been
found on module path or classpath. – with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory] javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException:
Implementation of JAXB-API has not been found on module path or
classpath.
with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory] at
com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:115)
at
com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634)

Caused by: javax.xml.bind.JAXBException: Implementation of JAXB-API
has not been found on module path or classpath.
with linked exception: [java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory] at
javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:232) at
javax.xml.bind.ContextFinder.find(ContextFinder.java:375) at
javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691) at
javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632) at
com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getStoredJAXBContext(AbstractJAXBProvider.java:196)
at
com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getJAXBContext(AbstractJAXBProvider.java:188)
at
com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:140)
at
com.sun.jersey.core.provider.jaxb.AbstractJAXBProvider.getUnmarshaller(AbstractJAXBProvider.java:123)
at
com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.readFrom(AbstractRootElementProvider.java:111)
… 96 more Caused by: java.lang.ClassNotFoundException:
com.sun.xml.bind.v2.ContextFactory at
org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:72)
at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1220)
at
javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
at
javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
… 104 more

I tried to add jaxb to build.gradle, but it doesn’t help.

    implementation 'org.glassfish.jaxb:jaxb-runtime:2.3.0-b170127.1453'
    implementation 'javax.activation:activation:1.1'
    implementation 'javax.xml.bind:jaxb-api:2.3.0'

listing method

    public void list() {
        ListObjectsResult objectListing = s3client.listObjects(nBucket);//this line causing the error

        for (S3Object objectSummary : objectListing.getObjects()) {
                System.out.println(" - " + objectSummary.getKey() + "  " +
                        "(size = " + objectSummary.getSize() + ")");
        }

    }

How can I fix the problem? Alternatively, any other methods to list all keys?

2

Answers


  1. USe AWS SDK for Java v2 S3Client. The official AWS SDK for Java V2 S3Client works perfectly in a Spring BOOT web project. Using the AWS Java S3Client, you can avoid all of these dependency issues.

    package com.example.s3;
    
    // snippet-start:[s3.java2.list_objects.import]
    import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.s3.S3Client;
    import software.amazon.awssdk.services.s3.model.ListObjectsRequest;
    import software.amazon.awssdk.services.s3.model.ListObjectsResponse;
    import software.amazon.awssdk.services.s3.model.S3Exception;
    import software.amazon.awssdk.services.s3.model.S3Object;
    import java.util.List;
    // snippet-end:[s3.java2.list_objects.import]
    
    /**
     * Before running this Java V2 code example, set up your development environment, including your credentials.
     *
     * For more information, see the following documentation topic:
     *
     * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
     */
    
    public class ListObjects {
    
        public static void main(String[] args) {
    
            final String usage = "n" +
                "Usage:n" +
                "    <bucketName> nn" +
                "Where:n" +
                "    bucketName - The Amazon S3 bucket from which objects are read. nn" ;
    
            if (args.length != 1) {
                System.out.println(usage);
                System.exit(1);
            }
    
            String bucketName = args[0];
            ProfileCredentialsProvider credentialsProvider = ProfileCredentialsProvider.create();
            Region region = Region.US_EAST_1;
            S3Client s3 = S3Client.builder()
                .region(region)
                .credentialsProvider(credentialsProvider)
                .build();
    
            listBucketObjects(s3, bucketName);
            s3.close();
        }
    
        // snippet-start:[s3.java2.list_objects.main]
        public static void listBucketObjects(S3Client s3, String bucketName ) {
    
            try {
                ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();
    
                ListObjectsResponse res = s3.listObjects(listObjects);
                List<S3Object> objects = res.contents();
                for (S3Object myValue : objects) {
                    System.out.print("n The name of the key is " + myValue.key());
                    System.out.print("n The object is " + calKb(myValue.size()) + " KBs");
                    System.out.print("n The owner is " + myValue.owner());
                }
    
            } catch (S3Exception e) {
                System.err.println(e.awsErrorDetails().errorMessage());
                System.exit(1);
            }
        }
    
        //convert bytes to kbs.
        private static long calKb(Long val) {
            return val/1024;
        }
       // snippet-end:[s3.java2.list_objects.main]
    }
    
    Login or Signup to reply.
  2. It’s always better to use an official AWS SDK instead of 3rd party APIs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search