我使用aws工具包lambda项目模板创建了aws示例。并将项目发布到Aws。
public class Function
{
IAmazonS3 S3Client { get; set; }
/// <summary>
/// Default constructor. This constructor is used by Lambda to construct the instance. When invoked in a Lambda environment
/// the AWS credentials will come from the IAM role associated with the function and the AWS region will be set to the
/// region the Lambda function is executed in.
/// </summary>
public Function()
{
S3Client = new AmazonS3Client();
}
/// <summary>
/// Constructs an instance with a preconfigured S3 client. This can be used for testing the outside of the Lambda environment.
/// </summary>
/// <param name="s3Client"></param>
public Function(IAmazonS3 s3Client)
{
this.S3Client = s3Client;
}
/// <summary>
/// This method is called for every Lambda invocation. This method takes in an S3 event object and can be used
/// to respond to S3 notifications.
/// </summary>
/// <param name="evnt"></param>
/// <param name="context"></param>
/// <returns></returns>
public async Task<string> FunctionHandler(S3Event evnt, ILambdaContext context)
{
var s3Event = evnt.Records?[0].S3;
if(s3Event == null)
{
return null;
}
try
{
var response = await this.S3Client.GetObjectMetadataAsync(s3Event.Bucket.Name, s3Event.Object.Key);
return response.Headers.ContentType;
}
catch(Exception e)
{
context.Logger.LogLine($"Error getting object {s3Event.Object.Key} from bucket {s3Event.Bucket.Name}. Make sure they exist and your bucket is in the same region as this function.");
context.Logger.LogLine(e.Message);
context.Logger.LogLine(e.StackTrace);
throw;
}
}
}
然后,我使用以下S3 Put
示例测试该功能。
{
"Records": [
{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": "arn:aws:s3:::mybucket",
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
但是,响应出现以下错误?
{
"errorType": "AggregateException",
"errorMessage": "One or more errors occurred. (Error making request with Error Code MovedPermanently and Http Status Code MovedPermanently. No further error information was returned by the service.)",
"stackTrace": [
"at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
],
"cause": {
"errorType": "AmazonS3Exception",
"errorMessage": "Error making request with Error Code MovedPermanently and Http Status Code MovedPermanently. No further error information was returned by the service.",
"stackTrace": [
"at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext executionContext, HttpErrorResponseException exception)",
"at Amazon.Runtime.Internal.ErrorHandler.ProcessException(IExecutionContext executionContext, Exception exception)",
"at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CredentialsRetriever.<InvokeAsync>d__7`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.S3.Internal.AmazonS3ExceptionHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.ErrorCallbackHandler.<InvokeAsync>d__5`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.MetricsHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at StratsLambda.Function.<FunctionHandler>d__6.MoveNext() in C:\\work\\Perforce\\Abs\\Strats\\StratsLambda\\Function.cs:line 64"
],
"cause": {
"errorType": "HttpErrorResponseException",
"errorMessage": "Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.",
"stackTrace": [
"at Amazon.Runtime.Internal.RedirectHandler.HandleRedirect(IExecutionContext executionContext)",
"at Amazon.Runtime.Internal.RedirectHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.Unmarshaller.<InvokeAsync>d__3`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.S3.Internal.AmazonS3ResponseHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1.MoveNext()"
]
}
},
"causes": [ {
"errorType": "AmazonS3Exception",
"errorMessage": "Error making request with Error Code MovedPermanently and Http Status Code MovedPermanently. No further error information was returned by the service.",
"stackTrace": [
"at Amazon.Runtime.Internal.HttpErrorResponseExceptionHandler.HandleException(IExecutionContext executionContext, HttpErrorResponseException exception)",
"at Amazon.Runtime.Internal.ErrorHandler.ProcessException(IExecutionContext executionContext, Exception exception)",
"at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CredentialsRetriever.<InvokeAsync>d__7`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at Amazon.Runtime.Internal.RetryHandler.<InvokeAsync>d__10`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.S3.Internal.AmazonS3ExceptionHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.ErrorCallbackHandler.<InvokeAsync>d__5`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.MetricsHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at StratsLambda.Function.<FunctionHandler>d__6.MoveNext() in C:\\work\\Perforce\\Abs\\Strats\\StratsLambda\\Function.cs:line 64"
],
"cause": {
"errorType": "HttpErrorResponseException",
"errorMessage": "Exception of type 'Amazon.Runtime.Internal.HttpErrorResponseException' was thrown.",
"stackTrace": [
"at Amazon.Runtime.Internal.RedirectHandler.HandleRedirect(IExecutionContext executionContext)",
"at Amazon.Runtime.Internal.RedirectHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.Unmarshaller.<InvokeAsync>d__3`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.S3.Internal.AmazonS3ResponseHandler.<InvokeAsync>d__1`1.MoveNext()",
"--- End of stack trace from previous location where exception was thrown ---",
"at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()",
"at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)",
"at Amazon.Runtime.Internal.ErrorHandler.<InvokeAsync>d__5`1.MoveNext()"
]
}
}
]
}