public async Task EnsureBucketExists(string bucketName, CancellationToken token = default)
 {
     if (!await _client.BucketExistsAsync(bucketName, token))
     {
         await _client.MakeBucketAsync(bucketName, cancellationToken : token);
     }
 }
Beispiel #2
0
        public async Task Check()
        {
            if (!await client.BucketExistsAsync(bucket))
            {
                await client.MakeBucketAsync(bucket);

                await client.SetPolicyAsync(bucket, $@"
{{
  ""Id"": ""ReadOnlyForEveryone"",
  ""Version"": ""2012-10-17"",
  ""Statement"": [
    {{
      ""Sid"": ""Stmt1601372292618"",
      ""Action"": [
        ""s3:GetObject"",
        ""s3:GetObjectVersion""
      ],
      ""Effect"": ""Allow"",
      ""Resource"": ""arn:aws:s3:::{bucket}/*"",
      ""Principal"": ""*""
    }}
  ]
}}
            ");
            }
        }
Beispiel #3
0
        private async static Task Run(Minio.MinioClient minio, IFormFile _request, string bucketName, string location, Guid id, 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 = _request.FileName;
            var objectName = id.ToString();
            var filePath   = FilePath;

            var contentType = _request.ContentType;

            //var contentType = "";
            //if (fileType == "audio")
            //{
            //    contentType = "audio/mp3";
            //}
            //else if (fileType == "video")
            //{
            //    contentType = "video/mp4";
            //}

            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);
            }
        }