上传成功,访问下面的地址
这里的微服务是基于abp vnext的。只是在应用层进行相关的服务开发。这里需要说明下,这里我是使用工厂模式,根据配置来创建对应的文件操作提供者,进而对对应的文件服务器上的文件进行操作的。这里我只摘录FastDFS相关的代码供大家参考。
第一步 安装对应的nuget包这里我用的是更新时间相对比较近下载量相对比较多的作者的nuget包,
Install-Package FastDFSNetCore -Version 1.2.5 第二步,编写一个FastDFSProvider具体的代码如下所示:
/// <summary> /// 功能介绍:FastDFS提供者 /// 作者:lzhu /// 创建日期:2020/4/23 19:54:35 /// </summary> public class FastDFSProvider : IStorageProvider, ITransientDependency { /// <summary> /// 提供程序名称 /// </summary> public string ProviderName => "FastDFS"; //配置 private readonly FastDFSConfig _fastDFSConfig; /// <summary> /// 构造函数 /// </summary> /// <param>注入配置数据</param> public FastDFSProvider(FastDFSConfig fastDFSConfig) { _fastDFSConfig = fastDFSConfig ?? throw new ArgumentNullException(nameof(fastDFSConfig)); string[] trackers = fastDFSConfig.Trackers.Split(',', StringSplitOptions.RemoveEmptyEntries); var trackerIPs = new List<IPEndPoint>(); foreach (var onetracker in trackers) { trackerIPs.Add(new IPEndPoint(IPAddress.Parse(onetracker), fastDFSConfig.Port)); } ConnectionManager.Initialize(trackerIPs); } /// <summary> /// 通过文件名删除对应的文件 /// </summary> /// <param>对象名称</param> /// <param>分组名称</param> /// <returns>是否成功</returns> public async Task<bool> DeleteObjectByNameAsync(string objectName, string groupName = "") { RestoreFileName(objectName, out string fileName, out groupName); await FastDFSClient.RemoveFileAsync(groupName, fileName); return true; } /// <summary> /// 获取网关连接 /// </summary> /// <returns>网关连接</returns> public Task<string> GetGateWayUrl() => Task.FromResult(_fastDFSConfig.GateWayUrl); /// <summary> /// 获取对象二进制数组 /// </summary> /// <param>组名称</param> /// <param>对象名称</param> /// <returns>对象的二进制数组</returns> public async Task<byte[]> GetObjectByteAsync(string objectName, string groupName = "") { RestoreFileName(objectName, out string fileName, out groupName); var storageNode = await GetStorageNodeAsync(groupName); return await FastDFSClient.DownloadFileAsync(storageNode, fileName); } /// <summary> /// 获取对象流 /// </summary> /// <param>组名称</param> /// <param>对象名称</param> /// <returns>对象流</returns> public async Task<Stream> GetObjectStreamAsync(string objectName, string groupName = "") { RestoreFileName(objectName, out string fileName, out groupName); var storageNode = await GetStorageNodeAsync(groupName); FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, fileName); Stream stream = new MemoryStream(); int limit = 1024 * 100;//每次获取100KB的数据 //如果文件大小大于100KB 分次写入 if (fileInfo.FileSize >= limit) { long offset = 0; long len = limit; while (len > 0) { byte[] buffer = await FastDFSClient.DownloadFileAsync(storageNode, fileName, offset, len); stream.Write(buffer, 0, int.Parse(len.ToString())); stream.Flush(); offset += len; len = (fileInfo.FileSize - offset) >= limit ? limit : (fileInfo.FileSize - offset); } } else { //如果文件大小小小于100KB 直接写入文件 byte[] buffer = await FastDFSClient.DownloadFileAsync(storageNode, fileName); stream.Write(buffer, 0, buffer.Length); stream.Flush(); } stream.Seek(0, SeekOrigin.Begin); return stream; } /// <summary> /// 存储文件 /// </summary> /// <param>对象</param> /// <param>对象名</param> /// <param>分组</param> /// <returns></returns> public async Task<string> StoreObjectByteAsync(byte[] objectByte, string objectName, string groupName = "") { var storageNode = await GetStorageNodeAsync(""); var filePath = await FastDFSClient.UploadFileAsync(storageNode, objectByte, Path.GetExtension(objectName)); return storageNode.GroupName + "http://www.likecs.com/" + filePath; } /// <summary> /// 存储文件 /// </summary> /// <param>文件流</param> /// <param>对象名</param> /// <param>分组</param> /// <returns>文件路径</returns> public async Task<string> StoreObjectStreamAsync(Stream objectStream, string objectName, string groupName = "") { var storageNode = await GetStorageNodeAsync(""); var filePath = await FastDFSClient.UploadFileAsync(storageNode, objectStream, Path.GetExtension(objectName)); return storageNode.GroupName + "http://www.likecs.com/" + filePath; } } 第三步 在分布式配置中心中进行配置