Darshan
Darshan

Reputation: 31

How to write list of strings to Amazon s3 files using Java

I have to write the List of strings as rows in an Amazon S3 file. For example:

List {str1,st2, str3}

will get converted into file having lines as

str1
str2
str3

I have one approach to do this, i.e covert this list as a string separated with "\n" and write this string to s3. ex

 String s3Strinng =  String.join("\n",
                        myList
                        .stream()
                        .collect(Collectors.toList()));

Is there any other way to do this in bettter way in java? Any code pointers will help a ton.

Thanks in advance.

Upvotes: 1

Views: 3130

Answers (1)

smac2020
smac2020

Reputation: 10734

Your logic looks fine. Once you get the String value, you can place the content into an Amazon S3 bucket by using the Amazon S3 API. Invoke the S3Client object's putObject method.

PutObjectRequest putOb = PutObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectKey)
                    .build();

PutObjectResponse response = s3.putObject(putOb,
                    RequestBody.fromString(<YOUR String>);

Notice RequestBody has this method:

https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/core/sync/RequestBody.html#fromString-java.lang.String-

Upvotes: 3

Related Questions