public static async Task Run(MinioClient client,
                                 string bucketName = "my-bucketname",
                                 string objectName = "my-objectname")
    {
        // default value for expiration is 2 minutes
        var expiration = DateTime.UtcNow.AddMinutes(2);

        var form = new PostPolicy();

        form.SetKey(objectName);
        form.SetBucket(bucketName);
        form.SetExpires(expiration);

        var args = new PresignedPostPolicyArgs()
                   .WithBucket(bucketName)
                   .WithObject(objectName)
                   .WithPolicy(form);

        var tuple = await client.PresignedPostPolicyAsync(form);

        var curlCommand = "curl -k --insecure -X POST";

        foreach (var pair in tuple.Item2)
        {
            curlCommand = curlCommand + $" -F {pair.Key}={pair.Value}";
        }
        curlCommand = curlCommand + " -F file=@/etc/issue " + tuple.Item1 + bucketName + "/";
    }
Ejemplo n.º 2
0
        public async static Task Run(MinioClient client)
        {
            try
            {
                PostPolicy form       = new PostPolicy();
                DateTime   expiration = DateTime.UtcNow;
                form.SetExpires(expiration.AddDays(10));
                form.SetKey("my-objectname");
                form.SetBucket("my-bucketname");

                Tuple <string, Dictionary <string, string> > tuple = await client.PresignedPostPolicyAsync(form);

                string curlCommand = "curl -X POST ";
                foreach (KeyValuePair <string, string> pair in tuple.Item2)
                {
                    curlCommand = curlCommand + String.Format(" -F {0}={1}", pair.Key, pair.Value);
                }
                curlCommand = curlCommand + " -F file=@/etc/bashrc " + tuple.Item1; // https://s3.amazonaws.com/my-bucketname";
                Console.Out.WriteLine(curlCommand);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception ", e.Message);
            }
        }
Ejemplo n.º 3
0
        public async Task <string> GetPresignedUploadUrlAsync(DTOs.Uploads.FileInfo fileInfo)
        {
            string endpoint   = _config["AppSettings:S3Endpoint"];
            string apiKey     = _config["AppSettings:S3ApiKey"];
            string apiSecret  = _config["AppSettings:S3ApiSecret"];
            string bucketName = _config["AppSettings:S3BucketName"];

            PostPolicy policy    = new PostPolicy();
            DateTime   expiredIn = DateTime.UtcNow.AddMinutes(15);

            policy.SetExpires(expiredIn);
            policy.SetContentLength(fileInfo.Size);
            policy.SetcontentType(fileInfo.ContentType);
            policy.SetKey(fileInfo.Name);
            policy.SetBucket(bucketName);

            var minio = new MinioClient(endpoint, apiKey, apiSecret);
            await minio.PresignedPostPolicyAsync(policy);

            string presignedUrl = await minio.PresignedPutObjectAsync(bucketName, fileInfo.Name, 15 * 60);

            return(presignedUrl);
        }
Ejemplo n.º 4
0
        static int Main()
        {
            /// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-bucketname and
            /// my-objectname are dummy values, please replace them with original values.
            var        client     = new MinioClient("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY");
            PostPolicy form       = new PostPolicy();
            DateTime   expiration = DateTime.UtcNow;

            form.SetExpires(expiration.AddDays(10));
            form.SetKey("my-objectname");
            form.SetBucket("my-bucketname");

            Dictionary <string, string> formData = client.PresignedPostPolicy(form);
            string curlCommand = "curl ";

            foreach (KeyValuePair <string, string> pair in formData)
            {
                curlCommand = curlCommand + " -F " + pair.Key + "=" + pair.Value;
            }
            curlCommand = curlCommand + " -F file=@/etc/bashrc https://s3.amazonaws.com/my-bucketname";
            Console.Out.WriteLine(curlCommand);
            return(0);
        }
Ejemplo n.º 5
0
        public async static Task Run(MinioClient client,
                                     string bucketName = "my-bucketname",
                                     string objectName = "my-objectname")
        {
            // default value for expiration is 2 minutes
            DateTime expiration = DateTime.UtcNow.AddMinutes(2);

            // DefaultPolicy defaultPolicy = delegate (string bucketName,
            //                                         string objectName,
            //                                         DateTime expiration)
            // {
            PostPolicy form = new PostPolicy();

            form.SetKey(objectName);
            form.SetBucket(bucketName);
            form.SetExpires(expiration);
            // return policy;
            // };
            // PostPolicy postPolicy = defaultPolicy(bucketName,
            //                                       objectName,
            //                                       expiration);

            PresignedPostPolicyArgs args = new PresignedPostPolicyArgs()
                                           .WithBucket(bucketName)
                                           .WithObject(objectName)
                                           .WithPolicy(form);

            var tuple = await client.PresignedPostPolicyAsync(form);

            string curlCommand = "curl -k --insecure -X POST";

            foreach (KeyValuePair <string, string> pair in tuple.Item2)
            {
                curlCommand = curlCommand + $" -F {pair.Key}={pair.Value}";
            }
            curlCommand = curlCommand + " -F file=@/etc/issue " + tuple.Item1 + bucketName + "/";
        }
Ejemplo n.º 6
0
        public void TestPresignedPostPolicy()
        {
            DateTime requestDate   = new DateTime(2020, 05, 01, 15, 45, 33, DateTimeKind.Utc);
            var      authenticator = new V4Authenticator(false, "my-access-key", "secretkey");

            var policy = new PostPolicy();

            policy.SetBucket("bucket-name");
            policy.SetKey("object-name");

            policy.SetAlgorithm("AWS4-HMAC-SHA256");
            var region = "mock-location";

            policy.SetCredential(authenticator.GetCredentialString(requestDate, region));
            policy.SetDate(requestDate);
            policy.SetSessionToken(null);

            string policyBase64 = policy.Base64();
            string signature    = authenticator.PresignPostSignature(region, requestDate, policyBase64);

            policy.SetPolicy(policyBase64);
            policy.SetSignature(signature);

            var headers = new Dictionary <string, string>
            {
                { "bucket", "bucket-name" },
                { "key", "object-name" },
                { "x-amz-algorithm", "AWS4-HMAC-SHA256" },
                { "x-amz-credential", "my-access-key/20200501/mock-location/s3/aws4_request" },
                { "x-amz-date", "20200501T154533Z" },
                { "policy", "eyJleHBpcmF0aW9uIjoiMDAwMS0wMS0wMVQwMDowMDowMC4wMDBaIiwiY29uZGl0aW9ucyI6W1siZXEiLCIkYnVja2V0IiwiYnVja2V0LW5hbWUiXSxbImVxIiwiJGtleSIsIm9iamVjdC1uYW1lIl0sWyJlcSIsIiR4LWFtei1hbGdvcml0aG0iLCJBV1M0LUhNQUMtU0hBMjU2Il0sWyJlcSIsIiR4LWFtei1jcmVkZW50aWFsIiwibXktYWNjZXNzLWtleS8yMDIwMDUwMS9tb2NrLWxvY2F0aW9uL3MzL2F3czRfcmVxdWVzdCJdLFsiZXEiLCIkeC1hbXotZGF0ZSIsIjIwMjAwNTAxVDE1NDUzM1oiXV19" },
                { "x-amz-signature", "ec6dad862909ee905cfab3ef87ede0e666eebd6b8f00d28e5df104a8fcbd4027" },
            };

            CollectionAssert.AreEquivalent(headers, policy.GetFormData());
        }
        static int Main()
        {
            /// 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 client = new MinioClient("s3.amazonaws.com", ConfigurationManager.AppSettings["s3AccessKey"],
                                         ConfigurationManager.AppSettings["s3SecretKey"]);
            PostPolicy form       = new PostPolicy();
            DateTime   expiration = DateTime.UtcNow;

            form.SetExpires(expiration.AddDays(10));
            form.SetKey("my-objectname");
            form.SetBucket("my-bucketname");

            Dictionary <string, string> formData = client.PresignedPostPolicy(form);
            string curlCommand = "curl ";

            foreach (KeyValuePair <string, string> pair in formData)
            {
                curlCommand = curlCommand + " -F " + pair.Key + "=" + pair.Value;
            }
            curlCommand = curlCommand + " -F file=@/etc/bashrc https://s3.amazonaws.com/my-bucketname";
            Console.Out.WriteLine(curlCommand);
            return(0);
        }
Ejemplo n.º 8
0
        private async static Task PresignedPostPolicy_Test1(MinioClient minio)
        {
            Console.Out.WriteLine("Test1: PresignedPostPolicyAsync");
            string bucketName = GetRandomName(15);
            string objectName = GetRandomName(10);
            string fileName   = CreateFile(1 * MB);


            try
            {
                await Setup_Test(minio, bucketName);

                await minio.PutObjectAsync(bucketName,
                                           objectName,
                                           fileName);

                // Generate presigned post policy url
                PostPolicy form       = new PostPolicy();
                DateTime   expiration = DateTime.UtcNow;
                form.SetExpires(expiration.AddDays(10));
                form.SetKey(objectName);
                form.SetBucket(bucketName);
                var    pairs = new List <KeyValuePair <string, string> >();
                string url   = "https://s3.amazonaws.com/" + bucketName;
                Tuple <string, System.Collections.Generic.Dictionary <string, string> > policyTuple = await minio.PresignedPostPolicyAsync(form);

                var httpClient = new HttpClient();

                using (var stream = File.OpenRead(fileName))
                {
                    MultipartFormDataContent multipartContent = new MultipartFormDataContent();
                    multipartContent.Add(new StreamContent(stream), fileName, objectName);
                    multipartContent.Add(new FormUrlEncodedContent(pairs));
                    var response = await httpClient.PostAsync(url, multipartContent);

                    response.EnsureSuccessStatusCode();
                }

                // Validate
                PolicyType policy = await minio.GetPolicyAsync(bucketName, objectName.Substring(5));

                Assert.AreEqual(policy.GetType(), PolicyType.READ_ONLY);
                await minio.RemoveObjectAsync(bucketName, objectName);

                await TearDown(minio, bucketName);

                File.Delete(fileName);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("Exception ", e.Message);
            }

            Console.Out.WriteLine("Test1: PresignedPostPolicyAsync Complete");


            await minio.RemoveObjectAsync(bucketName, objectName);

            await TearDown(minio, bucketName);

            File.Delete(fileName);
            Console.Out.WriteLine("Test1: PresignedPostPolicyAsync Complete");
        }