[英]AWS Lambda Java, write to S3 bucket
我正在創建一個每月運行的 AWS Lambda 函數。 每個月都會處理一些數據並將其寫回 S3 Bucket。
您知道如何將 AWS Lambda Java 中的文件寫入 S3 存儲桶嗎?
是的,您必須在 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");
我建議使用AWS Kinesis FireHose服務,該服務允許將數據作為字符串從AWS SDK 發送。 該服務將為您編寫文件、聚合事件,甚至使用時間戳壓縮文件。
試試這個:
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.