I have an api that takes in an IFormFile and converts it to a pdf, When running locally everything works but when hosed as a lambda function and API gateway the same file (.doc with 1 line of random text) returns a UnsupportedFileFormatException
[Route("DocToPdf")]
[HttpPost]
public IActionResult DocToPdf(IFormFile file)
{
if (file == null || file.Length == 0)
{
return BadRequest("Invalid File");
}
if (!file.ContentType.Contains(".document") &&
file.ContentType != "application/msword")
{
return BadRequest("Unsupported File type");
}
byte[] pdf = _aspose.ConvertFileToPdf(file);
return File(pdf, "application/pdf");
}
public byte[] ConvertFileToPdf(IFormFile file)
{
using (Stream stream = file.OpenReadStream())
{
return ConvertStreamToPdf(stream);
}
}
public byte[] ConvertStreamToPdf(Stream stream)
{
var doc = new Document(stream);
using (MemoryStream ms = new MemoryStream())
{
doc.Save(ms, SaveFormat.Pdf);
return ms.ToArray();
}
}
Aspose.Words.UnsupportedFileFormatException: Unsupported file format: Unknown at Aspose.Words.Document. (Stream , LoadOptions ) at Aspose.Words.Document. (Stream , LoadOptions ) at Aspose.Words.Document…ctor(Stream stream, LoadOptions loadOptions) at Aspose.Words.Document…ctor(Stream stream) at DocumentService.AsposeService.ConvertStreamToPdf(Stream stream) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\AsposeService.cs:line 31 at DocumentService.AsposeService.ConvertFileToPdf(IFormFile file) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\AsposeService.cs:line 25 at DocumentService.Controllers.ConvertController.DocToPdf(IFormFile file) in C:\Repos\Codifi.Tools\DocumentService\DocumentService\DocumentService\Controllers\ConvertController.cs:line 47 at lambda_method(Closure , Object , Object[] ) at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() — End of stack trace from previous location where exception was thrown — at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() — End of stack trace from previous location where exception was thrown — at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context) HEADERS ======= Accept: / Accept-Encoding: gzip, deflate, br CloudFront-Forwarded-Proto: https CloudFront-Is-Desktop-Viewer: true CloudFront-Is-Mobile-Viewer: false CloudFront-Is-SmartTV-Viewer: false CloudFront-Is-Tablet-Viewer: false CloudFront-Viewer-Country: GB Content-Type: multipart/form-data; boundary=--------------------------…Host: …execute-api.eu-west-2.amazonaws.com Postman-Token: xxxx-f9e1-xxx-9298-xxxxxUser-Agent: PostmanRuntime/7.26.8 Via: 1.1 …cloudfront.net (CloudFront) X-Amz-Cf-Id: xs8c78-xxxxx== X-Amzn-Trace-Id: Root=1-5ff889da-42dd27ce5498ae817ee63b18 X-Forwarded-For: 31.49.171.96, 70.132.38.82 X-Forwarded-Port: 443 X-Forwarded-Proto: https Content-Length: 28658