简体   繁体   English

如何将 AWS S3 存储桶 stream 响应转换为 .NET Core 中的文件

[英]How to Convert AWS S3 bucket stream response into a file in .NET Core

I created a utility class for Amazon AWS S3 bucket related stuff so that I can reuse the utility in different controllers.我为 Amazon AWS S3 存储桶相关内容创建了一个实用程序 class,以便我可以在不同的控制器中重用该实用程序。

I am trying to get the file from S3 bucket I am able to fetch the file as a stream, now I want convert it into an original file, but when I am trying to return the File , I am getting an error我正在尝试从 S3 存储桶中获取文件 我能够以 stream 的形式获取文件,现在我想将其转换为原始文件,但是当我尝试返回File时,出现错误

Non-invocable member 'File' cannot be used like a method不可调用成员“文件”不能像方法一样使用

Is there a way to convert stream into a file through a class not from the controller.有没有办法通过 class 而不是 controller 将 stream 转换成文件。

Here is my code:这是我的代码:

public async Task GetFileFromBucket(string fileName)
{
      var response = await client.GetObjectAsync(_bucket, fileName);
      return File(response.ResponseStream, response.Headers["Content-Type"]);
}

Is there a way to convert stream into a file through a class not from the Controller有没有办法通过 class 而不是 Controller 将 stream 转换成文件

The File Contents is either a string or a stream:文件内容是字符串或 stream:

public async Task<string> GetFileFromBucket(string fileName)
{
      var response = await client.GetObjectAsync(_bucket, fileName);
      return new File(response.ResponseStream, response.Headers["Content-Type"]).ReadAllText();
}

If you want the S3 bucket or file meta data then call a different S3 API. I'm assuming you want the file contents.如果您想要 S3 存储桶或文件元数据,请调用另一个 S3 API。我假设您想要文件内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM