public async Task PostPolicyAcl() { var bucketName = _fixture.BucketName; var objectName = "places/world.txt"; var credential = (await GoogleCredential.GetApplicationDefaultAsync()).UnderlyingCredential as ServiceAccountCredential; // Sample: PostPolicyAcl // Create a signed post policy which can be used to upload a specific object and // expires in 10 seconds after creation. // It also sets a starts-with condition on the acl form element, that should be met // by the actual form used for posting. UrlSigner urlSigner = UrlSigner .FromServiceAccountCredential(credential); UrlSigner.Options options = UrlSigner.Options .FromDuration(TimeSpan.FromHours(1)) .WithSigningVersion(SigningVersion.V4) .WithScheme("https"); UrlSigner.PostPolicy postPolicy = UrlSigner.PostPolicy.ForBucketAndKey(bucketName, objectName); postPolicy.SetStartsWith(UrlSigner.PostPolicyStandardElement.Acl, "public"); UrlSigner.SignedPostPolicy signedPostPolicy = await urlSigner.SignAsync(postPolicy, options); // Create an HTML form including all the fields in the signed post policy. StringBuilder form = new StringBuilder(); form.AppendLine($"<form action=\"{signedPostPolicy.PostUrl}\" method=\"post\" enctype=\"multipart/form-data\">"); foreach (var field in signedPostPolicy.Fields) { form.AppendLine($"<input type=\"hidden\" name=\"{field.Key}\" value=\"{field.Value}\">"); } // Include also an acl element with a value that meets the condition set in the policy. form.AppendLine("<input type=\"hidden\" name=\"acl\" value=\"public-read\">"); // Include the file element. It should always be the last element in the form. form.AppendLine("<input name=\"file\" type=\"file\">"); form.AppendLine("<input type=\"submit\" value=\"Upload\">"); form.AppendLine("</form>"); // You can now save the form to file and serve it as static content // or send it as the response to a request made to your application. File.WriteAllText("PostPolicyAcl.html", form.ToString()); //// End sample Assert.Contains(signedPostPolicy.PostUrl.ToString(), form.ToString()); File.Delete("PostPolicyAcl.html"); }
public async Task PostPolicySimple() { var bucketName = _fixture.BucketName; var objectName = "places/world.txt"; var credential = (await GoogleCredential.GetApplicationDefaultAsync()).UnderlyingCredential as ServiceAccountCredential; // Sample: PostPolicySimple // [START storage_generate_signed_post_policy_v4] // Create a signed post policy which can be used to upload a specific object and // expires in 1 hour after creation. UrlSigner urlSigner = UrlSigner .FromServiceAccountCredential(credential); UrlSigner.Options options = UrlSigner.Options .FromDuration(TimeSpan.FromHours(1)) .WithSigningVersion(SigningVersion.V4) .WithScheme("https"); UrlSigner.PostPolicy postPolicy = UrlSigner.PostPolicy.ForBucketAndKey(bucketName, objectName); postPolicy.SetCustomField(UrlSigner.PostPolicyCustomElement.GoogleMetadata, "x-goog-meta-test", "data"); UrlSigner.SignedPostPolicy signedPostPolicy = await urlSigner.SignAsync(postPolicy, options); // Create an HTML form including all the fields in the signed post policy. StringBuilder form = new StringBuilder(); form.AppendLine($"<form action=\"{signedPostPolicy.PostUrl}\" method=\"post\" enctype=\"multipart/form-data\">"); foreach (var field in signedPostPolicy.Fields) { form.AppendLine($"<input type=\"hidden\" name=\"{field.Key}\" value=\"{field.Value}\">"); } // Include the file element. It should always be the last element in the form. form.AppendLine("<input name=\"file\" type=\"file\">"); form.AppendLine("<input type=\"submit\" value=\"Upload\">"); form.AppendLine("</form>"); // You can now save the form to file and serve it as static content // or send it as the response to a request made to your application. File.WriteAllText("PostPolicySimple.html", form.ToString()); // [END storage_generate_signed_post_policy_v4] //// End sample Assert.Contains(signedPostPolicy.PostUrl.ToString(), form.ToString()); File.Delete("PostPolicySimple.html"); }