0

我有一个 S3 存储桶 xxx。我编写了一个 lambda 函数来访问来自 s3 存储桶的数据并将这些详细信息写入 RDS PostgreSQL 实例。我可以用我的代码做到这一点。我在 lambda 函数中添加了一个触发器,用于在文件落在 s3 上时调用它。

但从我的代码中,我只能读取名为“sampleData.csv”的文件。考虑下面给出的我的代码

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;
     }
}

从我的代码中你可以看到我提到了 key="SampleData.csv"; 有没有办法在不指定特定文件名的情况下获取存储桶中的密钥?

4

2 回答 2

0

如果您需要获取 S3 上的事件详细信息,您实际上可以启用 s3 事件通知器到 lambda 功能。请参阅链接 您可以通过以下方式启用此功能,

  1. 单击存储桶中的“属性”
  2. 点击“活动”
  3. 点击“添加通知”
  4. 命名并选择事件类型(例如,放置、删除等)
  5. 如有必要,提供前缀和后缀,否则留空以考虑所有事件
  6. 然后“发送到”Lambda 函数并提供 Lambda ARN。

现在事件详细信息将以 json 格式发送 lambda 函数。您可以从该 json 中获取详细信息。输入将是这样的:

{"记录":[{"eventVersion":"2.0","eventSource":"aws:s3","awsRegion":"ap-south-1","eventTime":"2017-11-23T09:25: 54.845Z","eventName":"ObjectRemoved:Delete","userIdentity":{"principalId":"AWS:AIDAJASDFGZTLA6UZ7YAK"},"requestParameters":{"sourceIPAddress":"52.95.72.70"},"responseElements": {"x-amz-request-id":"A235BER45D4974E","x-amz-id-2":"glUK9ZyNDCjMQrgjFGH0t7Dz19eBrJeIbTCBNI+Pe9tQugeHk88zHOY90DEBcVgruB9BdU0vV8="},"s3":{"s3SchemaVersion":"1.0","configurationId": "sns","bucket":{"name":"example-bucket1","ownerIdentity":{"principalId":"AQFXV36adJU8"},"arn":"arn:aws:s3:::example-bucket1"},"object":{"key":"SampleData.csv","sequencer":"005A169422CA7CDF66"}}}] }

您可以访问密钥objectname = event['Records'][0]['s3']['object']['key'](糟糕,这是用于 python),然后将此信息发送到 RDS。

于 2017-12-28T13:05:16.403 回答
0

这几个链接会有所帮助。

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

您可以使用前缀和分隔符列出对象以查找您要查找的键,而无需传递特定的文件名。

于 2017-12-28T12:59:01.943 回答