#gcp #gcs #storage #googleapis #google-cloud-rust

google-cloud-storage

Google Cloud Platform storage client library

17 releases (10 breaking)

0.11.0 Apr 23, 2023
0.10.0 Mar 17, 2023
0.9.0 Feb 22, 2023
0.7.0 Dec 20, 2022
0.3.0 Jul 10, 2022

#439 in Web programming

Download history 346/week @ 2023-02-04 450/week @ 2023-02-11 954/week @ 2023-02-18 1319/week @ 2023-02-25 1182/week @ 2023-03-04 2293/week @ 2023-03-11 3046/week @ 2023-03-18 2449/week @ 2023-03-25 3089/week @ 2023-04-01 3058/week @ 2023-04-08 1832/week @ 2023-04-15 1723/week @ 2023-04-22 1684/week @ 2023-04-29 2342/week @ 2023-05-06 2621/week @ 2023-05-13 2422/week @ 2023-05-20

9,360 downloads per month
Used in 2 crates

MIT license

270KB
4K SLoC

google-cloud-storage

Google Cloud Platform Storage Client library.

crates.io

Installation

[dependencies]
google-cloud-storage = <version>
google-cloud-default = { version = <version>, features = ["storage"] }

Quickstart

Authentication

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

The crate google-cloud-default provides two methods that help to implement those.

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};
use google_cloud_default::WithAuthExt;

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;
use google_cloud_storage::client::{ClientConfig, Client};
use google_cloud_default::WithAuthExt;

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

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

~15–26MB
~598K SLoC