从Amazon S3读取文件内容:任务已取消错误

时间:2018-11-16 14:47:52

标签: amazon-web-services amazon-s3 asp.net-core stream aws-lambda

我正在尝试通过AWS上的Lambda函数从S3存储桶读取.csv文件。

代码

private string GetCsvContentFromS3(S3Event s3Event)
{
  string responseBody = string.Empty;

  try
  {
    S3EventNotification.S3EventNotificationRecord eventRecord = s3Event.Records.First();
    string bucketName = eventRecord.S3.Bucket.Name;
    string keyName = eventRecord.S3.Object.Key;

    _logger.LogTrace(EventIds.AFileHasBeenUploaded, () => $"A file has been uploaded: Bucket Name: {bucketName}, Key Name: {keyName}");

    GetObjectRequest request = new GetObjectRequest
    {
      BucketName = bucketName,
      Key = keyName
    };

    AmazonS3Client s3Client = new AmazonS3Client(BucketRegion);

    _logger.LogInfo(
        EventIds.AmazonS3ClientInitialized,
        () => $"AmazonS3Client has been initialized with Region: {s3Client.Config.RegionEndpoint.DisplayName}");

    using (GetObjectResponse response = s3Client.GetObjectAsync(request).Result)
    using (Stream responseStream = response.ResponseStream)
    using (StreamReader reader = new StreamReader(responseStream))
    {
      responseBody = reader.ReadToEnd(); // Now you process the response body.
    }
  }
  catch (AmazonS3Exception e)
  {
    _logger.LogCritical(
        EventIds.SomethingWentWrong,
        () => $"AmazonS3Exception. Exception Message: {e.Message}");
  }
  catch (Exception e)
  {
    _logger.LogCritical(
        EventIds.SomethingWentWrong,
        () => $"Exception. Exception Message: {e.Message}");
  }

  return responseBody;
}

代码会抛出Exception(try / catch中的最后一个catch),并显示以下消息:

Exception. Exception Message: One or more errors occurred. (A task was canceled.)

我想念什么?

1 个答案:

答案 0 :(得分:0)

DevOps整理了一些奇怪的权限问题。