Search code examples
amazon-web-servicesamazon-s3s3-bucket

Accessing specified key from s3 bucket?


I have a S3 bucket xxx. I wrote one lambda function to access data from s3 bucket and writing those details to a RDS PostgreSQL instance. I can do it with my code. I added one trigger to the lambda function for invoking the same when a file falls on s3.

But from my code I can only read file having name 'sampleData.csv'. consider my code given below

public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {

private AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

public LambdaFunctionHandler() {}

// Test purpose only.
LambdaFunctionHandler(AmazonS3 s3) {
    this.s3 = s3;
}

@Override
public String handleRequest(S3Event event, Context context) {
    context.getLogger().log("Received event: " + event);
    String bucket = "xxx";
    String key = "SampleData.csv";




     System.out.println(key);

     try {
         S3Object response = s3.getObject(new GetObjectRequest(bucket, key));
         String contentType = response.getObjectMetadata().getContentType();
         context.getLogger().log("CONTENT TYPE: " + contentType);
      // Read the source file as text
         AmazonS3 s3Client = new AmazonS3Client();
         String body = s3Client.getObjectAsString(bucket, key);
         System.out.println("Body: " + body);
         System.out.println();
         System.out.println("Reading as stream.....");
         System.out.println();

         BufferedReader br = new BufferedReader(new InputStreamReader(response.getObjectContent()));
  // just saving the excel sheet data to the DataBase       
         String csvOutput;
         try { 
            Class.forName("org.postgresql.Driver");
            Connection con = DriverManager.getConnection("jdbc:postgresql://ENDPOINT:5432/DBNAME","USER", "PASSWORD");
            System.out.println("Connected");
            // Checking EOF
         while ((csvOutput = br.readLine()) != null) {
            String[] str = csvOutput.split(",");
            String name = str[1];
            String query = "insert into schema.tablename(name) values('"+name+"')";
            Statement statement = con.createStatement();
            statement.executeUpdate(query);

         }
         System.out.println("Inserted Successfully!!!");
         }catch (Exception ase) {
            context.getLogger().log(String.format(
                     "Error getting object %s from bucket %s. Make sure they exist and"
                     + " your bucket is in the same region as this function.", key, bucket));
            // throw ase;
         }


         return contentType;
     } catch (Exception e) {
         e.printStackTrace();
         context.getLogger().log(String.format(
             "Error getting object %s from bucket %s. Make sure they exist and"
             + " your bucket is in the same region as this function.", key, bucket));
         throw e;
     }
}

From my code you can see that I mentioned key="SampleData.csv"; is there any way to get the key inside a bucket without specifying a specific file name?


Solution

  • These couple of links would be of help.

    http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html

    You can list objects using prefix and delimiter to find the key you are looking for without passing a specific filename.