private static Bucket CreateS3BucketResources() { // Create an AWS resource (S3 Bucket) var bucket = new Bucket("s3-static-web-html-bucket", new BucketArgs { Website = new BucketWebsiteArgs { IndexDocument = "index.html" } }); Func <string, string> publicS3ReadPolicyFunc = bucketId => $@"{{ ""Version"": ""2012-10-17"", ""Statement"": [{{ ""Effect"": ""Allow"", ""Principal"": ""*"", ""Action"": [ ""s3:GetObject"" ], ""Resource"": ""arn:aws:s3:::{bucketId}/*"" }}] }}"; var bucketPolicy = new BucketPolicy("bucketPolicy", new BucketPolicyArgs { Bucket = bucket.Id, Policy = bucket.Id.Apply(publicS3ReadPolicyFunc), }); return(bucket); }
// Hydrate a bucket policy from JSON string internal static BucketPolicy GenerateBucketPolicy(string policyString, string bucketName) { var contentBytes = System.Text.Encoding.UTF8.GetBytes(policyString); var stream = new MemoryStream(contentBytes); return(BucketPolicy.ParseJson(stream, bucketName)); }
/// <summary> /// Internal method that sets the bucket access policy /// </summary> /// <param name="bucketName">Bucket Name.</param> /// <param name="policy">Valid Json policy object</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns>Task that sets policy</returns> private async Task setPolicyAsync(string bucketName, BucketPolicy policy, CancellationToken cancellationToken = default(CancellationToken)) { string policyJson = policy.GetJson(); var request = await this.CreateRequest(Method.PUT, bucketName, resourcePath : "?policy", contentType : "application/json", body : policyJson); IRestResponse response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken); }
public void TestIfStringIsetGetsDeSerialized_Test1() { string policyString = @"{""Version"":""2012 - 10 - 17"",""Statement"":[{""Sid"":"""",""Effect"":""Allow"",""Principal"":{""AWS"":"" * ""},""Action"":""s3: GetBucketLocation"",""Resource"":""arn: aws: s3:::miniodotnetvpn5pic718xfutt""},{""Sid"":"""",""Effect"":""Allow"",""Principal"":{""AWS"":"" * ""},""Action"":""s3: ListBucket"",""Resource"":""arn: aws: s3:::miniodotnetvpn5pic718xfutt"",""Condition"":{""StringEquals"":{""s3: prefix"":""dotnetcms1ssazhd""}}},{""Sid"":"""",""Effect"":""Allow"",""Principal"":{""AWS"":"" * ""},""Action"":""s3: GetObject"",""Resource"":""arn: aws: s3:::miniodotnetvpn5pic718xfutt / dotnetcms1ssazhd * ""}]}"; // ConditionKeyMap ckmap = JsonConvert.DeserializeObject<ConditionKeyMap>(ckmapString); var contentBytes = System.Text.Encoding.UTF8.GetBytes(policyString); string bucketName = "miniodotnetvpn5pic718xfutt"; var stream = new MemoryStream(contentBytes); BucketPolicy policy = BucketPolicy.ParseJson(stream, bucketName); }
/// <summary> /// Sets the current bucket policy /// </summary> /// <param name="bucketName">Bucket Name</param> /// <param name="objectPrefix">Name of the object prefix.</param> /// <param name="policyType">Desired Policy type change </param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns>Task to set a policy</returns> public async Task SetPolicyAsync(String bucketName, String objectPrefix, PolicyType policyType, CancellationToken cancellationToken = default(CancellationToken)) { utils.validateObjectPrefix(objectPrefix); BucketPolicy policy = await GetPolicyAsync(bucketName, cancellationToken); if (policyType == PolicyType.NONE && policy.Statements() == null) { // As the request is for removing policy and the bucket // has empty policy statements, just return success. return; } policy.SetPolicy(policyType, objectPrefix); await setPolicyAsync(bucketName, policy, cancellationToken); }
/// <summary> /// Returns current policy stored on the server for this bucket /// </summary> /// <param name="bucketName">Bucket name.</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns>Task that returns the Bucket policy</returns> private async Task <BucketPolicy> GetPolicyAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { BucketPolicy policy = null; IRestResponse response = null; var path = bucketName + "?policy"; var request = await this.CreateRequest(Method.GET, bucketName, contentType : "application/json", resourcePath : "?policy"); try { response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken); var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content); using (var stream = new MemoryStream(contentBytes)) { policy = BucketPolicy.ParseJson(stream, bucketName); } } catch (ErrorResponseException e) { // Ignore if there is if (!e.Response.Code.Equals("NoSuchBucketPolicy")) { throw e; } } finally { if (policy == null) { policy = new BucketPolicy(bucketName); } } return(policy); }
/// <summary> /// Get bucket policy at given objectPrefix /// </summary> /// <param name="bucketName">Bucket name.</param> /// <param name="objectPrefix">Name of the object prefix</param> /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param> /// <returns>Task that returns the PolicyType </returns> public async Task <PolicyType> GetPolicyAsync(string bucketName, string objectPrefix = "", CancellationToken cancellationToken = default(CancellationToken)) { BucketPolicy policy = await GetPolicyAsync(bucketName, cancellationToken); return(policy.GetPolicy(objectPrefix)); }