yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
247 stars 89 forks source link

Support uploading custom metadata with object #98

Closed johnrichardrinehart closed 1 year ago

johnrichardrinehart commented 1 year ago

Is there any way, currently, to associate metadata with an object? client::upload_object seems to explicitly not support attaching custom metadata to the object. Is it the case that either: 1) This is known and not planned to be supported (if so, why?)? 2) This is something that is planned to be supported but isn't, yet (and, so, a PR would be accepted)? 3) It already is supported, but with another set of methods and objects besides upload_object?

Thank you for open-sourcing this work!

yoshidan commented 1 year ago

I modified the API to upload metadata and published the new version of google-cloud-storage crate. Please try it.

johnrichardrinehart commented 1 year ago

Yeah, that works really well! Thanks a lot!

use std::collections::HashMap;

use google_cloud_default::WithAuthExt;
use google_cloud_storage::{
    client::ClientConfig,
    http::objects::{upload::UploadObjectRequest, Object},
};

fn main() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();

    let client = rt.block_on(async {
        let cfg = ClientConfig::default()
            .with_auth()
            .await
            .map_err(|e| format!("failed to get cloud storage client: {}", e))
            .unwrap();

        return google_cloud_storage::client::Client::new(cfg);
    });

    let mut metadata = HashMap::new();
    metadata.insert("abc".to_string(), "def".to_string());
    metadata.insert("ghi".to_string(), "klm".to_string());

    let object = Object {
        name: "imanobject".into(),
        metadata: Some(metadata),
        ..Default::default()
    };

    let obj = rt
        .block_on(client.upload_object(
            &UploadObjectRequest {
                bucket: "$MYBUCKET".into(),
                ..Default::default()
            },
            vec![0x40, 0x41, 0x42, 0x43],
            &google_cloud_storage::http::objects::upload::UploadType::Multipart(Box::new(object)),
            None,
        ))
        .unwrap();

    println!(
        "I'm yay big: {} with {:?} metadata",
        obj.size,
        obj.metadata.unwrap()
    )
}

and a Cargo.toml of

[package]
name = "google_cloud_storage"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
google-cloud-default = { version = "0.1.0", features = ["storage"] }
google-cloud-storage = { version = "0.9.0", features = ["default"] }
tokio = { version = "1.26.0", features = ["rt", "macros", "rt-multi-thread", "time", "sync"] }

yields

$ pwd                                              
/home/john/code/scratchpads/rust/google_cloud_storage
$ ./target/debug/google_cloud_storage
I'm yay big: 4 with {"ghi": "klm", "abc": "def"} metadata