S3存储桶中的AWS Lambda .net Core 2.1列表文件

时间:2018-12-08 16:36:09

标签: c# amazon-web-services amazon-s3 lambda

正如标题所示,我想在我的lambda函数中列出S3存储桶中的文件(键)。

到目前为止,我有以下内容:

public static async Task < bool > ListObjectsInBucket(string S3_ACCESS_KEY_ID, string S3_SECRET_ACCESS_KEY, string S3_REGION, string S3_BUCKET, string GC_ClientID) {

 try {
  // Create a client
  var regionIdentifier = RegionEndpoint.GetBySystemName(S3_REGION);
  AmazonS3Client client = new AmazonS3Client(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, regionIdentifier);

  // List all objects
  ListObjectsRequest listRequest = new ListObjectsRequest {
   BucketName = S3_BUCKET + "/" + GC_ClientID + "/news-articles",
  };

  ListObjectsResponse listResponse;
  do {
   // Get a list of objects
   listResponse = await client.ListObjectsAsync(listRequest);
   foreach(S3Object obj in listResponse.S3Objects) {
    Console.WriteLine("Object - " + obj.Key);
    Console.WriteLine(" Size - " + obj.Size);
    Console.WriteLine(" LastModified - " + obj.LastModified);
    Console.WriteLine(" Storage class - " + obj.StorageClass);
   }

   // Set the marker property
   listRequest.Marker = listResponse.NextMarker;
  } while (listResponse.IsTruncated);

  return true;

 } catch (Exception ex) {
  Console.WriteLine("Exception:" + ex.Message);
  return false;
 }
}

,以下称呼它:

public string FunctionHandler(ILambdaContext context) {

 var checkFile = ListObjectsInBucket(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_REGION, S3_BUCKET, GC_ClientID);

 return "Complete: " + checkFile;

}

我遇到以下错误:

“完成:System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1+AsyncStateMachineBox 1 [System.Boolean,ExportArticles.Function + d__3]”

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

由于列表调用是异步的,因此需要使函数处理程序异步。现在,您正在从函数处理程序中调用异步列表方法,然后在异步方法完成之前立即返回。您的函数处理程序应该类似于

public async Task<string> FunctionHandler(ILambdaContext context) {

 var checkFile = await ListObjectsInBucket(S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_REGION, S3_BUCKET, GC_ClientID);

 return "Complete: " + checkFile;

}