public async Task <FileDownloadResponse> DownloadAsync(FileDownloadRequest filedownloadRequest)
        {
            CloudBlobContainer cloudBlobContainer = GetBlobContainerConfig(filedownloadRequest.UserId);
            bool isContainerExists = await cloudBlobContainer.ExistsAsync();

            if (!isContainerExists)
            {
                throw new ResourceNotExistsException($"userId: {filedownloadRequest.FileName} does not exists");
            }
            //Get blob reference
            var  blobReference = cloudBlobContainer.GetBlockBlobReference(filedownloadRequest.FileName);
            bool isExists      = await blobReference.ExistsAsync();

            if (!isExists)
            {
                throw new ResourceNotExistsException($"Provided File {filedownloadRequest.FileName} does not exists");
            }
            var stream = new MemoryStream();
            await blobReference.DownloadToStreamAsync(stream);

            stream.Position = 0;
            FileDownloadResponse response = new FileDownloadResponse {
                ContentType = ContentTypeHelper.GetContentType(blobReference.Name), MemoryStream = stream
            };

            return(response);
        }
Esempio n. 2
0
        private static async Task Run(MinioClient minio, string userBucketName, string uploadFilePath, string saveFileName)
        {
            var bucketName = userBucketName; //桶名
            var location   = "us-east-1";    //地址
            var filePath   = uploadFilePath; //上传文件路径
            //var objectName = "device1/"+"20210107/"+saveFileName;//保存文件名
            //var objectName = "device3" + "/" +levelTwo + "/" + saveFileName;
            var objectName  = "LK500" + "/" + DateTime.Now.ToShortDateString().ToString() + "/" + saveFileName;
            var contentType = ContentTypeHelper.GetContentType(saveFileName.Substring(saveFileName.LastIndexOf('.') + 1));
            var file        = new FileInfo(uploadFilePath);

            try
            {
                var found = await minio.BucketExistsAsync(bucketName);//判断是否存在桶名

                if (!found)
                {
                    await minio.MakeBucketAsync(bucketName, location);
                }

                _minioClient.SetTraceOn(new LogHelper());//我们在上传开始的时候,打开日志,通过日志抛出的块编号来计算出当前进度

                ViewModelLocator.FileUploadViewModel.FileSize   = file.Length;
                ViewModelLocator.FileUploadViewModel.TotalParts = file.Length / App.MinimumPartSize + 1;//计算出文件总块数

                //20210109 插入内容,如果不存在二级、三级内容及内容,直接创建并插入,如果存在二级、三级及对应内容则不插入,并返回标识
                ObjectStat statObject = await PutObject_Tester(minio, bucketName, objectName, filePath, contentType);

                Assert.IsTrue(statObject != null);
                Assert.IsTrue(statObject.MetaData != null);

                //await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);//上传文件
                //await minio.PutObjectAsync();//上传文件

                Debug.WriteLine("Successfully uploaded " + objectName);
            }
            catch (MinioException e)
            {
                App.NewNLog.Error($"File Upload Error: {e}");
                Debug.WriteLine($"File Upload Error: {e.Message}");
            }
            finally
            {
                _minioClient.SetTraceOff();
            }
        }
Esempio n. 3
0
        public async Task <FileDownloadResponse> DownloadAsync(FileDownloadRequest fileDownloadReq)
        {
            var uploads  = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", fileDownloadReq.UserId, "uploads");
            var filePath = Path.Combine(uploads, fileDownloadReq.FileName);

            if (!System.IO.File.Exists(filePath))
            {
                return(null);
            }
            var memory = new MemoryStream();

            using (var stream = new FileStream(filePath, FileMode.Open))
            {
                await stream.CopyToAsync(memory);
            }
            memory.Position = 0;

            return(new FileDownloadResponse {
                MemoryStream = memory, ContentType = ContentTypeHelper.GetContentType(filePath)
            });
        }
Esempio n. 4
0
        /// <summary>
        /// 20210310 保存
        /// </summary>
        /// <param name="minio"></param>
        /// <param name="userBucketName"></param>
        /// <param name="uploadFilePath"></param>
        /// <param name="saveFileName"></param>
        /// <returns></returns>
        private static async Task Run(MinioClient minio, string userBucketName, string uploadFilePath, string saveFileName)
        {
            var bucketName  = userBucketName;
            var location    = "us-east-1";
            var objectName  = saveFileName;
            var filePath    = uploadFilePath;
            var contentType = ContentTypeHelper.GetContentType(saveFileName.Substring(saveFileName.LastIndexOf('.') + 1));
            var file        = new FileInfo(uploadFilePath);

            try
            {
                var found = await minio.BucketExistsAsync(bucketName);

                if (!found)
                {
                    await minio.MakeBucketAsync(bucketName, location);
                }

                _minioClient.SetTraceOn(new LogHelper());//我们在上传开始的时候,打开日志,通过日志抛出的块编号来计算出当前进度

                ViewModelLocator.Instance.FileUploadViewModel.FileSize   = file.Length;
                ViewModelLocator.Instance.FileUploadViewModel.TotalParts = file.Length / App.MinimumPartSize + 1;//计算出文件总块数

                //上传文件
                await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);

                Debug.WriteLine("Successfully uploaded " + objectName);
            }
            catch (MinioException e)
            {
                App.NewNLog.Error($"File Upload Error: {e}");
                Debug.WriteLine($"File Upload Error: {e.Message}");
            }
            finally
            {
                _minioClient.SetTraceOff();
            }
        }