添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am creating an AWS Lambda function that is runned monthly. Each month process some data and write that back to S3 Bucket.

Do you know how you can you write a file from AWS Lambda Java to a S3 bucket?

You can refer to aws sample at https://docs.aws.amazon.com/AmazonS3/latest/dev/UploadObjSingleOpJava.html Udara S.S Liyanage Apr 17, 2018 at 1:19

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.

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");

I would advise using the AWS Kinesis FireHose service that allows to send data as strings from the AWS SDK. The service will write the files for you, aggregate events, and even compress the files with timestamps.

Kinesis seems overkill to me to just publish a monthly single file to S3. Why not just using aws SDK with an IAM role attached to your lambda function? – Tom Apr 29, 2016 at 15:34 Sure you could just use the SDK to write a file from string. I dont know which amount of data do you have, it might be a problem storing everything in memory and then dumping as a file. – Shimon Tolts Apr 29, 2016 at 16:13 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();

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.