简体   繁体   English

AWS Lambda Java,写入 S3 存储桶

[英]AWS Lambda Java, write to S3 bucket

I am creating an AWS Lambda function that is runned monthly.我正在创建一个每月运行的 AWS Lambda 函数。 Each month process some data and write that back to S3 Bucket.每个月都会处理一些数据并将其写回 S3 Bucket。

Do you know how you can you write a file from AWS Lambda Java to a S3 bucket?您知道如何将 AWS Lambda Java 中的文件写入 S3 存储桶吗?

Yes, you have to create a text file in S3 bucket and take a reference of the following code to update the file contents as per your requirement.是的,您必须在 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");

The same way you can write a file to S3 from any Java application.与您可以从任何 Java 应用程序将文件写入 S3 的方式相同。 Use the AWS SDK for Java .使用适用于 JavaAWS 开发工具包

I would advise using the AWS Kinesis FireHose service that allows to send data as strings from the AWS SDK .我建议使用AWS Kinesis FireHose服务,该服务允许将数据作为字符串从AWS SDK 发送 The service will write the files for you, aggregate events, and even compress the files with timestamps.该服务将为您编写文件、聚合事件,甚至使用时间戳压缩文件。

Try this:试试这个:

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();
        }

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

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