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

查看:116
本文介绍了通过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网关上设置Binary Media Type,创建流的各种类型.我确定它与编码有关,但是我不确定下一步要去哪里.任何帮助将不胜感激.

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.

推荐答案

您是否已在设置"标签中配置了二进制媒体类型"?

Have you configured the "Binary Media Types" in the settings tab?

API网关默认情况下将所有请求有效负载视为文本数据(即非二进制数据),并对有效负载执行base64编码.您所拥有的较大的PDF应该是包含原始PDF的base64编码版本的文件.

API Gateway by default considers all request payload as text data, i.e., non-binary data, and performs a base64-encoding on the payload. The larger PDF you've got should be a file containing the base64-encoded version of the original PDF.

参考: https://docs .aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

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

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