Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(services/gcs): Allow setting PredefinedAcl #1989

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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