Exemplo n.º 1
0
        protected static async Task BigFileAppendUploadDownLoad()
        {
            var         temp        = Enumerable.Repeat((byte)99, 1024 * 1024 * 100);
            var         testBytes   = temp.ToArray();
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync("group1", "default");


            var filename = await FastDFSClient.UploadAppenderFileAsync(storageNode, testBytes.Take(1024 * 1024 * 2).ToArray(), "txt", "default");

            for (int i = 0; i < 49; i++)
            {
                FDFSFileInfo fileInfo = await FastDFSClient.GetFileInfoAsync(storageNode, filename, "default");

                var appendBytes = testBytes.Skip((int)fileInfo.FileSize).Take(1024 * 1024 * 2).ToArray();
                await FastDFSClient.AppendFileAsync("group1", filename, appendBytes, "default");
            }

            using (var fileStream = File.OpenWrite("c:\\fastdfs_test.txt"))
            {
                for (int i = 0; i < 50; i++)
                {
                    var buffer = await FastDFSClient.DownloadFileAsync(storageNode, filename, "default",
                                                                       1024 * 1024 * 2 *i,
                                                                       1024 * 1024 * 2);

                    fileStream.Write(buffer, 0, buffer.Length);
                }
            }

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

            temp      = null;
            testBytes = null;
        }
        /// <summary>
        /// 获取对象二进制数组
        /// </summary>
        /// <param name="groupName">组名称</param>
        /// <param name="objectName">对象名称</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));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> GetFileAsync([FromRoute] string filename)
        {
            filename = HttpUtility.UrlDecode(filename);
            //StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync(NodeGroup);
            StorageNode storageNode = await FastDFSClient.GetStorageNodeAsync();

            byte[] fileContent = await FastDFSClient.DownloadFileAsync(storageNode, filename);

            return(File(fileContent, "application/octet-stream"));
        }
Exemplo n.º 4
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.º 5
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");
            }
        }
        /// <summary>
        /// 获取对象流
        /// </summary>
        /// <param name="groupName">组名称</param>
        /// <param name="objectName">对象名称</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);
        }