#google-cloud #cloud-storage #storage #gcp #gc

google-cloud-storage

Google Cloud Platform storage client library

25 releases (16 breaking)

new 0.17.0 Apr 18, 2024
0.16.0 Feb 27, 2024
0.15.0 Dec 3, 2023
0.14.0 Oct 15, 2023
0.3.0 Jul 10, 2022

#2 in #gc

Download history 5222/week @ 2023-12-23 8436/week @ 2023-12-30 11391/week @ 2024-01-06 11400/week @ 2024-01-13 13106/week @ 2024-01-20 13408/week @ 2024-01-27 13994/week @ 2024-02-03 12419/week @ 2024-02-10 15473/week @ 2024-02-17 24590/week @ 2024-02-24 21232/week @ 2024-03-02 24350/week @ 2024-03-09 20606/week @ 2024-03-16 24825/week @ 2024-03-23 29899/week @ 2024-03-30 15089/week @ 2024-04-06

95,296 downloads per month
Used in 7 crates

MIT license

305KB
4.5K SLoC

google-cloud-storage

Google Cloud Platform Storage Client library.

crates.io

Installation

[dependencies]
google-cloud-storage = "version"

Quickstart

Authentication

There are two ways to create a client that is authenticated against the google cloud.

Automatically

The function with_auth() will try and read the credentials from a file specified in the environment variable GOOGLE_APPLICATION_CREDENTIALS, GOOGLE_APPLICATION_CREDENTIALS_JSON or from a metadata server.

This is also described in google-cloud-auth

use google_cloud_storage::client::{ClientConfig, Client};

async fn run() {
    let config = ClientConfig::default().with_auth().await.unwrap();
    let client = Client::new(config);
}

Manually

When you can't use the gcloud authentication but you have a different way to get your credentials (e.g a different environment variable) you can parse your own version of the 'credentials-file' and use it like that:

use google_cloud_auth::credentials::CredentialsFile;
// or google_cloud_storage::client::google_cloud_auth::credentials::CredentialsFile
use google_cloud_storage::client::{ClientConfig, Client};

async fn run(cred: CredentialsFile) {
    let config = ClientConfig::default().with_credentials(cred).await.unwrap();
    let client = Client::new(config);
}

Anonymous Access

To provide anonymous access without authentication, do the following.

use google_cloud_storage::client::{ClientConfig, Client};

async fn run() {
    let config = ClientConfig::default().anonymous();
    let client = Client::new(config);
}

Passing a custom reqwest middleware cliemt

use google_cloud_storage::client::Client;
use google_cloud_storage::client::ClientConfig;
use google_cloud_storage::http::Error;
use reqwest_middleware::ClientBuilder;
use reqwest_retry::policies::ExponentialBackoff;
use reqwest_retry::RetryTransientMiddleware;
use retry_policies::Jitter;

async fn run() -> Result<(), Error> {
    let retry_policy = ExponentialBackoff::builder()
        .base(2)
        .jitter(Jitter::Full)
        .build_with_max_retries(3);

    let mid_client = ClientBuilder::new(reqwest::Client::default())
        // reqwest-retry already comes with a default retry stategy that matches http standards
        // override it only if you need a custom one due to non standard behaviour
        .with(RetryTransientMiddleware::new_with_policy(retry_policy))
        .build();

    Client::new(
        ClientConfig {
            http: Some(mid_client),
            ..Default::default()
        }
        .with_auth()
        .await?,
    );

    Ok(())
}

Usage

use google_cloud_storage::client::Client;
use google_cloud_storage::client::ClientConfig;
use google_cloud_storage::sign::SignedURLOptions;
use google_cloud_storage::sign::SignBy;
use google_cloud_storage::sign::SignedURLMethod;
use google_cloud_storage::http::Error;
use google_cloud_storage::http::objects::download::Range;
use google_cloud_storage::http::objects::get::GetObjectRequest;
use google_cloud_storage::http::objects::upload::{Media, UploadObjectRequest, UploadType};
use tokio::task::JoinHandle;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;

async fn run(config: ClientConfig) -> Result<(), Error> {

    // Create client.
    let mut client = Client::new(config);

    // Upload the file
    let upload_type = UploadType::Simple(Media::new("file.png"));
    let uploaded = client.upload_object(&UploadObjectRequest {
        bucket: "bucket".to_string(),
        ..Default::default()
    }, "hello world".as_bytes(), &upload_type).await;

    // Download the file
    let data = client.download_object(&GetObjectRequest {
        bucket: "bucket".to_string(),
        object: "file.png".to_string(),
        ..Default::default()
   }, &Range::default()).await;

    // Create signed url with the default key and google-access-id of the client
    let url_for_download = client.signed_url("bucket", "foo.txt", None, None, SignedURLOptions::default());
    let url_for_upload = client.signed_url("bucket", "foo.txt", None, None, SignedURLOptions {
        method: SignedURLMethod::PUT,
        ..Default::default()
    });

    Ok(())
}

Dependencies

~16–33MB
~623K SLoC