Skip to content

Commit

Permalink
feat(services/gcs): Allow setting PredefinedAcl (#1989)
Browse files Browse the repository at this point in the history
  • Loading branch information
silver-ymz committed Apr 14, 2023
1 parent 763fbc3 commit dcafa53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
25 changes: 25 additions & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const DEFAULT_GCS_SCOPE: &str = "https://www.googleapis.com/auth/devstorage.read
/// - `bucket`: Set the container name for backend
/// - `endpoint`: Customizable endpoint setting
/// - `credentials`: Credential string for GCS OAuth2
/// - `predefined_acl`: Predefined ACL for GCS
///
/// You can refer to [`GcsBuilder`]'s docs for more information
///
Expand All @@ -85,6 +86,8 @@ const DEFAULT_GCS_SCOPE: &str = "https://www.googleapis.com/auth/devstorage.read
/// builder.root("/path/to/dir");
/// // set the credentials for GCS OAUTH2 authentication
/// builder.credential("authentication token");
/// // set the predefined ACL for GCS
/// builder.predefined_acl("publicRead");
///
/// let op: Operator = Operator::new(builder)?.finish();
/// Ok(())
Expand All @@ -111,6 +114,7 @@ pub struct GcsBuilder {

http_client: Option<HttpClient>,
customed_token_loader: Option<Box<dyn GoogleTokenLoad>>,
predefined_acl: Option<String>,
}

impl GcsBuilder {
Expand Down Expand Up @@ -198,6 +202,22 @@ impl GcsBuilder {
self.customed_token_loader = Some(token_load);
self
}

/// Set the predefined acl for GCS.
///
/// Available values are:
/// - `authenticatedRead`
/// - `bucketOwnerFullControl`
/// - `bucketOwnerRead`
/// - `private`
/// - `projectPrivate`
/// - `publicRead`
pub fn predefined_acl(&mut self, acl: &str) -> &mut Self {
if !acl.is_empty() {
self.predefined_acl = Some(acl.to_string())
};
self
}
}

impl Debug for GcsBuilder {
Expand All @@ -210,6 +230,9 @@ impl Debug for GcsBuilder {
if self.credential.is_some() {
ds.field("credentials", &"<redacted>");
}
if self.predefined_acl.is_some() {
ds.field("predefined_acl", &self.predefined_acl);
}
ds.finish()
}
}
Expand All @@ -226,6 +249,7 @@ impl Builder for GcsBuilder {
map.get("endpoint").map(|v| builder.endpoint(v));
map.get("credential").map(|v| builder.credential(v));
map.get("scope").map(|v| builder.scope(v));
map.get("predefined_acl").map(|v| builder.predefined_acl(v));

builder
}
Expand Down Expand Up @@ -299,6 +323,7 @@ impl Builder for GcsBuilder {
signer,
token_loader,
credential_loader: cred_loader,
predefined_acl: self.predefined_acl.clone(),
}),
};

Expand Down
8 changes: 7 additions & 1 deletion core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct GcsCore {
pub signer: GoogleSigner,
pub token_loader: GoogleTokenLoader,
pub credential_loader: GoogleCredentialLoader,

pub predefined_acl: Option<String>,
}

impl Debug for GcsCore {
Expand Down Expand Up @@ -136,13 +138,17 @@ impl GcsCore {
) -> Result<Request<AsyncBody>> {
let p = build_abs_path(&self.root, path);

let url = format!(
let mut url = format!(
"{}/upload/storage/v1/b/{}/o?uploadType=media&name={}",
self.endpoint,
self.bucket,
percent_encode_path(&p)
);

if let Some(acl) = &self.predefined_acl {
write!(&mut url, "&predefinedAcl={}", acl).unwrap();
}

let mut req = Request::post(&url);

if let Some(size) = size {
Expand Down

0 comments on commit dcafa53

Please sign in to comment.