admin管理员组

文章数量:1400080

I am currently reading a json file from s3 location using the below code and then printing the data.

Json File

[
 {
   "name": "A",
   "lastName" : "B"
 },
 {
   "name": "C",
   "lastName" : "D"
 }
]

Reading data from s3

AwsBasicCredentials awsSourceBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
URI sourceUrl = new URI(ep);
S3Client  s3Client= S3Client.builder()
         .credentialsProvider(StaticCredentialsProvider.create(awsSourceBasicCredentials))
         .region(Region.AWS_GLOBAL)
         .endpointOverride(sourceUrl)
         .build();
ResponseInputStream object = s3Client.getObject(request -> request
                                .bucket(bucketName)
                                .key(key));

This ResponseInputStram I am priniting.

Say suppose I have a Name Object. Name.class

class Name
{
 String name;
 String lastName;
}

Is there a way that instead of printing I can create a List<Name>?

I am currently reading a json file from s3 location using the below code and then printing the data.

Json File

[
 {
   "name": "A",
   "lastName" : "B"
 },
 {
   "name": "C",
   "lastName" : "D"
 }
]

Reading data from s3

AwsBasicCredentials awsSourceBasicCredentials = AwsBasicCredentials.create(accessKey, secretKey);
URI sourceUrl = new URI(ep);
S3Client  s3Client= S3Client.builder()
         .credentialsProvider(StaticCredentialsProvider.create(awsSourceBasicCredentials))
         .region(Region.AWS_GLOBAL)
         .endpointOverride(sourceUrl)
         .build();
ResponseInputStream object = s3Client.getObject(request -> request
                                .bucket(bucketName)
                                .key(key));

This ResponseInputStram I am priniting.

Say suppose I have a Name Object. Name.class

class Name
{
 String name;
 String lastName;
}

Is there a way that instead of printing I can create a List<Name>?

Share Improve this question edited Mar 24 at 12:44 chubbsondubs 39.2k25 gold badges109 silver badges142 bronze badges asked Mar 24 at 7:36 Tanya BhandariTanya Bhandari 12 bronze badges 1
  • Again, you seem to be mixing up "ECS location" and "S3 location", just like in your last question. I don't actually see anything in the text of your question that is related to ECS at all, so I'm not sure why you are referring to "ECS location" in your question or tagging your question with amazon-ecs. This question appears to be entirely about S3. – Mark B Commented Mar 24 at 12:10
Add a comment  | 

2 Answers 2

Reset to default 0

So if you just want to read the JSON file and bind that into a Java object you can use Jackson for that:

try( var res = s3Client.getObject(request -> request
                                .bucket(bucketName)
                                .key(key)) ) {
   Reader reader = new InputStreamReader( res, res.response().contentType() );   
   List<Name> names = objectMapper.readValue(reader, new TypeReference<List<Name>>(){});
}

And remember to add the dependency to your build environment:

<!-- https://mvnrepository/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.18.3</version>
</dependency>

Or

// https://mvnrepository/artifact/com.fasterxml.jackson.core/jackson-core
implementation("com.fasterxml.jackson.core:jackson-core:2.18.3")

I implemented something like this:

ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
String jsonString = objectBytes.asUtf8String();
List<Name> fileObjects = mapper.readValue(jsonString, new TypeReference<List<Name>>(){});

本文标签: javaReading a json file from ECS location and transforming it into list of ObjectStack Overflow