简体   繁体   English

如何使用 Java 将字符串列表写入 Amazon s3 文件

[英]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.我必须将字符串列表编写为 Amazon S3 文件中的行。 For example:例如:

List {str1,st2, str3}列表 {str1,st2, str3}

will get converted into file having lines as将被转换为具有以下行的文件

str1
str2
str3

I have one approach to do this, ie covert this list as a string separated with "\\n" and write this string to s3.我有一种方法可以做到这一点,即将这个列表转换为一个用“\\n”分隔的字符串并将这个字符串写入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?有没有其他方法可以在java中以更好的方式做到这一点? Any code pointers will help a ton.任何代码指针都会有很大帮助。

Thanks in advance.提前致谢。

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.获得 String 值后,您可以使用 Amazon S3 API 将内容放入 Amazon S3 存储桶中。 Invoke the S3Client object's putObject method.调用S3Client对象的putObject方法。

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

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

Notice RequestBody has this method:注意RequestBody有这个方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM