Example #1
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);
                }
                await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);

                Console.WriteLine("Successfully uploaded " + objectName);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
Example #2
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()
                        .WithEndpoint("play.min.io")
                        .WithCredentials("Q3AM3UQ867SPQQA43P2F",
                                         "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG")
                        .WithSSL()
                        .Build();
            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.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime);
            }

            //Supply a new bucket name
            string bucketName = "mynewbucket";

            if (isBucketExists(minio, bucketName))
            {
                RemoveBucketArgs remBuckArgs = new Minio.RemoveBucketArgs()
                                               .WithBucket(bucketName);
                var removeBucketTask = minio.RemoveBucketAsync(remBuckArgs);
                Task.WaitAll(removeBucketTask);
            }

            MakeBucketArgs mkBktArgs = new MakeBucketArgs()
                                       .WithBucket(bucketName);

            Task.WaitAll(minio.MakeBucketAsync(mkBktArgs));

            bool found = isBucketExists(minio, bucketName);

            Console.WriteLine("Bucket exists? = " + found);
            Console.ReadLine();
        }
Example #3
0
        public async Task PresignedGetObjectWithHeaders()
        {
            // todo how to test this with mock client.
            var client = new MinioClient()
                         .WithEndpoint("play.min.io")
                         .WithCredentials("Q3AM3UQ867SPQQA43P2F",
                                          "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");

            var bucket     = "bucket";
            var objectName = "object-name";

            Dictionary <string, string> reqParams = new Dictionary <string, string>
            {
                { "Response-Content-Disposition", "attachment; filename=\"filename.jpg\"" },
            };

            var bktExistArgs = new BucketExistsArgs()
                               .WithBucket(bucket);
            bool found = await client.BucketExistsAsync(bktExistArgs);

            if (!found)
            {
                var mkBktArgs = new MakeBucketArgs()
                                .WithBucket(bucket);
                await client.MakeBucketAsync(mkBktArgs);
            }

            if (!await this.ObjectExistsAsync(client, bucket, objectName))
            {
                var helloData   = Encoding.UTF8.GetBytes("hello world");
                var helloStream = new MemoryStream();
                helloStream.Write(helloData);
                var PutObjectArgs = new PutObjectArgs()
                                    .WithBucket(bucket)
                                    .WithObject(objectName)
                                    .WithStreamData(helloStream)
                                    .WithObjectSize(helloData.Length);
                await client.PutObjectAsync(PutObjectArgs);
            }

            PresignedGetObjectArgs presignedGetObjectArgs = new PresignedGetObjectArgs()
                                                            .WithBucket("bucket")
                                                            .WithObject("object-name")
                                                            .WithExpiry(3600)
                                                            .WithHeaders(reqParams)
                                                            .WithRequestDate(_requestDate);

            var signedUrl = client.PresignedGetObjectAsync(presignedGetObjectArgs);

            Assert.AreEqual(
                "http://play.min.io/bucket/object-name?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=Q3AM3UQ867SPQQA43P2F%2F20200501%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200501T154533Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3D%22filename.jpg%22&X-Amz-Signature=de66f04dd4ac35838b9e83d669f7b5a70b452c6468e2b4a9e9c29f42e7fa102d",
                signedUrl);
        }
        public async Task ReuseTcpTest()
        {
            var bucket     = "bucket";
            var objectName = "object-name";

            var bktExistArgs = new BucketExistsArgs()
                               .WithBucket(bucket);
            bool found = await this.MinioClient.BucketExistsAsync(bktExistArgs);

            if (!found)
            {
                var mkBktArgs = new MakeBucketArgs()
                                .WithBucket(bucket);
                await this.MinioClient.MakeBucketAsync(mkBktArgs);
            }

            if (!await this.ObjectExistsAsync(this.MinioClient, bucket, objectName))
            {
                var helloData   = Encoding.UTF8.GetBytes("hello world");
                var helloStream = new MemoryStream();
                helloStream.Write(helloData);
                helloStream.Seek(0, SeekOrigin.Begin);
                var putObjectArgs = new PutObjectArgs()
                                    .WithBucket(bucket)
                                    .WithObject(objectName)
                                    .WithStreamData(helloStream)
                                    .WithObjectSize(helloData.Length);
                await this.MinioClient.PutObjectAsync(putObjectArgs);
            }

            var length = await this.GetObjectLength(bucket, objectName);

            Assert.IsTrue(length > 0);

            for (int i = 0; i < 100; i++)
            {
                // sequential execution, produce one tcp connection, check by netstat -an | grep 9000
                length = this.GetObjectLength(bucket, objectName).Result;
                Assert.IsTrue(length > 0);
            }

            Parallel.ForEach(Enumerable.Range(0, 500),
                             new ParallelOptions
            {
                MaxDegreeOfParallelism = 8
            },
                             i =>
            {
                // concurrent execution, produce eight tcp connections.
                length = this.GetObjectLength(bucket, objectName).Result;
                Assert.IsTrue(length > 0);
            });
        }
Example #5
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       mkBktArgs = new MakeBucketArgs()
                                      .WithBucket(bucketName);
                await minio.MakeBucketAsync(mkBktArgs);

                var statObjArgs = new StatObjectArgs()
                                  .WithBucket(bucketName)
                                  .WithObject(badName);
                var ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>(
                    () => minio.StatObjectAsync(statObjArgs));

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

                GetObjectArgs getObjectArgs = new GetObjectArgs()
                                              .WithBucket(bucketName)
                                              .WithObject(badName)
                                              .WithCallbackStream(s => {});
                ex = await Assert.ThrowsExceptionAsync <InvalidObjectNameException>(
                    () => minio.GetObjectAsync(getObjectArgs));

                Assert.AreEqual(ex.Response.Code, "InvalidObjectName");
            }
            finally
            {
                var args = new RemoveBucketArgs()
                           .WithBucket(bucketName);
                await minio.RemoveBucketAsync(args);
            }
        }
Example #6
0
        public async Task PresignedGetObject()
        {
            // todo how to test this with mock client.
            var client = new MinioClient()
                         .WithEndpoint("play.min.io")
                         .WithCredentials("Q3AM3UQ867SPQQA43P2F",
                                          "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");

            var bucket     = "bucket";
            var objectName = "object-name";

            var bktExistArgs = new BucketExistsArgs()
                               .WithBucket(bucket);
            bool found = await client.BucketExistsAsync(bktExistArgs);

            if (!found)
            {
                var mkBktArgs = new MakeBucketArgs()
                                .WithBucket(bucket);
                await client.MakeBucketAsync(mkBktArgs);
            }

            if (!await this.ObjectExistsAsync(client, bucket, objectName))
            {
                var helloData   = Encoding.UTF8.GetBytes("hello world");
                var helloStream = new MemoryStream();
                helloStream.Write(helloData);
                helloStream.Seek(0, SeekOrigin.Begin);
                var PutObjectArgs = new PutObjectArgs()
                                    .WithBucket(bucket)
                                    .WithObject(objectName)
                                    .WithStreamData(helloStream)
                                    .WithObjectSize(helloData.Length);
                await client.PutObjectAsync(PutObjectArgs);
            }

            PresignedGetObjectArgs presignedGetObjectArgs = new PresignedGetObjectArgs()
                                                            .WithBucket("bucket")
                                                            .WithObject("object-name")
                                                            .WithExpiry(3600)
                                                            .WithRequestDate(_requestDate);

            var signedUrl = client.PresignedGetObjectAsync(presignedGetObjectArgs);

            Assert.AreEqual(
                "http://play.min.io/bucket/object-name?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=Q3AM3UQ867SPQQA43P2F%2F20200501%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200501T154533Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=d4202da690618f77142d6f0557c97839f0773b2c718082e745cd9b199aa6b28f",
                signedUrl);
        }
Example #7
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";
        // The following is a source file that needs to be created in
        // your local filesystem.
        var filePath    = "C:\\Users\\vagrant\\Downloads\\golden_oldies.mp3";
        var contentType = "application/zip";

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

            if (!found)
            {
                var mkBktArgs = new MakeBucketArgs()
                                .WithBucket(bucketName)
                                .WithLocation(location);
                await minio.MakeBucketAsync(mkBktArgs);
            }

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

        // Added for Windows folks. Without it, the window, tests
        // run in, dissappears as soon as the test code completes.
        if (IsWindows())
        {
            Console.ReadLine();
        }
    }