You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/29 16:51:03 UTC

[GitHub] [arrow-rs] alamb commented on a diff in pull request #2204: Add Builder style config objects for object_store

alamb commented on code in PR #2204:
URL: https://github.com/apache/arrow-rs/pull/2204#discussion_r933454130


##########
object_store/src/gcp.rs:
##########
@@ -779,55 +785,116 @@ fn reader_credentials_file(
     Ok(serde_json::from_reader(reader).context(DecodeCredentialsSnafu)?)
 }
 
-/// Configure a connection to Google Cloud Storage.
-pub fn new_gcs(
-    service_account_path: impl AsRef<std::path::Path>,
-    bucket_name: impl Into<String>,
-) -> Result<GoogleCloudStorage> {
-    new_gcs_with_client(service_account_path, bucket_name, Client::new())
+/// Configure a connection to Google Cloud Storage using the specified
+/// credentials.
+///
+/// # Example
+/// ```
+/// # let BUCKET_NAME = "foo";
+/// # let SERVICE_ACCOUNT_PATH = "/tmp/foo.json";
+/// let gcs = object_store::gcp::GoogleCloudStorageBuilder::new()
+///  .with_service_account_path(SERVICE_ACCOUNT_PATH)
+///  .with_bucket_name(BUCKET_NAME)
+///  .build();
+/// ```
+#[derive(Debug, Default)]
+pub struct GoogleCloudStorageBuilder {
+    bucket_name: Option<String>,
+    service_account_path: Option<String>,
+    client: Option<Client>,
 }
 
-/// Configure a connection to Google Cloud Storage with the specified HTTP client.
-pub fn new_gcs_with_client(
-    service_account_path: impl AsRef<std::path::Path>,
-    bucket_name: impl Into<String>,
-    client: Client,
-) -> Result<GoogleCloudStorage> {
-    let credentials = reader_credentials_file(service_account_path)?;
-
-    // TODO: https://cloud.google.com/storage/docs/authentication#oauth-scopes
-    let scope = "https://www.googleapis.com/auth/devstorage.full_control";
-    let audience = "https://www.googleapis.com/oauth2/v4/token".to_string();
-
-    let oauth_provider = (!credentials.disable_oauth)
-        .then(|| {
-            OAuthProvider::new(
-                credentials.client_email,
-                credentials.private_key,
-                scope.to_string(),
-                audience,
-            )
-        })
-        .transpose()?;
+impl GoogleCloudStorageBuilder {
+    /// Create a new [`GoogleCloudStorageBuilder`] with default values.
+    pub fn new() -> Self {
+        Default::default()
+    }
 
-    let bucket_name = bucket_name.into();
-    let encoded_bucket_name =
-        percent_encode(bucket_name.as_bytes(), NON_ALPHANUMERIC).to_string();
+    /// Set the bucket name (required)
+    pub fn with_bucket_name(mut self, bucket_name: impl Into<String>) -> Self {
+        self.bucket_name = Some(bucket_name.into());
+        self
+    }
 
-    // The cloud storage crate currently only supports authentication via
-    // environment variables. Set the environment variable explicitly so
-    // that we can optionally accept command line arguments instead.
-    Ok(GoogleCloudStorage {
-        client: Arc::new(GoogleCloudStorageClient {
-            client,
-            base_url: credentials.gcs_base_url,
-            oauth_provider,
-            token_cache: Default::default(),
+    /// Set the path to the service account file (required). Example
+    /// `"/tmp/gcs.json"`
+    ///
+    /// Example contents of `gcs.json`:
+    ///
+    /// ```json
+    /// {
+    ///    "gcs_base_url": "https://localhost:4443",
+    ///    "disable_oauth": true,
+    ///    "client_email": "",
+    ///    "private_key": ""
+    /// }
+    /// ```
+    pub fn with_service_account_path(
+        mut self,
+        service_account_path: impl Into<String>,
+    ) -> Self {
+        self.service_account_path = Some(service_account_path.into());
+        self
+    }
+
+    /// Use the specified http [`Client`] (defaults to [`Client::new`])
+    ///
+    /// This allows you to set custom client options such as allowing
+    /// non secure connections or custom headers.
+    pub fn with_client(mut self, client: Client) -> Self {

Review Comment:
   Since it is only in `test` I will make it `#[cfg(test)]` and we can always make it pub later



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org