Exemplo n.º 1
0
        /// <summary>
        ///  Presigned post policy
        /// </summary>
        public Dictionary <string, string> PresignedPostPolicy(PostPolicy policy)
        {
            if (!policy.IsBucketSet())
            {
                throw new ArgumentException("bucket should be set");
            }

            if (!policy.IsKeySet())
            {
                throw new ArgumentException("key should be set");
            }

            if (!policy.IsExpirationSet())
            {
                throw new ArgumentException("expiration should be set");
            }

            string   region      = Regions.GetRegion(this.client.BaseUrl.Host);
            DateTime signingDate = DateTime.UtcNow;

            policy.SetAlgorithm("AWS4-HMAC-SHA256");
            policy.SetCredential(this.authenticator.GetCredentialString(signingDate, region));
            policy.SetDate(signingDate);

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

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

            return(policy.GetFormData());
        }
        static int Main(string[] args)
        {
            var client = new MinioClient("https://s3.amazonaws.com", "ACCESSKEY", "SECRETKEY");
            PostPolicy form = new PostPolicy();
            DateTime expiration = DateTime.UtcNow;
            form.SetExpires(expiration.AddDays(10));
            form.SetKey("objectName");
            form.SetBucket("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/bucketName";
            Console.Out.WriteLine(curlCommand);
            return 0;
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        /// <summary>
        ///  Presigned post policy
        /// </summary>
        public Dictionary<string, string> PresignedPostPolicy(PostPolicy policy)
        {
            if (!policy.IsBucketSet())
                {
                        throw new ArgumentException("bucket should be set");
                }

                if (!policy.IsKeySet())
                {
                        throw new ArgumentException("key should be set");
                }

                if (!policy.IsExpirationSet())
                {
                        throw new ArgumentException("expiration should be set");
                }

                string region = Regions.GetRegion(this.client.BaseUrl.Host);
                DateTime signingDate = DateTime.UtcNow;

                policy.SetAlgorithm("AWS4-HMAC-SHA256");
                policy.SetCredential(this.authenticator.GetCredentialString(signingDate, region));
                policy.SetDate(signingDate);

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

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

                return policy.getFormData();
        }
Exemplo n.º 5
0
        /// <summary>
        ///  Presigned post policy
        /// </summary>
        public async Task <Tuple <string, Dictionary <string, string> > > PresignedPostPolicyAsync(PostPolicy policy)
        {
            string region = null;

            if (!policy.IsBucketSet())
            {
                throw new ArgumentException("bucket should be set");
            }

            if (!policy.IsKeySet())
            {
                throw new ArgumentException("key should be set");
            }

            if (!policy.IsExpirationSet())
            {
                throw new ArgumentException("expiration should be set");
            }

            // Initialize a new client.
            if (!BucketRegionCache.Instance.Exists(policy.Bucket))
            {
                region = await BucketRegionCache.Instance.Update(this, policy.Bucket);
            }
            else
            {
                region = BucketRegionCache.Instance.Region(policy.Bucket);
            }
            // Set Target URL
            Uri requestUrl = RequestUtil.MakeTargetURL(this.BaseUrl, this.Secure, bucketName: policy.Bucket, region: region, usePathStyle: false);

            SetTargetURL(requestUrl);
            DateTime signingDate = DateTime.UtcNow;

            policy.SetAlgorithm("AWS4-HMAC-SHA256");
            policy.SetCredential(this.authenticator.GetCredentialString(signingDate, region));
            policy.SetDate(signingDate);

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

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

            return(new Tuple <string, Dictionary <string, string> >(this.restClient.BaseUrl.AbsoluteUri, policy.GetFormData()));
        }
Exemplo n.º 6
0
 public PresignedPostPolicyArgs WithPolicy(PostPolicy policy)
 {
     this.Policy = policy;
     return(this);
 }