2016-04-29 36 views
2

私は毎月実行されるAWSラムダ関数を作成しています。毎月いくつかのデータを処理し、それをS3 Bucketに書き戻します。AWS Lambda Java、S3バケットに書き込む

AWS Lambda JavaからS3バケットにファイルを書き込む方法をご存知ですか?

答えて

1

JavaアプリケーションからS3にファイルを書き込むのと同じ方法です。 AWS SDK for Javaを使用してください。

0

AWS SDKから文字列としてデータを送信できるようにするAWS Kinesis FireHoseサービスを使用することをお勧めします。 サービスはあなたのためにファイルを書き、イベントを集約し、タイムスタンプでファイルを圧縮します。

+2

キネシスはちょうどS3に毎月1つのファイルをパブリッシュするために私には行き過ぎと思われます。ラムダ関数にIAMロールを添付してaws SDKを使用するのはなぜですか? – Tom

+0

もちろん、SDKを使用して文字列からファイルを書き込むこともできます。私はあなたが持っているデータの量を知っていません、それはメモリにすべてを格納し、ファイルとしてダンプする問題かもしれません。 –

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(); 
     } 
関連する問題