原有系统的附件都是保存在服务器上的,占用了大量的磁盘空间,如今要切换到AWS S3上做文件存储管理。文件打包下载之前的做法是,后台将文件服务器上的多个文件打包到一个压缩文件中,存放在服务器,后端接口返回压缩文件地址供前端下载;为此,特写下这篇文章来记录此处是如何根据文件流来制作压缩包的。
AWS S3更多存取文件的demo需看官方源码,本文中有下载的实例。
需要引用:AWSSDK.Core、AWSSDK.S3,可从NuGet安装;
需要引用:System.IO.Compression,可从NuGet安装;
在ConfigureServices下单例注入AWS类
public void ConfigureServices(IServiceCollection services)
{
.......
services.AddSingleton();
.........
}
Configure下获取AWS实例
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//......//
Aws.Instance = app.ApplicationServices.GetService();
}
AWS类
public class Aws
{
#region Init
private readonly IConfiguration _cfg;
///
/// 存储桶名称
///
private string bucketName;
///
/// aws Id
///
private readonly string awsAccessKeyId;
///
/// aws Id key
///
private readonly string awsSecretAccessKey;
private IAmazonS3 client;
private AmazonS3Config config = new AmazonS3Config()
{
ServiceURL = "http://s3.amazonaws.com",
RegionEndpoint = Amazon.RegionEndpoint.CNNorth1 //账号在哪个区域就用哪个区域,不然会出现账号401的问题
};
#endregion
public Aws(IConfiguration cfg)
{
_cfg = cfg;
awsAccessKeyId = _cfg["AccessKeyId"];
awsSecretAccessKey = _cfg["SecretAccessKey"];
bucketName = _cfg["BucketName"];
client = new AmazonS3Client(awsAccessKeyId, awsSecretAccessKey, config);
}
///
/// AWS文件批量下载
///
/// 文件相对地址(aws key)
///
public async Task BatchDownload(List fileNames)
{
try
{
byte[] res;
using (MemoryStream ms = new MemoryStream())
{
using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
{
foreach (var fileName in fileNames)
{
//通过aws下载文件流
var response = await client.GetObjectAsync(bucketName, newfileName);
var pdfStream= response.ResponseStream;//得到文件流
var pdfBytes = StringHelper.StreamToBytes(pdfStream);//文件流转换成字节
ZipArchiveEntry entry = zip.CreateEntry("abc.pdf");//压缩文件内创建一个文件名为“abc.pdf”,流是什么文件格式就用什么格式,此处我的是pdf格式
using (Stream sw = entry.Open())
{
sw.Write(pdfBytes, 0, pdfBytes.Length);//将文件的字节写到abc.pdf中
}
}
InvokeWriteFile(zip);//重新计算压缩文件的大小,此处参考别人的
int nowPos = (int)ms.Position;
res = new byte[ms.Length];
ms.Position = 0;
ms.Read(res, 0, res.Length);
ms.Position = nowPos;
}
return res;
}
}
catch (Exception ex)
{
throw ex;
}
}
public void InvokeWriteFile(ZipArchive zipArchive)
{
foreach (MethodInfo method in zipArchive.GetType().GetRuntimeMethods())
{
if (method.Name == "WriteFile")
{
method.Invoke(zipArchive, new object[0]);
}
}
}
}
控制器下载压缩文件
///
/// 批量下载
///
///
///
public async Task Packing(List ids)
{
var paths = new List();
//根据Id查询本地对应的文件名称,Id换取文件名称
var bytes = await Aws.Instance.BatchDownload(paths).ToList());
Response.ContentType = "application/octet-stream";
var contentDisposition = "attachment;" + "filename=" + HttpUtility.UrlEncode("XXXX.zip");
Response.Headers.Add("Content-Disposition", new string[] { contentDisposition });
Response.ContentLength = bytes.Length;
Response.Body.Write(bytes, 0, bytes.Length);//发送读取的内容数据到客户端浏览器
Response.Body.Flush();
return new EmptyResult();
}
前端Ajax调用
//后端返回的是文件流
var ids = [1,2,3];
var url = "/PackingByAws";
var form = $("
").attr("action", url).attr("method", "post");后端为List参数
ids.forEach(function (value) {
form.append($("").attr("type", "hidden").attr("name", "ids").attr("value", value));
})
form.appendTo('body').submit().remove();
第一次写文章,从业也有几年了,一直都是拿来主义。这篇文件也是近期项目遇到的特拿来分享,也是怀着无比忐忑的心情才写下来的,写的不好的地方还请见谅。