Amazon web services SAM无服务器应用程序中出现异常:(访问被拒绝):AggregateException
我有一个sam应用程序,它声明S3 bucket、lambda函数和DynamoDB表,当CSV文件上载到S3 bucket时,它会触发lambda函数,该函数解析记录并将其存储到DynamoDB表中,在我将CSV文件上载到S3 bucket后,会触发lambda函数,但我有一个异常,即: 发生了一个或多个错误。(拒绝访问):AggregateException 在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken CancellationToken) 在lambda_方法下(闭包、流、流、LambdaContenternal) 在Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext executionContext,HttpErrorResponseException异常)中 在Amazon.Runtime.Internal.ErrorHandler.ProcessException(IExecutionContext executionContext,Exception)中 在Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.EndpointDiscoveryHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.CredentialsRetriever.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.RetryHandler.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.CallbackHandler.InvokeAsync[T](IExecutionContext executionContext)上 位于Amazon.S3.Internal.AmazonS3ExceptionHandler.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.ErrorCallbackHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.MetricsHandler.InvokeAsync[T](IExecutionContext executionContext)上 在E:\Azavar Repositories\Lata 3\Lata\Code\Microservices\市政业务\src\市政业务\process市政业务\process市政业务上载.Handler(S3EventNotification s3Event)中的Localgov.MunicipalityBusiness.process市政业务上载.Handler中:第39行 引发了类型为“Amazon.Runtime.Internal.HttpErrorResponseException”的异常。示例:HttpErrorResponseException 在Amazon.Runtime.HttpWebRequestMessage.GetResponseAsync(CancellationToken CancellationToken) 在Amazon.Runtime.Internal.HttpHandler`1.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.RedirectHandler.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.Runtime.Internal.Unmarshaller.InvokeAsync[T](IExecutionContext executionContext)上 在Amazon.S3.Internal.AmazonS3ResponseHandler.InvokeAsync[T](IExecutionContext executionContext) 在Amazon.Runtime.Internal.ErrorHandler.InvokeAsync[T](IExecutionContext executionContext)上 我的sam代码是Amazon web services SAM无服务器应用程序中出现异常:(访问被拒绝):AggregateException,amazon-web-services,aws-lambda,aws-sam,Amazon Web Services,Aws Lambda,Aws Sam,我有一个sam应用程序,它声明S3 bucket、lambda函数和DynamoDB表,当CSV文件上载到S3 bucket时,它会触发lambda函数,该函数解析记录并将其存储到DynamoDB表中,在我将CSV文件上载到S3 bucket后,会触发lambda函数,但我有一个异常,即: 发生了一个或多个错误。(拒绝访问):AggregateException 在System.Threading.Tasks.Task.Wait(Int32毫秒计时,CancellationToken Cance
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Sample SAM Template for serverless-lata
Globals:
Function:
Timeout: 10
Resources:
ResultsTable:
Type: AWS::Serverless::SimpleTable
ProcessMunicipalityBusinessUpload:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src/MunicipalityBusiness/
Handler: Localgov.MunicipalityBusiness::Localgov.MunicipalityBusiness.ProcessMunicipalityBusinessUpload::Handler
Runtime: dotnetcore2.1
Description: Parse CSV text and insert it into DynamoDB
MemorySize: 512
Timeout: 30
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "s3:GetObject"
Resource: "arn:aws:s3:::*"
- Effect: Allow
Action:
- "dynamodb:GetItem"
- "dynamodb:PutItem"
- "dynamodb:DeleteItem"
- "dynamodb:UpdateItem"
- "dynamodb:Scan"
- "dynamodb:Query"
- "dynamodb:BatchWriteItem"
- "dynamodb:BatchGetItem"
- "dynamodb:DescribeTable"
- "dynamodb:ConditionCheckItem"
Resource:
"Fn::Join":
- ""
- - "arn:aws:dynamodb:"
- Ref: "AWS::Region"
- ":"
- Ref: "AWS::AccountId"
- ":table/"
- Ref: ResultsTable
Events:
BucketEvent1:
Type: S3
Properties:
Bucket:
Ref: SourceCSVBucket
Events:
- "s3:ObjectCreated:*"
SourceCSVBucket:
Type: "AWS::S3::Bucket"
DynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "municipalityId"
AttributeType: "S"
- AttributeName: "bId"
AttributeType: "S"
KeySchema:
- AttributeName: "municipalityId"
KeyType: "HASH"
- AttributeName: "bId"
KeyType: "RANGE"
BillingMode: PAY_PER_REQUEST
GlobalSecondaryIndexes:
- IndexName: GetMunicipalityBusinessGSI
KeySchema:
- AttributeName: bId
KeyType: HASH
- AttributeName: municipalityId
KeyType: RANGE
Projection:
ProjectionType: ALL
我的DotNet文件是:
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using System.IO;
using CsvHelper;
using System.Globalization;
using Amazon.S3.Util;
using Amazon.S3.Model;
using Amazon;
using Amazon.S3;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using System.Collections.Generic;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace Localgov.MunicipalityBusiness
{
public class ProcessMunicipalityBusinessUpload
{
public async Task Handler(S3EventNotification s3Event)
{
//The region should be dynamic, I will fix it
RegionEndpoint bucketRegion = RegionEndpoint.USEast1;
IAmazonS3 client = new AmazonS3Client(bucketRegion);
var bucketName = s3Event.Records[0].S3.Bucket.Name;
var keyName = s3Event.Records[0].S3.Object.Key;
string responseBody = "";
GetObjectRequest request = new GetObjectRequest
{
BucketName = bucketName,
Key = keyName,
};
using (GetObjectResponse response = await client.GetObjectAsync(request))
using (Stream responseStream = response.ResponseStream)
using (StreamReader reader = new StreamReader(responseStream))
{
responseBody = reader.ReadToEnd(); // Now you process the response body.
LambdaLogger.Log(responseBody.ToString());
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<Business>();
foreach (var item in records)
{
Business business = (Business)item;
var request1 = new PutItemRequest
{
//this should be dynamic name, nut I didn't know how to import it
TableName = "sam-app2-DynamoDBTable-1JC98R13C6X31",
Item = new Dictionary<string, AttributeValue>
{
{ "municipalityId", new AttributeValue { N = "1" }},
{ "bId", new AttributeValue { S = business.Fein_ss }},
{ "dba", new AttributeValue { S = business.Dba }},
{ "businessName", new AttributeValue { S = business.BusinessName }},
{ "businessAddress", new AttributeValue { S = business.BusinessAddress }},
{ "stateTaxId", new AttributeValue { S = business.StateTaxId }},
{ "fein_ss", new AttributeValue { S = business.Fein_ss }},
{ "licesne", new AttributeValue { S = business.Licesne }},
{ "mailingAddress", new AttributeValue { S = business.MailingAddress }},
{ "dateClosed", new AttributeValue { S = business.DateClosed }},
{ "businessCity", new AttributeValue { S = business.BusinessCity }},
{ "businessState", new AttributeValue { S = business.BusinessState }},
{ "businessZip", new AttributeValue { S = business.BusinessZip }},
}
};
var client2 = new AmazonDynamoDBClient();
await client2.PutItemAsync(request1);
}
}
}
}
}
}
使用System.Threading.Tasks;
使用Amazon.Lambda.Core;
使用System.IO;
使用CsvHelper;
利用制度全球化;
使用Amazon.S3.Util;
使用Amazon.S3.0模型;
使用亚马逊;
使用Amazon.S3;
使用Amazon.DynamoDBv2;
使用Amazon.DynamoDBv2.Model;
使用System.Collections.Generic;
//Assembly属性,使Lambda函数的JSON输入能够转换为.NET类。
[程序集:LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
名称空间Localgov.MunicipalityBusiness
{
公共类进程市政业务上载
{
公共异步任务处理程序(S3EventNotification s3Event)
{
//该地区应该是动态的,我会解决它
RegionEndpoint bucketRegion=RegionEndpoint.USEast1;
IAmazonS3客户端=新Amazons3客户端(bucketRegion);
var bucketName=s3Event.Records[0].S3.Bucket.Name;
var keyName=s3Event.Records[0].S3.Object.Key;
字符串responseBody=“”;
GetObjectRequest=新的GetObjectRequest
{
BucketName=BucketName,
Key=keyName,
};
使用(GetObjectResponse-response=await client.GetObjectAsync(请求))
使用(Stream responseStream=response.responseStream)
使用(StreamReader=新StreamReader(responseStream))
{
responseBody=reader.ReadToEnd();//现在处理响应主体。
Log(responseBody.ToString());
使用(var csv=new CsvReader(reader,CultureInfo.InvariantCulture))
{
var records=csv.GetRecords();
foreach(记录中的var项目)
{
业务=(业务)项;
var request1=新的PutItemRequest
{
//这应该是动态名称,我不知道如何导入它
TableName=“sam-app2-DynamoDBTable-1JC98R13C6X31”,
Item=新字典
{
{“市政属性ID”,新属性值{N=“1”},
{“bId”,新属性值{S=business.Fein_ss},
{“dba”,新属性值{S=business.dba},
{“businessName”,新属性值{S=business.businessName},
{“businessAddress”,新属性值{S=business.businessAddress},
{“stateTaxId”,新属性值{S=business.stateTaxId},
{“fein_ss”,新属性值{S=business.fein_ss},
{“licesne”,新属性值{S=business.licesne