pub struct PostPolicyV4Builder { /* private fields */ }Expand description
Creates V4 Signed Policy Document (POST Object Forms).
This builder allows you to generate signed V4 POST policy documents for Google Cloud Storage. A Signed Policy Document enables unauthenticated users to upload files to GCS using an HTML form by providing a time-limited signature and enforcing conditions on the upload (like file size limits).
§Example: Generating a Signed POST Policy
use std::time::Duration;
let policy = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "uploads/my-object.txt")
.with_expiration(Duration::from_secs(3600)) // 1 hour
.with_content_length_range(1, 10 * 1024 * 1024) // 1 byte to 10 MiB limit
.with_starts_with("$key", "uploads/") // Enforce upload prefix
.with_field("Content-Type", "text/plain") // Enforce content type
.sign_with(signer)
.await?;
println!("Upload URL: {}", policy.url);
for (key, value) in &policy.fields {
println!("Form field -> {}: {}", key, value);
}§Example: Creating a Signer
You can use google-cloud-auth to create a Signer.
§Using Application Default Credentials (ADC)
use google_cloud_auth::credentials::Builder;
use google_cloud_auth::signer::Signer;
let signer = Builder::default().build_signer()?;§Using a Service Account Key File
use google_cloud_auth::credentials::service_account::Builder;
use google_cloud_auth::signer::Signer;
let service_account_key = serde_json::json!({ /* add details here */ });
let signer = Builder::new(service_account_key).build_signer()?;Implementations§
Source§impl PostPolicyV4Builder
impl PostPolicyV4Builder
Sourcepub fn for_object<B, O>(bucket: B, object: O) -> Self
pub fn for_object<B, O>(bucket: B, object: O) -> Self
Creates a new builder for the specified bucket and object.
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt");Sourcepub fn with_expiration(self, expiration: Duration) -> Self
pub fn with_expiration(self, expiration: Duration) -> Self
Sets the policy expiration duration. Maximum is 7 days (604,800 seconds).
§Example
use std::time::Duration;
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_expiration(Duration::from_secs(3600));Sourcepub fn with_url_style(self, url_style: UrlStyle) -> Self
pub fn with_url_style(self, url_style: UrlStyle) -> Self
Sets the URL formatting style.
§Example
use google_cloud_storage::signed_url::UrlStyle;
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_url_style(UrlStyle::VirtualHostedStyle);Sourcepub fn with_client_email<S: Into<String>>(self, client_email: S) -> Self
pub fn with_client_email<S: Into<String>>(self, client_email: S) -> Self
Sets the authorizer client email. If not set, it falls back to the signer’s email.
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_client_email("my-service-account@my-project.iam.gserviceaccount.com");Sourcepub fn with_universe_domain<S: Into<String>>(self, universe_domain: S) -> Self
pub fn with_universe_domain<S: Into<String>>(self, universe_domain: S) -> Self
Sets the GCS universe domain (defaults to googleapis.com).
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_universe_domain("googleapis.com");Sourcepub fn with_endpoint<S: Into<String>>(self, endpoint: S) -> Self
pub fn with_endpoint<S: Into<String>>(self, endpoint: S) -> Self
Sets a custom endpoint.
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_endpoint("https://private.googleapis.com");Sourcepub fn with_field<K: Into<String>, V: Into<String>>(
self,
key: K,
value: V,
) -> Self
pub fn with_field<K: Into<String>, V: Into<String>>( self, key: K, value: V, ) -> Self
Adds a form field/exact condition match (e.g. “acl” = “public-read”).
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_field("acl", "public-read")
.with_field("Content-Type", "text/plain");Sourcepub fn with_starts_with<F: Into<String>, P: Into<String>>(
self,
field: F,
prefix: P,
) -> Self
pub fn with_starts_with<F: Into<String>, P: Into<String>>( self, field: F, prefix: P, ) -> Self
Adds a starts-with condition constraint (e.g. “$key”, “uploads/”).
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_starts_with("$key", "uploads/");Sourcepub fn with_content_length_range(self, min: u64, max: u64) -> Self
pub fn with_content_length_range(self, min: u64, max: u64) -> Self
Adds a content-length-range constraint (minimum and maximum file size in bytes).
§Example
let builder = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.with_content_length_range(1, 10 * 1024 * 1024); // 1 byte to 10 MiBSourcepub async fn sign_with(
self,
signer: &Signer,
) -> Result<PostPolicyV4Result, SigningError>
pub async fn sign_with( self, signer: &Signer, ) -> Result<PostPolicyV4Result, SigningError>
Sign the policy document.
§Example
async fn run(signer: &Signer) -> anyhow::Result<()> {
let policy = PostPolicyV4Builder::for_object("projects/_/buckets/my-bucket", "my-object.txt")
.sign_with(signer)
.await?;
}Trait Implementations§
Source§impl Clone for PostPolicyV4Builder
impl Clone for PostPolicyV4Builder
Source§fn clone(&self) -> PostPolicyV4Builder
fn clone(&self) -> PostPolicyV4Builder
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for PostPolicyV4Builder
impl RefUnwindSafe for PostPolicyV4Builder
impl Send for PostPolicyV4Builder
impl Sync for PostPolicyV4Builder
impl Unpin for PostPolicyV4Builder
impl UnsafeUnpin for PostPolicyV4Builder
impl UnwindSafe for PostPolicyV4Builder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request