【问题标题】:How to stream a file in aws lambda using c#如何使用 c# 在 aws lambda 中流式传输文件
【发布时间】:2020-01-25 21:00:36
【问题描述】:

我正在使用文件流上传证据文件以进行条带化,但应用程序托管在不支持文件流的 aws lambda 中。

这是我的代码

public async Task<IActionResult> PostFile(D.StripeFilePurpose stripeFilePurpose)
        {
            IFormFile file = Request.Form.Files[0];

            var fileName = ContentDispositionHeaderValue.Parse(
                file.ContentDisposition).FileName.Trim('"');

            var path = string.Empty;
            var webRootPath = _hostingEnvironment.WebRootPath;

            if (string.IsNullOrEmpty(webRootPath))
            {
                path = Directory.GetCurrentDirectory();
            }

            string fileId;

            var filePath = Path.Combine(path, fileName);


            using (var fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                file.CopyTo(fileStream);
            }

            using (var stream = new FileStream(filePath, FileMode.Open))
            {

                var stripeFileUpload = await _stripeDisputeService
                     .UploadFileAsync(
                         fileName,
                         stream,
                         stripeFilePurpose.GetDescription());

                fileId = stripeFileUpload.Id;

            }

            return StatusCode(200, fileId);
        }

每当指定文件路径时,lamba 都会将其附加到 /var/task/**mypath。

我什至硬编码文件路径仍然在文件路径之前附加 /var/task。我搜索并发现只有将文件存储在 /tmp 文件夹(lambda)中才能进行流式传输..

如何做到这一点??

【问题讨论】:

    标签: c# amazon-web-services aws-lambda stripe-payments


    【解决方案1】:

    您可以尝试使用 MemoryStream。

        public async Task<IActionResult> PostFile(D.StripeFilePurpose stripeFilePurpose)
        {
            IFormFile file = Request.Form.Files[0];
    
            var fileName = file.FileName.Trim('"');
            using MemoryStream memStream = new MemoryStream();
            await file.CopyToAsync(memStream);
            memStream.Position = 0;
    
    
            var stripeFileUpload = await _stripeDisputeService
                     .UploadFileAsync(
                         fileName,
                         memStream,
                         stripeFilePurpose.GetDescription());
    
            fileId = stripeFileUpload.Id;
    
    
    
            return StatusCode(200, fileId);
        }
    

    它会在服务中消耗更多内存,但避免使用磁盘。

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 2011-02-27
      相关资源
      最近更新 更多