AWS Lambda Java,写入S3存储桶

7

我正在创建一个每月运行的AWS Lambda函数。每个月处理一些数据并将其写回S3存储桶。

您知道如何从AWS Lambda Java向S3存储桶写入文件吗?


1
您可以参考AWS示例,网址为https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html。 - Udara S.S Liyanage
5个回答

3

是的,您需要在S3存储桶中创建一个文本文件,并参考以下代码来根据您的需求更新文件内容。

AmazonS3 client = new AmazonS3Client();
/**
 * @param bucketName
 *            The name of the bucket to place the new object in.
 * @param key
 *            The key of the object to create.
 * @param content
 *            The String to encode
 */
client.putObject("**Bucket Name**", "**File Name to write**", "updated string contents");

1

同样的方法,您可以从任何Java应用程序将文件写入S3。使用AWS SDK for Java


0
我建议使用 AWS Kinesis FireHose服务,它允许从 AWS SDK发送数据作为字符串。 该服务将为您编写文件,聚合事件,并甚至压缩文件并加上时间戳。

4
对我来说,使用Kinesis仅仅为了将每月的单个文件发布到S3似乎有些过度。为什么不只是使用附加到Lambda函数的IAM角色与aws SDK呢? - Tom
当然,您可以使用SDK从字符串编写文件。我不知道您有多少数据,将所有数据存储在内存中,然后转储为文件可能会成为一个问题。 - Shimon Tolts


0

试试这个:

try{
            // Create new file
            Util._logger.log(" Creating file transfer ");

            StringBuilder stringBuilder = new StringBuilder();

            //Writing in file
            stringBuilder.append('Data you want to write in file');

            // Upload file
            ByteArrayInputStream inputStream = new ByteArrayInputStream(stringBuilder.toString().getBytes(Constants.UTF_8));
            s3Client.putObject(dstBucket, uploadFileName, inputStream, new ObjectMetadata());

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AmazonServiceException ase) {
            System.out.println("Caught an AmazonServiceException, " +
                    "which means your request made it " + 
                    "to Amazon S3, but was rejected with an error " +
                    "response for some reason.");
            System.out.println("Error Message:    " + ase.getMessage());
            System.out.println("HTTP Status Code: " + ase.getStatusCode());
            System.out.println("AWS Error Code:   " + ase.getErrorCode());
            System.out.println("Error Type:       " + ase.getErrorType());
            System.out.println("Request ID:       " + ase.getRequestId());
            Util._logger.log(Constants.EXCEPTION_ERROR + ase.getMessage());
            ase.printStackTrace();
        } catch (AmazonClientException ace) {
                System.out.println("Caught an AmazonClientException, " +
                        "which means the client encountered " +
                        "an internal error while trying to " +
                        " communicate with S3, " +
                        "such as not being able to access the network.");
                System.out.println(Constants.EXCEPTION_ERROR + ace.getMessage());
                Util._logger.log(Constants.EXCEPTION_ERROR + ace.getMessage());
                ace.printStackTrace();
        } catch (Exception e) {
                Util._logger.log(Constants.EXCEPTION_ERROR + e.getMessage());
                e.printStackTrace();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接