C# .NET中的AWS Lambda函数在为s3 bucket创建新文件时抛出错误(无法确定内容长度)
我正在使用GN.NETCore2开发AWS lambda函数。这是我第一次使用.NET开发AWS lambda函数。我现在尝试做的很简单,读取现有文件并用新名称(新名称)保存文件。读取文件工作正常。但是当我使用PutObjectRequest保存文件时,它给了我一个错误 这是我的处理函数代码C# .NET中的AWS Lambda函数在为s3 bucket创建新文件时抛出错误(无法确定内容长度),c#,.net,amazon-web-services,amazon-s3,aws-lambda,C#,.net,Amazon Web Services,Amazon S3,Aws Lambda,我正在使用GN.NETCore2开发AWS lambda函数。这是我第一次使用.NET开发AWS lambda函数。我现在尝试做的很简单,读取现有文件并用新名称(新名称)保存文件。读取文件工作正常。但是当我使用PutObjectRequest保存文件时,它给了我一个错误 这是我的处理函数代码 public async Task<string> FunctionHandler(S3Event evnt, ILambdaContext context) {
public async Task<string> FunctionHandler(S3Event evnt, ILambdaContext context)
{
var s3Event = evnt.Records?[0].S3;
if(s3Event == null)
{
return null;
}
try
{
if(s3Event.Object.Key.ToLower().Contains("thumb"))
{
//Console.WriteLine("The image is already a thumb file");
return "The file is aready a thumb image file";
}
string[] pathSegments = s3Event.Object.Key.Split('/');
string eventCode = pathSegments[0];
string userEmail = pathSegments[1];
string filename = pathSegments[2];
string extension = Path.GetExtension(filename); //.jpeg with "dot"
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filename);
using(var objectResponse = await this.S3Client.GetObjectAsync(s3Event.Bucket.Name, s3Event.Object.Key))
{
using(Stream responseStream = objectResponse.ResponseStream)
{
//This code is throwing error
await this.S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = s3Event.Bucket.Name,
Key = fileNameWithoutExtension + ".thumb" + extension,
InputStream = responseStream
});
}
}
return "Thumbnail version of the image has been created";
}
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;
}
}
公共异步任务FunctionHandler(S3Event evnt,ILambdaContext上下文)
{
var s3Event=evnt.Records?[0].S3;
if(s3Event==null)
{
返回null;
}
尝试
{
if(s3Event.Object.Key.ToLower()包含(“thumb”))
{
//WriteLine(“图像已经是一个拇指文件”);
返回“该文件是一个拇指图像文件”;
}
string[]pathSegments=s3Event.Object.Key.Split('/');
字符串eventCode=pathSegments[0];
字符串userEmail=pathSegments[1];
字符串文件名=路径段[2];
字符串扩展名=Path.GetExtension(文件名);//.jpeg,带“点”
字符串fileNameWithoutExtension=Path.GetFileNameWithoutExtension(文件名);
使用(var objectResponse=wait this.S3Client.GetObjectAsync(s3Event.Bucket.Name,s3Event.Object.Key))
{
使用(Stream responseStream=objectResponse.responseStream)
{
//此代码正在抛出错误
等待这个.S3Client.PutObjectAsync(新的Amazon.S3.Model.PutObjectRequest
{
BucketName=s3Event.Bucket.Name,
Key=filename不带outextension+“.thumb”+扩展名,
InputStream=responseStream
});
}
}
返回“已创建图像的缩略图版本”;
}
捕获(例外e)
{
context.Logger.LogLine($“从bucket{s3Event.bucket.Name}获取对象{s3Event.object.Key}时出错。请确保它们存在并且您的bucket与此函数位于同一区域。”);
context.Logger.LogLine(e.Message);
context.Logger.LogLine(如StackTrace);
投掷;
}
}
当我运行测试函数时,它给了我这个错误
{
"errorType": "AggregateException",
"errorMessage": "One or more errors occurred. (Could not determine content length)",
"stackTrace": [
"at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
],
"cause": {
"errorType": "AmazonS3Exception",
"errorMessage": "Could not determine content length",
"stackTrace": [
"at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.GetStreamWithLength(Stream baseStream, Int64 hintLength)",
"at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest putObjectRequest)",
"at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext)",
"at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext)",
"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 AwsS3Lambda.Function.<FunctionHandler>d__10.MoveNext() in C:\\Users\\Acer\\Desktop\\MementoCSharp\\memento-dotnet\\AwsS3Lambda\\Function.cs:line 99"
]
},
"causes": [
{
"errorType": "AmazonS3Exception",
"errorMessage": "Could not determine content length",
"stackTrace": [
"at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.GetStreamWithLength(Stream baseStream, Int64 hintLength)",
"at Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest putObjectRequest)",
"at Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext)",
"at Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext)",
"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 AwsS3Lambda.Function.<FunctionHandler>d__10.MoveNext() in C:\\Users\\Acer\\Desktop\\MementoCSharp\\memento-dotnet\\AwsS3Lambda\\Function.cs:line 99"
]
}
]
}
{
“errorType”:“AggregateException”,
“errorMessage”:“发生了一个或多个错误。(无法确定内容长度)”,
“stackTrace”:[
“位于System.Threading.Tasks.Task`1.GetResultCore(布尔waitCompletionNotification)”,
“在lambda_方法(闭包、流、流、lambdacontexternal)”
],
“原因”:{
“errorType”:“AmazonS3Exception”,
“errorMessage”:“无法确定内容长度”,
“stackTrace”:[
“在Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshalller.GetStreamWithLength(流基流,Int64 hintLength)”中,
“在Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest PutObjectRequest)”中,
“在Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext executionContext)”中,
“在Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext executionContext)上”,
“在Amazon.Runtime.Internal.CallbackHandler.d_u9`1.MoveNext()”,
“---引发异常的上一个位置的堆栈结束跟踪----”,
“在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中,
“在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)”中,
“在Amazon.S3.Internal.AmazonS3ExceptionHandler.d_u1`1.MoveNext()”,
“---引发异常的上一个位置的堆栈结束跟踪----”,
“在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中,
“在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)”中,
“在Amazon.Runtime.Internal.ErrorCallbackHandler.d_u5`1.MoveNext()”,
“---引发异常的上一个位置的堆栈结束跟踪----”,
“在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中,
“在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)”中,
“在Amazon.Runtime.Internal.MetricsHandler.d_u1`1.MoveNext()”,
“---引发异常的上一个位置的堆栈结束跟踪----”,
“在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中,
“在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)”中,
“在C:\\Users\\Acer\\Desktop\\MementoCSharp\\memento dotnet\\AwsS3Lambda\\Function.d_u10.MoveNext()中的AwsS3Lambda.Function.d_u10.MoveNext()。cs:第99行”
]
},
“原因”:[
{
“errorType”:“AmazonS3Exception”,
“errorMessage”:“无法确定内容长度”,
“stackTrace”:[
“在Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshalller.GetStreamWithLength(流基流,Int64 hintLength)”中,
“在Amazon.S3.Model.Internal.MarshallTransformations.PutObjectRequestMarshaller.Marshall(PutObjectRequest PutObjectRequest)”中,
“在Amazon.Runtime.Internal.Marshaller.PreInvoke(IExecutionContext executionContext executionContext)”中,
“在Amazon.Runtime.Internal.Marshaller.InvokeAsync[T](IExecutionContext executionContext executionContext)上”,
“在Amazon.Runtime.Internal.CallbackHandler.d_u9`1.MoveNext()”,
“---引发异常的上一个位置的堆栈结束跟踪----”,
“在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中,
“在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)”中,
“在Amazon.S3.Internal.AmazonS3ExceptionHandler.d_u1`1.MoveNext()”,
“---来自上一个IOU的堆栈结束跟踪
await this.S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = s3Event.Bucket.Name,
Key = fileNameWithoutExtension + ".thumb" + extension,
InputStream = responseStream
});
await this.S3Client.PutObjectAsync(new Amazon.S3.Model.PutObjectRequest
{
BucketName = s3Event.Bucket.Name,
Key = fileNameWithoutExtension + ".thumb" + extension,
InputStream = responseStream,
Headers.ContentLength = s3event.object.size
});