AWS Lambda Java,写入S3存储桶

时间:2016-04-29 13:57:42

标签: java amazon-web-services

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

您知道如何将AWS Lambda Java中的文件写入S3存储桶吗?

5 个答案:

答案 0 :(得分:1)

您可以从任何Java应用程序将文件写入S3。使用AWS SDK for Java

答案 1 :(得分:1)

是的,您必须在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");

答案 2 :(得分:0)

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

答案 3 :(得分: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();
        }

答案 4 :(得分:0)