2017-08-04 42 views
0

我想調用一個由S3Event觸發的lambda函數,我也創建了一個存儲桶,並且還將兩個圖像添加到存儲桶中。執行失敗,同時調用lambda函數

以下是桶的規格。

enter image description here

下面是我的代碼,我已經寫在Java

public String handleRequest(S3Event event, Context context) { 
    context.getLogger().log("Received event: " + event); 

    // Get the object from the event and show its content type 
    String bucket = event.getRecords().get(0).getS3().getBucket().getName(); 
    String key = event.getRecords().get(0).getS3().getObject().getKey(); 
    try { 
     S3Object response = s3.getObject(new GetObjectRequest(bucket, key)); 
     String contentType = response.getObjectMetadata().getContentType(); 
     context.getLogger().log("CONTENT TYPE: " + contentType); 
     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.", bucket, key)); 
     throw e; 
    } 
} 

以下是我得到

com.amazonaws.services.lambda.runtime.events錯誤.S3Event不存在

+0

@oop它沒關係,但我可以有理由嗎? – sparrowTrajon

+0

好的,所以你想downvote在這種情況下? – sparrowTrajon

+0

好的,謝謝:) – sparrowTrajon

回答

1

代碼看起來不錯,確認您已導入此包:

com.amazonaws.services.lambda.runtime.events.S3Event

並實現接口 「RequestHandler」 與你的類。

如果問題仍然存在遵循本教程: AWS Lambda with S3 for real-time data processing

希望這將幫助!