Exemplo n.º 1
0
        /// <summary>
        /// SyncTest
        /// </summary>
        /// <returns></returns>
        private static void SyncTest()
        {
            StorageNode storageNode = FastDFSClient.GetStorageNodeAsync("group1", "server").GetAwaiter().GetResult();

            string[] files     = Directory.GetFiles("testimage", "*.jpg");
            string[] strArrays = files;
            for (int i = 0; i < strArrays.Length; i++)
            {
                string str1         = strArrays[i];
                var    fileStream   = new FileStream(str1, FileMode.Open);
                var    binaryReader = new BinaryReader(fileStream);
                byte[] numArray;
                try
                {
                    numArray = binaryReader.ReadBytes((int)fileStream.Length);
                }
                finally
                {
                    binaryReader.Dispose();
                }
                var str = FastDFSClient.UploadFileAsync(storageNode, numArray, "jpg", "default").GetAwaiter().GetResult();
                Console.WriteLine(StorageLink + str);
                FastDFSClient.RemoveFileAsync("group1", str, "default").GetAwaiter().GetResult();;
                Console.WriteLine("FastDFSClient.RemoveFile" + str);
            }
        }
        /// <summary>
        /// 存储文件
        /// </summary>
        /// <param name="objectStream">文件流</param>
        /// <param name="objectName">对象名</param>
        /// <returns>文件路径</returns>
        public async Task <string> StoreObjectStreamAsync(Stream objectStream, string objectName)
        {
            StorageNode storageNode = FastDFSClient.GetStorageNodeAsync("group1").GetAwaiter().GetResult();
            var         filePath    = await FastDFSClient.UploadFileAsync(storageNode, objectStream, Path.GetExtension(objectName));

            return(StorageLink + storageNode.GroupName + "/" + filePath);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="contentBytes">比特数组</param>
        /// <param name="imageType">图片类型</param>
        /// <returns>图片地址</returns>
        public static async Task <string> FastDFSUploadFile(byte[] contentBytes, string imageType)
        {
            if (contentBytes == null || contentBytes.Length == 0)
            {
                throw new ArgumentNullException("contentBytes");
            }

            if (string.IsNullOrEmpty(imageType))
            {
                throw new ArgumentNullException("imageType");
            }

            var config = await GetFastDfsConfig();

            EnsureConnectionInitialize(config);

            var group       = config.StorageGroup;
            var storageNode = await FastDFSClient.GetStorageNodeAsync(group);

            string paths = await FastDFSClient.UploadFileAsync(storageNode, contentBytes, imageType);

            StringBuilder resultImageUrl = new StringBuilder();
            var           storageLink    = config.StorageServerLink;

            resultImageUrl.Append(storageLink);
            resultImageUrl.Append(paths);

            return(resultImageUrl.ToString());
        }
        /// <summary>
        /// 存储文件
        /// </summary>
        /// <param name="objectStream">文件流</param>
        /// <param name="objectName">对象名</param>
        /// <param name="groupName">分组</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 + "/" + filePath);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> UploadFileAsync(/*[FromForm] IFormCollection formCollection*/ IFormFile file)
        {
            //IFormFile file = formCollection.Files["file"];
            //IFormFile file = HttpContext.Request.Form.Files["file"];
            //StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync(NodeGroup);
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync();

            string filename = await FastDFSClient.UploadFileAsync(storageNode, file.OpenReadStream(), Path.GetExtension(file.FileName));

            return(Ok(filename));
        }
Exemplo n.º 6
0
        public static async Task <string> UpdateFile(Byte[] content, string fileExt = "jpg", string groupName = "group1")
        {
            if (content == null)
            {
                throw new ArgumentNullException(nameof(content));
            }

            var node = await FastDFSClient.GetStorageNodeAsync(groupName);

            return(await FastDFSClient.UploadFileAsync(node, content, fileExt));
        }
Exemplo n.º 7
0
        /// <summary>
        /// UploadAsync2
        /// </summary>
        /// <param name="storageLink"></param>
        /// <param name="fileBytes"></param>
        /// <returns></returns>
        private static async Task UploadAsync2(string storageLink, byte[] fileBytes)
        {
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "default");

            var str = await FastDFSClient.UploadFileAsync(storageNode, fileBytes, "jpg", "default");

            Console.WriteLine(storageLink + str);

            await FastDFSClient.RemoveFileAsync("group1", str, "default");

            Console.WriteLine("FastDFSClient.RemoveFile" + str);
        }
Exemplo n.º 8
0
        /// <summary>
        /// UploadAsync2
        /// </summary>
        /// <param name="storageLink"></param>
        /// <param name="fileBytes"></param>
        /// <returns></returns>
        private static async Task UploadAsync2(string storageLink, Stream stream)
        {
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1");

            var str = await FastDFSClient.UploadFileAsync(storageNode, stream, "jpg", CancellationToken.None);

            Console.WriteLine(storageLink + str);
            stream.Dispose();

            await FastDFSClient.RemoveFileAsync("group1", str, CancellationToken.None);

            Console.WriteLine("FastDFSClient.RemoveFile" + str);
        }
Exemplo n.º 9
0
        protected static async Task BigFileUploadDownLoad()
        {
            var         temp        = Enumerable.Repeat((byte)99, 1024 * 1024 * 100);
            var         testBytes   = temp.ToArray();
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "default");

            var filename = await FastDFSClient.UploadFileAsync(storageNode, testBytes, "txt", "default");

            using (var fileStream = File.OpenWrite("c:\\fastdfs_test.txt"))
            {
                await FastDFSClient.DownloadFileAsync(storageNode, filename, new StreamDownloadCallback(fileStream), "default");
            }

            await FastDFSClient.RemoveFileAsync("group1", filename, "default");

            temp      = null;
            testBytes = null;
        }
Exemplo n.º 10
0
        /// <summary>
        /// UploadAsync
        /// </summary>
        /// <param name="storageLink"></param>
        /// <returns></returns>
        private static async Task UploadAsync(string storageLink)
        {
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "server");

            string[] files     = Directory.GetFiles("testimage", "*.jpg");
            string[] strArrays = files;
            for (int i = 0; i < strArrays.Length; i++)
            {
                string str1     = strArrays[i];
                var    numArray = GetFileBytes(str1);
                var    str      = await FastDFSClient.UploadFileAsync(storageNode, numArray, "jpg", "default");

                Console.WriteLine(storageLink + str);
                await FastDFSClient.RemoveFileAsync("group1", str, "default");

                Console.WriteLine("FastDFSClient.RemoveFile" + str);
            }
        }
Exemplo n.º 11
0
        public static async Task <string> UpdateFile(Stream fileStream, string fileExt = "jpg", string groupName = "group1")
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            byte[] content = new byte[fileStream.Length];

            using (BinaryReader reader = new BinaryReader(fileStream))
            {
                content = reader.ReadBytes((int)fileStream.Length);
            }

            var node = await FastDFSClient.GetStorageNodeAsync(groupName);

            return(await FastDFSClient.UploadFileAsync(node, content, fileExt));
        }
Exemplo n.º 12
0
        /// <summary>
        /// SyncTest
        /// </summary>
        /// <returns></returns>
        private static void SyncTest()
        {
            StorageNode storageNode = FastDFSClient.GetStorageNodeAsync("group1").GetAwaiter().GetResult();

            string[] files     = Directory.GetFiles("testimage", "*.jpg");
            string[] strArrays = files;
            for (int i = 0; i < strArrays.Length; i++)
            {
                string str1       = strArrays[i];
                var    fileStream = new FileStream(str1, FileMode.Open, FileAccess.Read);

                var str = FastDFSClient.UploadFileAsync(storageNode, fileStream, "jpg", CancellationToken.None
                                                        ).GetAwaiter().GetResult();
                fileStream.Dispose();
                Console.WriteLine(StorageLink + str);
                FastDFSClient.RemoveFileAsync("group1", str, CancellationToken.None).GetAwaiter().GetResult();;
                Console.WriteLine("FastDFSClient.RemoveFile" + str);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// UploadAsync
        /// </summary>
        /// <param name="storageLink"></param>
        /// <returns></returns>
        private static async Task UploadAsync(string storageLink)
        {
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync(group);

            string[] files     = Directory.GetFiles(@"C:\Users\Administrator.DESKTOP-LOM86CH\Desktop\images");
            string[] strArrays = files;
            for (int i = 0; i < strArrays.Length; i++)
            {
                string str1     = strArrays[i];
                var    numArray = GetFileBytes(str1);

                //务必保存此文件路径
                var str = await FastDFSClient.UploadFileAsync(storageNode, numArray, "jpg");

                Console.WriteLine(storageLink + str);
                await FastDFSClient.RemoveFileAsync(group, str);

                Console.WriteLine("FastDFSClient.RemoveFile" + str);
            }
        }
Exemplo n.º 14
0
        private static async Task DownLoadFile()
        {
            var         testBytes   = Encoding.UTF8.GetBytes("12345678911118888888888888888881111111111");
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "default");

            var filename = await FastDFSClient.UploadFileAsync(storageNode, testBytes, "txt", "default");

            var bytes = await FastDFSClient.DownloadFileAsync(storageNode, filename, "default");

            if (bytes == null)
            {
                Console.WriteLine($"DownLoadFile Fail : Bytes null ");
            }
            if (Encoding.UTF8.GetString(bytes) == Encoding.UTF8.GetString(testBytes))
            {
                Console.WriteLine($"DownLoadFile Success");
            }
            else
            {
                throw new ApplicationException("DownLoadFile Fail : Bytes Diff");
            }
        }
Exemplo n.º 15
0
        public static async Task <string> UploadAsync(byte[] fileBytes, string fileExt)
        {
            List <IPEndPoint> pEndPoints = new List <IPEndPoint>()
            {
                new IPEndPoint(IPAddress.Parse(ip), Convert.ToInt32(prot))
            };

            ConnectionManager.Initialize(pEndPoints);

            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync(groupName);

            string str = "";

            try
            {
                str = domainName + '/' + groupName + '/' + await FastDFSClient.UploadFileAsync(storageNode, fileBytes, fileExt);
            }catch (Exception e)
            {
                //失败则两秒后重试
                await Task.Delay(2000);

                try
                {
                    str = domainName + '/' + groupName + '/' + await FastDFSClient.UploadFileAsync(storageNode, fileBytes, fileExt);
                }
                catch (Exception ex)
                {
                    //失败则两秒后重试
                    await Task.Delay(2000);

                    str = domainName + '/' + groupName + '/' + await FastDFSClient.UploadFileAsync(storageNode, fileBytes, fileExt);
                }
            }

            return(str);
        }
Exemplo n.º 16
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="contentBytes">比特数组</param>
        /// <param name="imageType">图片类型</param>
        /// <returns>图片地址</returns>
        public static async Task <string> FastDFSUploadFile(Stream stream, string imageType)
        {
            if (string.IsNullOrEmpty(imageType))
            {
                throw new ArgumentNullException("imageType");
            }

            var config = await GetFastDfsConfig();

            EnsureConnectionInitialize(config);

            var group       = config.StorageGroup;
            var storageNode = await FastDFSClient.GetStorageNodeAsync(group);

            string paths = await FastDFSClient.UploadFileAsync(storageNode, stream, imageType, CancellationToken.None);

            StringBuilder resultImageUrl = new StringBuilder();
            var           storageLink    = config.StorageServerLink;

            resultImageUrl.Append(storageLink);
            resultImageUrl.Append(paths);

            return(resultImageUrl.ToString());
        }