Пример #1
0
        static void Main(string[] args)
        {
            /// Note: s3 AccessKey and SecretKey needs to be added in App.config file
            /// See instructions in README.md on running examples for more information.
            var minio = new MinioClient(ConfigurationManager.AppSettings["Endpoint"],
                                        ConfigurationManager.AppSettings["AccessKey"],
                                        ConfigurationManager.AppSettings["SecretKey"]).WithSSL();

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12;

            var getListBucketsTask = minio.ListBucketsAsync();

            try
            {
                Task.WaitAll(getListBucketsTask); // block while the task completes
            }
            catch (AggregateException aggEx)
            {
                aggEx.Handle(HandleBatchExceptions);
            }
            var list = getListBucketsTask.Result;

            foreach (Bucket bucket in list.Buckets)
            {
                Console.Out.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
            }

            var bucketExistTask = minio.BucketExistsAsync("mynewbucket");

            Task.WaitAll(bucketExistTask);
            var found = bucketExistTask.Result;

            if (!found)
            {
                // Supply a new bucket name
                Task.WaitAll(minio.MakeBucketAsync("mynewbucket"));
            }
            Console.Out.WriteLine("bucket was " + found);
            Console.ReadLine();
        }
        public async Task <string> StoreFileAsync(UploadVm model, DateTimeOffset now)
        {
            var fileName     = model.File.FileName;
            var folder       = $"attachments/{now.Year}/{now.Month}/{now.Day}/";
            var saveFileName = model.MD5 + Path.GetExtension(fileName);
            //attachments/2018/12/28/{id}.ext
            var objectName = folder + saveFileName;

            bool found = await _client.BucketExistsAsync(_minioOption.BucketName);

            if (!found)
            {
                await _client.MakeBucketAsync(_minioOption.BucketName);
            }
            using (var stream = model.File.OpenReadStream())
            {
                await _client.PutObjectAsync(_minioOption.BucketName, objectName, stream, model.File.Length);
            }
            return(objectName);
        }
Пример #3
0
        // Make a bucket
        public async static Task Run(MinioClient minio,
                                     string bucketName = "my-bucket-name", string loc = "us-east-1")
        {
            try
            {
                Console.WriteLine("Running example for API: MakeBucketAsync");
                await minio.MakeBucketAsync(
                    new MakeBucketArgs()
                    .WithBucket(bucketName)
                    .WithLocation(loc)
                    );

                Console.WriteLine($"Created bucket {bucketName}");
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine($"[Bucket]  Exception: {e}");
            }
        }
Пример #4
0
        public async Task TestInvalidObjectNameError()
        {
            var badName    = new string('A', 260);
            var bucketName = Guid.NewGuid().ToString("N");
            var minio      = new MinioClient()
                             .WithEndpoint("play.min.io")
                             .WithCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
                             .Build();

            try
            {
                const int tryCount = 5;
                var       args     = new MakeBucketArgs()
                                     .WithBucket(bucketName);
                await minio.MakeBucketAsync(args);

                var ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>(
                    () => minio.StatObjectAsync(bucketName, badName));

                for (int i = 0;
                     i < tryCount &&
                     (ex.ServerResponse != null &&
                      ex.ServerResponse.StatusCode.Equals(HttpStatusCode.ServiceUnavailable)); ++i)
                {
                    ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>(
                        () => minio.StatObjectAsync(bucketName, badName));
                }
                Assert.AreEqual(ex.Response.Code, "InvalidObjectName");

                ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>(
                    () => minio.GetObjectAsync(bucketName, badName, s => { }));

                Assert.AreEqual(ex.Response.Code, "InvalidObjectName");
            }
            finally
            {
                var args = new RemoveBucketArgs()
                           .WithBucket(bucketName);
                await minio.RemoveBucketAsync(args);
            }
        }
Пример #5
0
        public async Task <UploadResponse> FileUploadCopy(List <TempFilesToDestination> content)
        {
            var            bucketName = this.bucket;
            UploadResponse obj        = new UploadResponse();

            try
            {
                var minio = new MinioClient(this.endpoint, accessKey, accessSecret);
                // Make a bucket on the server, if not already present.
                bool found = await minio.BucketExistsAsync(bucketName);

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

                foreach (var item in content)
                {
                    try
                    {
                        await minio.StatObjectAsync(bucketName, item.distObjectName);
                    }
                    catch (MinioException e)
                    {
                        await minio.CopyObjectAsync(bucketName, item.tempObjectName, bucketName, item.distObjectName);

                        await minio.RemoveObjectAsync(bucketName, item.tempObjectName);
                    }
                }
                obj.Message    = "Uploaded Successfully.";
                obj.HasSucceed = true;
            }
            catch (MinioException e)
            {
                Console.WriteLine("File Upload Error: {0}", e.Message);
                obj.Message    = "Uploaded failed with error " + e.message;
                obj.HasSucceed = false;
                obj.FileUrl    = this.BaseUrl + bucketName + "/";
            }
            return(obj);
        }
        public void Init()
        {
            ESTraceLogger.Debug($"AWS_S3CacheProvider Init: Starting");
            if (client == null)
            {
                lock (_lockconnections)
                {
                    if (connections.ContainsKey(this.AWS_S3Servers.First()))
                    {
                        client = connections[this.AWS_S3Servers.First()];
                    }
                    else
                    {
                        client = new MinioClient(this.AWS_S3Servers.First(),
                                                 this.AccessKey,
                                                 this.SecretKey,
                                                 this.Region
                                                 );
                        connections.Add(this.AWS_S3Servers.First(), client);

                        bool found = client.BucketExistsAsync(this.Bucket).Result;
                        if (!found)
                        {
                            ESTraceLogger.Debug($"AWS_S3CacheProvider Init: Bucket {this.AWS_S3Servers.First()} / {this.Bucket} doesn't exist. Creating new one");
                            try
                            {
                                client.MakeBucketAsync(this.Bucket).Wait();
                            }
                            catch (Exception e)
                            {
                                ESTraceLogger.Fatal($"AWS_S3CacheProvider: Created db error {e.Message}", e);
                                throw new ApplicationException($"Cannot created AWS_S3  {this.AWS_S3Servers.First()} / {this.Bucket}");
                            }
                        }
                    }
                    ESTraceLogger.Debug($"AWS_S3CacheProvider Init: Bucket {this.AWS_S3Servers.First()} / {this.Bucket} is live.");
                    cleaning.Elapsed += Cleaning_Elapsed;
                    cleaning.Start();
                }
            }
        }
Пример #7
0
        static void Main(string[] args)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
                                                   | SecurityProtocolType.Tls11
                                                   | SecurityProtocolType.Tls12;


            /// Note: s3 AccessKey and SecretKey needs to be added in App.config file
            /// See instructions in README.md on running examples for more information.
            var minio = new MinioClient("play.min.io:9000",
                                        "Q3AM3UQ867SPQQA43P2F",
                                        "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG").WithSSL();

            var getListBucketsTask = minio.ListBucketsAsync();

            try
            {
                Task.WaitAll(getListBucketsTask); // block while the task completes
            }
            catch (AggregateException aggEx)
            {
                aggEx.Handle(HandleBatchExceptions);
            }
            var list = getListBucketsTask.Result;

            foreach (Bucket bucket in list.Buckets)
            {
                Console.Out.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
            }

            //Supply a new bucket name
            Task.WaitAll(minio.MakeBucketAsync("mynewbucket"));

            var bucketExistTask = minio.BucketExistsAsync("mynewbucket");

            Task.WaitAll(bucketExistTask);
            var found = bucketExistTask.Result;

            Console.Out.WriteLine("bucket was " + found);
            Console.ReadLine();
        }
Пример #8
0
        public async Task <IActionResult> UploadFile(IFormFile file)
        {
            if (file is null)
            {
                return(BadRequest("Must upload a valid file!"));
            }

            var fileId    = Guid.NewGuid().ToString();
            var extension = Path.GetExtension(file.FileName);
            var fileName  = $"{fileId}{extension}";

            var filePath = Path.GetTempFileName();

            _logger.LogInformation($"Temp file name: '{filePath}'.");

            try
            {
                await using var stream = System.IO.File.Create(filePath);
                await file.CopyToAsync(stream);

                _logger.LogInformation("File copied to stream.");

                if (!await _minioClient.BucketExistsAsync(BucketName))
                {
                    await _minioClient.MakeBucketAsync(BucketName);
                }

                _logger.LogInformation("Bucket exists/created, uploading file...");

                stream.Seek(0, SeekOrigin.Begin);

                await _minioClient.PutObjectAsync(BucketName, fileName, stream, stream.Length, file.ContentType);
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, exception.Message);
                throw;
            }

            return(Ok(new { FileName = fileName }));
        }
Пример #9
0
        public async Task CreateAndListBuckets()
        {
            var minio = new MinioClient(Endpoint, AccessKey, SecretKey) /*.WithSSL()*/;

            var listBucketsResult = await minio.ListBucketsAsync();

            for (var i = 0; i < 10; i++)
            {
                var bucketName = "bucket" + i;

                if (await minio.BucketExistsAsync(bucketName) == false)
                {
                    await minio.MakeBucketAsync(bucketName);
                }
            }

            foreach (var bucket in listBucketsResult.Buckets)
            {
                Console.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
            }
        }
Пример #10
0
        //  BUCKET OPERATIONS

        // true - bucket was created | false - bucket already exists
        public async Task <Boolean> CreateBucketIfExistAsync(String BucketName)
        {
            try
            {
                // Create bucket if it doesn't exist.
                bool found = await minio.BucketExistsAsync(BucketName);

                if (found)
                {
                    return(false);
                }
                else
                {
                    // Create bucket 'bucketname'.
                    await minio.MakeBucketAsync(BucketName);

                    return(true);
                }
            }
            catch (MinioException e) { throw; }
        }
Пример #11
0
        public async Task <UploadResponse> FileUploadv2(Microsoft.AspNetCore.Http.IFormFile file)
        {
            var            bucketName = this.bucket;
            UploadResponse obj        = new UploadResponse();

            try
            {
                var minio = new MinioClient(this.endpoint, accessKey, accessSecret);
                // Make a bucket on the server, if not already present.
                bool found = await minio.BucketExistsAsync(bucketName);

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

                var stream = file.OpenReadStream();
                await minio.PutObjectAsync(bucketName, file.Name, stream, stream.Length);

                if (file.Length > 0)
                {
                    obj.Message    = "Uploaded Successfully.";
                    obj.HasSucceed = true;
                    obj.FileName   = file.Name;
                    obj.FileUrl    = this.BaseUrl + bucketName + "/" + file.Name;
                }
                else
                {
                    obj.Message = this.BaseUrl + bucketName + "/";
                }
            }
            catch (MinioException e)
            {
                Console.WriteLine("File Upload Error: {0}", e.Message);
                obj.Message    = "Uploaded failed with error " + e.message;
                obj.HasSucceed = false;
                obj.FileUrl    = this.BaseUrl + bucketName + "/";
            }
            return(obj);
        }
Пример #12
0
        public async Task <bool> CreateBucketAsync(string bucketName)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            bool found = await BucketExistsAsync(bucketName);

            if (found)
            {
                throw new BucketExistException($"Bucket '{bucketName}' already exists.");
            }
            else
            {
                await _client.MakeBucketAsync(
                    new MakeBucketArgs()
                    .WithBucket(bucketName)
                    .WithLocation(Options.Region));

                return(true);
            }
        }
Пример #13
0
        private async static Task MakeBucket_Test4(MinioClient minio)
        {
            Console.Out.WriteLine("Test 4 : MakeBucketAsync with region");
            string bucketName = GetRandomName(length: 20) + ".withperiod";

            try
            {
                await minio.MakeBucketAsync(bucketName, location : "us-west-2");

                bool found = await minio.BucketExistsAsync(bucketName);

                Assert.IsTrue(found);
                if (found)
                {
                    await minio.RemoveBucketAsync(bucketName);
                }
            }
            catch (MinioException)
            {
                Assert.Fail();
            }
        }
Пример #14
0
        public async Task <MinioUploadDto> Upload(IFormFile file)
        {
            MinioClient minioClient = new MinioClient(AppSettings.MinIO.EndPoint, AppSettings.MinIO.AccessKey, AppSettings.MinIO.SecretKey);
            bool        isExist     = await minioClient.BucketExistsAsync(AppSettings.MinIO.BucketName);

            if (!isExist)
            {
                minioClient.MakeBucketAsync(AppSettings.MinIO.BucketName);
                //minioClient.SetPolicyAsync(AppSettings.MinIO.BucketName, "*.*", PolicyType.WRITE_ONLY);
            }
            var    fileName   = file.Name;
            string objectName = $"{Clock.Now.ToString("yyyyMMdd")}/fileName";

            minioClient.PutObjectAsync(AppSettings.MinIO.BucketName, objectName, fileName, file.ContentType);
            MinioUploadDto minioUploadDto = new MinioUploadDto
            {
                FileName = fileName,
                Url      = $"{AppSettings.MinIO.EndPoint}/{AppSettings.MinIO.BucketName}/{objectName}",
            };

            return(minioUploadDto);
        }
Пример #15
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();
            }
        }
Пример #16
0
        public async Task <bool> Handle(UploadProfilePhotoCommand request, CancellationToken cancellationToken)
        {
            string bucketName = _configuration["MinioConfiguration:UserProfilePhotosBucketName"];
            bool   found      = await _minioClient.BucketExistsAsync(bucketName);

            if (!found)
            {
                await _minioClient.MakeBucketAsync(bucketName, cancellationToken : cancellationToken);
            }

            var    fileExtension = Path.GetExtension(request.FileName);
            string realFileName  = $"{request.UserId.ToString()}{fileExtension}".ToLower();

            var fileStream = GetStreamFromBytes(request.PhotoContent);
            await _minioClient.PutObjectAsync(
                bucketName : bucketName,
                objectName : realFileName,
                data : fileStream,
                size : fileStream.Length
                );

            return(true);
        }
Пример #17
0
        /// <summary>
        /// Task that uploads a file to a bucket
        /// </summary>
        /// <param name="minio"></param>
        /// <returns></returns>
        private static async Task Run(MinioClient minio)
        {
            // Make a new bucket called mymusic.
            var bucketName = "mymusic-folder"; //<==== change this
            var location   = "us-east-1";
            // Upload the zip file
            var objectName  = "my-golden-oldies.mp3";
            var filePath    = "C:\\Users\\vagrant\\Downloads\\golden_oldies.mp3";
            var contentType = "application/zip";

            try
            {
                var bktExistArgs = new BucketExistsArgs()
                                   .WithBucket(bucketName);
                bool found = await minio.BucketExistsAsync(bktExistArgs);

                if (!found)
                {
                    var mkBktArgs = new MakeBucketArgs()
                                    .WithBucket(bucketName)
                                    .WithLocation(location);
                    await minio.MakeBucketAsync(mkBktArgs);
                }
                PutObjectArgs putObjectArgs = new PutObjectArgs()
                                              .WithBucket(bucketName)
                                              .WithObject(objectName)
                                              .WithFileName(filePath)
                                              .WithContentType(contentType);
                await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false);

                Console.WriteLine($"\nSuccessfully uploaded {objectName}\n");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Пример #18
0
        public async Task <SaveImageResult> SaveImageByteAsync(SaveImageParam param)
        {
            string defaultLocation = "us-east-1";

            try
            {
                bool isBucketExist = await _minio.BucketExistsAsync(param.BucketName);

                if (!isBucketExist)
                {
                    await _minio.MakeBucketAsync(param.BucketName, defaultLocation);
                }

                param.ImageData.Position = 0;

                await _minio.PutObjectAsync(
                    param.BucketName,
                    param.ImageName,
                    param.ImageData,
                    param.ImageSize,
                    param.ContentType,
                    param.MetaData);

                return(new SaveImageResult()
                {
                    BucketName = param.BucketName,
                    ImageName = param.ImageName,
                    Location = defaultLocation
                });
            }
            catch (MinioException e)
            {
                //SentrySdk.CaptureException(e);
                throw;
            }
        }
Пример #19
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        /// <param name="bucketName"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public async Task <bool> FileUpload(string filePath, string fileName, string bucketName)
        {
            try
            {
                var contentType = "application/zip";
                // Make a bucket on the server, if not already present.
                bool found = await minioClient.BucketExistsAsync(bucketName);

                if (!found)
                {
                    await minioClient.MakeBucketAsync(bucketName);
                }
                // Upload a file to bucket.
                await minioClient.PutObjectAsync(bucketName, fileName, filePath, contentType);

                Console.WriteLine("Successfully uploaded " + fileName);
                return(true);
            }
            catch (MinioException ex)
            {
                throw ex;
                Console.WriteLine("File Upload Error: {0}", ex.Message);
            }
        }
Пример #20
0
        /// <summary>
        /// Upload file to minio
        /// </summary>
        /// <param name="formFile"></param>
        /// <param name="bucketName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task <bool> UploadFile(IFormFile formFile, string bucketName, string fileName)
        {
            bool response = true;

            try
            {
                bool bucketFound = await _minioClient.BucketExistsAsync(bucketName);

                if (!bucketFound)
                {
                    _minioClient.MakeBucketAsync(bucketName, "Tr-tr").Wait();
                }
                using (Stream stream = formFile.OpenReadStream())
                {
                    await _minioClient.PutObjectAsync(bucketName, fileName, stream, stream.Length);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                response = false;
            }
            return(response);
        }
        private async static Task Run(MinioClient minio, IFormFile _request, string bucketName, string location, Guid guid, string fileType, string fileLocation)
        {
            if (!Directory.Exists(fileLocation))
            {
                Directory.CreateDirectory(fileLocation);
            }

            string FilePath = "";



            using (var fileStream = new FileStream(fileLocation + _request.FileName, FileMode.Create))
            {
                await _request.CopyToAsync(fileStream);

                FilePath = fileStream.Name;
            }

            var objectName = "";

            if (fileType == "audio")
            {
                //var fileExtension = _request.FileName.Split(".");
                objectName = guid.ToString();
            }
            else if (fileType == "video")
            {
                objectName = guid.ToString();
            }


            var filePath = FilePath;

            var contentType = "";

            if (fileType == "audio")
            {
                contentType = _request.ContentType;
            }
            else if (fileType == "video")
            {
                contentType = _request.ContentType;
            }

            try
            {
                // Make a bucket on the server, if not already present.
                bool found = await minio.BucketExistsAsync(bucketName);

                if (!found)
                {
                    await minio.MakeBucketAsync(bucketName, location);
                }
                // Upload a file to bucket.
                await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);

                System.IO.File.Delete(filePath);
                Console.WriteLine("Successfully uploaded " + objectName);
            }
            catch (MinioException e)
            {
                logger.Error(e);
                Console.WriteLine("File Upload Error: {0}", e.Message);
            }
        }
Пример #22
0
        private async Task <string> moveObject(string input, MinioClient minio)
        {
            var request = getTaskFromDb(input);

            if (request.ContainsKey("error"))
            {
                return(request.ToString());
            }

            var bucket    = request["params"]["bucket"].Value <string>();
            var file      = request["params"]["object"].Value <string>();
            var newBucket = request["params"]["newBucket"].Value <string>();

            bool found = await minio.BucketExistsAsync(newBucket);

            if (!found)
            {
                await minio.MakeBucketAsync(newBucket);
            }
            string result = string.Empty;

            minio.CopyObjectAsync(bucket, file, newBucket).ContinueWith((x) =>
            {
                if (x.IsFaulted)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(x.Exception.Message);
                    foreach (var e in x.Exception.InnerExceptions)
                    {
                        sb.AppendLine(e.Message);
                    }

                    result = sb.ToString();
                }
                else
                {
                    Console.WriteLine($"Object {file} copied to {newBucket} successfully");

                    var key      = Guid.NewGuid().ToString();
                    var callJson = new JObject(
                        new JProperty("_key", key),
                        new JProperty("params", new JObject(
                                          new JProperty("bucket", bucket),
                                          new JProperty("object", file),
                                          new JProperty("topic", "augury"))
                                      ));
                    writeTaskToDb(callJson);
                    string callResponse = string.Empty;
                    makeCallAsync("kafkawriter", key).ContinueWith(c =>
                    {
                        if (x.IsFaulted)
                        {
                            callResponse = $"Error calling objectmover for {file}: {c.Exception.Message}";
                        }
                        else
                        {
                            callResponse = c.Result;
                        }
                        Console.WriteLine($"{input}: {callResponse}");
                    }).Wait();
                    result = callResponse;
                }
            })
            .Wait();
            return(result);
        }
Пример #23
0
 public bool MakeBucket(string bucketName)
 {
     _minioClient.MakeBucketAsync(bucketName).Wait();
     return(true);
 }
Пример #24
0
 public Task MakeBucketAsync(string bucketName, string location = "us-east-1", CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_client.MakeBucketAsync(bucketName, location, cancellationToken));
 }
Пример #25
0
        public async Task <IActionResult> FileUpload(FileUpload formFile)
        {
            // Perform an initial check to catch FileUpload class attribute violations.
            if (!ModelState.IsValid)

            {
                return(Json(new { status = "error", message = "The model is not correct" }));
            }

            //Check MIME
            if (formFile.CardImageUpload.ContentType.ToLower() != "image/png")
            {
                return(Json(new
                {
                    status = "error",
                    message = "The file mime must be image/png"
                }));
            }

            //Check if size is between 0 and 10MB
            if (formFile.CardImageUpload.Length == 0)
            {
                return(Json(new
                {
                    status = "error",
                    message = "Upload Failed. The selected file is empty."
                }));
            }
            else if (formFile.CardImageUpload.Length > 10485760)
            {
                return(Json(new
                {
                    status = "error",
                    message = "The selected file exceeds 10 MB."
                }));
            }

            //Generate Random Name
            //string ext = System.IO.Path.GetExtension(formFile.CardImageUpload.FileName); //Get the file extension

            string objectName = Guid.NewGuid() + ".png";

            var filePath = System.IO.Path.GetTempFileName() + objectName; //Create Temp File with Random GUID

            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CardImageUpload.CopyToAsync(fileStream);
            }

            //Prepare S3 Upload
            var bucketName  = "tcg-upload";
            var location    = "us-east-1";
            var contentType = formFile.CardImageUpload.ContentType.ToLower();

            try
            {
                // Make a bucket on the server, if not already present.
                bool found = await minio.BucketExistsAsync(bucketName);

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

                // Upload a file to bucket.
                await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);

                Console.Out.WriteLine("Successfully uploaded " + objectName);

                return(Json(new
                {
                    status = "Ok",
                    message = "Successfully uploaded " + objectName,
                    image = objectName
                }));
            }
            catch (MinioException e)
            {
                return(Json(new
                {
                    status = "Error",
                    message = "File Upload Error:" + e.Message
                }));
            }
        }
Пример #26
0
        public async Task <IActionResult> UploadFiles(List <IFormFile> files)
        {
            var log = new List <ScanResult>();

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    var extension = formFile.FileName.Contains('.')
                        ? formFile.FileName.Substring(formFile.FileName.LastIndexOf('.'), formFile.FileName.Length - formFile.FileName.LastIndexOf('.'))
                        : string.Empty;
                    var file = new File
                    {
                        Name        = $"{Guid.NewGuid()}{extension}",
                        Alias       = formFile.FileName,
                        Region      = "us-east-1",
                        Bucket      = BUCKET_NAME,
                        ContentType = formFile.ContentType,
                        Size        = formFile.Length,
                        Uploaded    = DateTime.UtcNow,
                    };
                    var ping = await _clam.PingAsync();

                    if (ping)
                    {
                        _logger.LogInformation("Successfully pinged the ClamAV server.");
                        var result = await _clam.SendAndScanFileAsync(formFile.OpenReadStream());

                        file.ScanResult = result.Result.ToString();
                        file.Infected   = result.Result == ClamScanResults.VirusDetected;
                        file.Scanned    = DateTime.UtcNow;
                        if (result.InfectedFiles != null)
                        {
                            foreach (var infectedFile in result.InfectedFiles)
                            {
                                file.Viruses.Add(new Virus
                                {
                                    Name = infectedFile.VirusName
                                });
                            }
                        }
                        var metaData = new Dictionary <string, string>
                        {
                            { "av-status", result.Result.ToString() },
                            { "av-timestamp", DateTime.UtcNow.ToString() },
                            { "alias", file.Alias }
                        };

                        try
                        {
                            var found = await _minio.BucketExistsAsync(BUCKET_NAME);

                            if (!found)
                            {
                                await _minio.MakeBucketAsync(BUCKET_NAME);
                            }
                            await _minio.PutObjectAsync(BUCKET_NAME,
                                                        file.Name,
                                                        formFile.OpenReadStream(),
                                                        formFile.Length,
                                                        formFile.ContentType,
                                                        metaData);

                            await _context.Files.AddAsync(file);

                            await _context.SaveChangesAsync();
                        }
                        catch (MinioException e)
                        {
                            _logger.LogError($"File Upload Error: {e.Message}");
                        }


                        var scanResult = new ScanResult()
                        {
                            FileName  = formFile.FileName,
                            Result    = result.Result.ToString(),
                            Message   = result.InfectedFiles?.FirstOrDefault()?.VirusName,
                            RawResult = result.RawResult
                        };
                        log.Add(scanResult);
                    }
                    else
                    {
                        _logger.LogWarning("Wasn't able to connect to the ClamAV server.");
                    }
                }
            }

            var model = new UploadFilesViewModel
            {
                Results = log
            };

            return(View("UploadResults", model));
        }