title: Read and write to a file in Amazon s3 bucket
body: <p>I need to read a large (>15mb) file (say <code>sample.csv</code>) from an Amazon S3 bucket. I then need to process the data present in <code>sample.csv</code> and keep writing it to another directory in the S3 bucket. I intend to use an AWS Lambda function to run my java code. </p>
  
  <p>As a first step I developed java code that runs on my local system. The java code reads the <code>sample.csv</code> file from the S3 bucket and I used the <code>put</code> method to write data back to the S3 bucket. But I find only the last line was processed and put back.</p>
  
  <pre><code>Region clientRegion = Region.Myregion;    
  AwsBasicCredentials awsCreds = AwsBasicCredentials.create("myAccessId","mySecretKey");    
  S3Client s3Client = S3Client.builder().region(clientRegion).credentialsProvider(StaticCredentialsProvider.create(awsCreds)).build();    
  ResponseInputStream&lt;GetObjectResponse&gt; s3objectResponse = s3Client.getObject(GetObjectRequest.builder().bucket(bucketName).key("Input/sample.csv").build());    
  BufferedReader reader = new BufferedReader(new InputStreamReader(s3objectResponse));    
  String line = null;
  while ((line = reader.readLine()) != null) {
                  s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key("Test/Testout.csv").build(),RequestBody.fromString(line));
  }
  </code></pre>
  
  <p>Example: sample.csv contains</p>
  
  <pre><code>1,sam,21,java,beginner;
  2,tom,28,python,practitioner;
  3,john,35,c#,expert.
  </code></pre>
  
  <p>My output should be </p>
  
  <pre><code>1,mas,XX,java,beginner;
  2,mot,XX,python,practitioner;
  3,nhoj,XX,c#,expert. 
  </code></pre>
  
  <p>But only <code>3,nhoj,XX,c#,expert</code> is written in the <code>Testout.csv</code>.</p>