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

```rust use googlecloudstorage::client::{ClientConfig, Client}; use googleclouddefault::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:

```rust use googlecloudauth::credentials::CredentialsFile; use googlecloudstorage::client::{ClientConfig, Client}; use googleclouddefault::WithAuthExt;

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

Usage

```rust use googlecloudstorage::client::Client; use googlecloudstorage::client::ClientConfig; use googlecloudstorage::sign::SignedURLOptions; use googlecloudstorage::sign::SignBy; use googlecloudstorage::sign::SignedURLMethod; use googlecloudstorage::http::Error; use googlecloudstorage::http::objects::download::Range; use googlecloudstorage::http::objects::get::GetObjectRequest; use googlecloudstorage::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(())

} ```