【问题标题】:Image Upload s3 bucket form deployed dot net cor AWS lambda serverless app not working (broken Image)从部署 dotnet 核心 AWS lambda 无服务器应用程序上传 s3 存储桶不工作(损坏的图像)
【发布时间】:2019-07-23 22:45:17
【问题描述】:

当我从我的本地 dot net core aws lambda 无服务器应用程序发出将图像文件上传到 aws s3 存储桶的发布请求时,它可以工作,但在我部署的应用程序中,图像仍然上传到 s3 存储桶,但图像损坏(显示黑色空图像)

这里是代码

    [HttpPut("PostImageFile")]
    public async Task FileImageAsync(string  Id)
    {
        var s3Client = new AmazonS3Client("*******", "*******", Amazon.RegionEndpoint.USEast1);

        try
        {
            var httpRequest = HttpContext.Request;
            //posted file
            var file = httpRequest.Form.Files[0];            

            byte[] fileBytes = new byte[file.Length];
            file.OpenReadStream().Read(fileBytes, 0, Int32.Parse(file.Length.ToString()));

            var fileName = Guid.NewGuid() + file.FileName;

            PutObjectResponse response = null;

            using (var stream = new MemoryStream())
            {
                file.CopyTo(stream);

                var request = new PutObjectRequest
                {
                    BucketName = "imageakal",
                    Key = fileName,
                    InputStream = stream,

                    ContentType = file.ContentType,
                    CannedACL = S3CannedACL.PublicReadWrite
                };

                response = await s3Client.PutObjectAsync(request);
            };
        }
        catch (Exception ex)
        {
            Console.Write("Upload Failed: " + ex.Message);
        }
    }

【问题讨论】:

  • 你有我们可以看到的图片的链接吗?
  • 是的,当我从本地服务器 imgfile1.s3.amazonaws.com/… 发出 post 请求时,这个是同一个图像文件,当我从部署的服务器 imgfile1.s3.amazonaws.com/… 发出 post 请求时,这个是同一个图像文件而且我还注意到文件增加了
  • 酷,你能在你的 C# lambda 中包含你的 put 代码
  • 我现在无法访问我的电脑,我将发布代码大约一两个小时
  • 好吧,我只是发布代码看看

标签: amazon-web-services amazon-s3 .net-core


【解决方案1】:

如果没有更多详细信息,我猜您的 AWS 设置可能有一个允许/拒绝域的列表。我会检查您的 AWS 实例是否配置为允许来自您的域的请求。

【讨论】:

    【解决方案2】:

    只需将“multipart/form-data”放在 Api Gateway 设置选项卡的“二进制媒体类型”部分,然后部署它(不要忘记)。

    【讨论】:

      猜你喜欢
      • 2022-08-17
      • 2020-07-15
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      相关资源
      最近更新 更多