我有一个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 =&#34; SampleData.csv&#34 ;;有没有办法在没有指定特定文件名的情况下获取桶内的密钥?
答案 0 :(得分:0)
这些链接会有所帮助。
http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingObjectKeysUsingJava.html
您可以使用前缀和分隔符列出对象,以便在不传递特定文件名的情况下查找您要查找的密钥。
答案 1 :(得分:0)
如果您需要获取S3上的事件详细信息,您实际上可以将s3事件通知程序启用为lambda函数。 Refer the link 您可以通过
启用此功能现在事件详细信息将以lambda函数的形式发送为json格式。你可以从那个json中获取细节。输入将是这样的:
{&#34;记录&#34;:[{&#34; eventVersion&#34;:&#34; 2.0&#34;&#34; EventSource的&#34;:&#34; AWS:S3& #34;&#34; awsRegion&#34;:&#34; AP-南-1&#34;&#34; EVENTTIME&#34;:&#34; 2017-11-23T09:25:54.845Z&# 34;,&#34; eventName的&#34;:&#34; ObjectRemoved:删除&#34;&#34;的UserIdentity&#34; {&#34; principalId&#34;:&#34; AWS:AIDAJASDFGZTLA6UZ7YAK&# 34;}&#34; requestParameters的&#34; {&#34; sourceIPAddress&#34;:&#34; 52.95.72.70&#34;}&#34; responseElements&#34; {&#34;的x AMZ-请求-ID&#34;:&#34; A235BER45D4974E&#34;&#34; X-AMZ-ID-2&#34;:&#34; glUK9ZyNDCjMQrgjFGH0t7Dz19eBrJeIbTCBNI + Pe9tQugeHk88zHOY90DEBcVgruB9BdU0vV8 =&#34;},& #34; S3&#34; {&#34; s3SchemaVersion&#34;:&#34; 1.0&#34;&#34; configurationId&#34;:&#34; SNS&#34;&#34;桶&#34; {&#34;名称&#34;:&#34;例如-bucket1&#34;&#34; ownerIdentity&#34; {&#34; principalId&#34;:&#34; AQFXV36adJU8& #34;}&#34; ARN&#34;:&#34; ARN:AWS:S3 :::例如-bucket1&#34;}&#34;对象&#34; {&#34;键&# 34;:&#34; SampleData.csv&#34;&#34;定序&#34;:&#34; 005A169422CA7CDF66&#34;}}}]}
您可以访问密钥objectname = event['Records'][0]['s3']['object']['key']
(哎呀,这是for python)
然后将此信息发送给RDS。