[][src]Crate cloud_storage_rs

This crate aims to simplify interacting with the Google Cloud Storage JSON API. Use it until Google releases a Cloud Storage Client Library for Rust. Shoutout to YMF for funding this free and open source project.

It lazily generates tokens that are cached for one hour. After this hour, at the next request, a new token is fetched from google, which is then again cached for one hour. Caching these tokens is thread-safe, but does require locking.

Authentication is provided from the service-account.json file that you can download from the cloud storage console. Then you must grant this service account the roles Service Account Token Creator and Storage Object Admin. Then specify the path to the service accont file using a .env file or a proper environtment parameter, with the name SERVICE_ACCOUNT.

Examples:

Creating a new Bucket in Google Cloud Storage:

use cloud_storage_rs::Bucket;

let bucket = Bucket::create("mybucket").unwrap();

Connecting to an existing Bucket in Google Cloud Storage:

use cloud_storage_rs::Bucket;

let bucket = Bucket::existing("mybucket"); // note: doesn't fail, even if the name is incorrect

Read a file from disk and store it on googles server:

use std::io::prelude::*;
use std::fs::File;
use cloud_storage_rs::Bucket;

let mut bytes: Vec<u8> = Vec::new();
for byte in File::open("myfile.txt").unwrap().bytes() {
    bytes.push(byte.unwrap())
}
let bucket = Bucket::existing("mybucket");
bucket.upload(&bytes, "mydifferentfilename.txt", "text/plain");

Structs

Bucket

Represents a Bucket in Google Cloud Storage that can be used to upload, download or move files

Error

Represents any of the ways storing something in Google Cloud Storage can fail