[][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.

This project talks to Google using a Service Account. A service account is an account that you must create in the cloud storage console. When the account is created, you can download the file service-account-********.json. Store this file somewhere on your machine, and place the path to this file in the environment parameter SERVICE_ACCOUNT. Environment parameters declared in the .env file are also registered. The service account can then be granted Roles in the cloud storage console. The roles required for this project to function are Service Account Token Creator and Storage Object Admin.

Examples:

Creating a new Bucket in Google Cloud Storage:

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

Connecting to an existing Bucket in Google Cloud Storage:

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:

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");

Renaming or and moving a file

let bucket = Bucket::existing("mybucket");
bucket.update("old/path/to/resource.txt", "newname.txt").unwrap();

Removing a file

let bucket = Bucket::existing("mybucket");
bucket.update("old/path/to/resource.txt", "newname.txt").unwrap();

Structs

Bucket

Represents a Bucket in Google Cloud Storage that can be used to upload, download or move files. Internally, the files in the bucket live in a flat namespace, that is, the slashes that indicate folders are simply part of the filename. This means that renaming a file and moving it to a different directory are the same operation.

Error

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