通过 AWS API Gateway 上传的 PDF 已损坏 [英] PDF Uploaded via AWS API Gateway getting corrupted

查看:44
本文介绍了通过 AWS API Gateway 上传的 PDF 已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用已部署到 AWS API 网关的 C# Web API 通过 Web 表单将 PDF 上传到 S3 存储桶.

I am trying to uploading a PDF via a web form into an S3 bucket using a C# Web API that I have deployed to the AWS API gateway.

这是我的代码:

 private static IAmazonS3 s3Client;


        public static async Task<string> UploadS3FileAsync(string bucketName, IFormFile file, string keyName)  
        {
            try
            {  

                s3Client = new AmazonS3Client();      
                var fileTransferUtility =  new TransferUtility(s3Client);

                //Upload data from a type of System.IO.Stream.
                var stream = file.OpenReadStream();
                var length = (int)stream.Length;
                byte[] data = new byte[length];

                stream.Read(buffer: data, offset: 0, count: length);

               var fileTransferUtilityRequest = new TransferUtilityUploadRequest

                {
                    BucketName = bucketName,
                    InputStream = stream,
                    StorageClass = S3StorageClass.Standard,
                    ContentType = "application/pdf",
                    PartSize = 6291456, // 6 MB.
                    Key = keyName,
                    CannedACL = S3CannedACL.NoACL
                };

                await fileTransferUtility.UploadAsync(fileTransferUtilityRequest); 

                return "success";

            }
            catch (AmazonS3Exception e)
            {
                throw e;
                //Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                throw e;
                //Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }

        }

当我在本地运行该过程时,这非常有效,但是当我将 API 部署到网关时,虽然上传到存储桶中的 PDF 已损坏并且比原始文件大.

This works perfectly when I run the process locally, but when I deploy the API to the gateway, the PDF, although uploaded into the bucket is corrupted and is larger than the original file.

我尝试了各种方法,例如在 API 网关上设置二进制媒体类型、创建流的各种类型.我确定它与编码有关,但我不确定下一步该去哪里.任何帮助将不胜感激.

I have tried various things like setting the Binary Media Type on the API gateway, various types of creating the stream. I'm sure it has something to do with encoding, but I'm not sure where to go next. Any help would be greatly appreciated.

推荐答案

这就是为我解决的方法:

This is what solved it for me:

https://github.com/aws/aws-lambda-dotnet/issues/635

  1. 在二进制媒体类型标题下的设置中,为 multipart/form-data 创建一个条目.
  2. 使用以下配置创建一个新模型.

{
   "$schema": "http://json-schema.org/draft-04/schema#",
   "title": "MediaFileUpload",
   "type": "object",
   "properties": {
   "file": { "type": "string" }
  }
}

  1. 修改方法请求步骤,在请求正文"下添加一个条目;使用模型作为 multipart/form-data 的内容类型.
  1. Modify the method request step adding adding an entry under "Request Body" using the model as a content type of multipart/form-data.

这篇关于通过 AWS API Gateway 上传的 PDF 已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆