Sample code for 30+ languages & platforms
Rust

Google Cloud Storage List Buckets

See more Google Cloud Storage Examples

Demonstrates how to retrieve a list of buckets for a given project.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example uses a previously obtained access token having permission for the 
// scope "https://www.googleapis.com/auth/cloud-platform"

let json_token = chilkat::JsonObject::new();
if json_token.load_file("qa_data/tokens/googleCloudStorage.json").is_err() {
    println!("{}", json_token.last_error_text());
    return;
}

let http = chilkat::Http::new();
http.set_auth_token(&json_token.string_of("access_token").unwrap_or_default());

// For more info see Cloud Storage Documentation - Buckets: list 
// 
let _ = http.set_url_var("PROJECT_ID", "chilkattest-1050").is_ok();
let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", "https://www.googleapis.com/storage/v1/b?project={$PROJECT_ID}", &resp).is_err() {
    println!("{}", http.last_error_text());
    return;
}

let response_code = resp.status_code();
if response_code == 401 {
    println!("{}", resp.body_str());
    println!("If invalid credentials, then it is likely the access token expired.");
    println!("Your app should automatically fetch a new access token and re-try.");
    return;
}

println!("Response code: {}", response_code);
println!("Response body");

let json = chilkat::JsonObject::new();
let _ = json.load(&resp.body_str()).is_ok();

json.set_emit_compact(false);

println!("{}", json.emit().unwrap_or_default());

// A response code = 200 indicates success, and the response body contains JSON such as this:

// {
//   "kind": "storage#buckets",
//   "items": [
//     {
//       "kind": "storage#bucket",
//       "id": "chilkat-bucket",
//       "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-bucket",
//       "projectNumber": "5332332985",
//       "name": "chilkat-bucket",
//       "timeCreated": "2018-10-23T00:04:44.507Z",
//       "updated": "2018-10-23T00:04:44.507Z",
//       "metageneration": "1",
//       "iamConfiguration": {
//         "bucketPolicyOnly": {
//           "enabled": false
//         }
//       },
//       "location": "US",
//       "storageClass": "MULTI_REGIONAL",
//       "etag": "CAE="
//     },
//     {
//       "kind": "storage#bucket",
//       "id": "chilkat-images",
//       "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-images",
//       "projectNumber": "5332332985",
//       "name": "chilkat-images",
//       "timeCreated": "2018-10-23T11:24:43.000Z",
//       "updated": "2018-10-23T11:24:43.000Z",
//       "metageneration": "1",
//       "iamConfiguration": {
//         "bucketPolicyOnly": {
//           "enabled": false
//         }
//       },
//       "location": "US",
//       "storageClass": "MULTI_REGIONAL",
//       "etag": "CAE="
//     }
//   ]
// }

// Use this online tool to generate parsing code from sample JSON: 
// Generate Parsing Code from JSON

let mut i: i32 = 0;

let _ = json.string_of("kind").unwrap_or_default();
i = 0;
let count_i = json.size_of_array("items");
while i < count_i {
    json.set_i(i);
    let _ = json.string_of("items[i].kind").unwrap_or_default();
    let _ = json.string_of("items[i].id").unwrap_or_default();
    let _ = json.string_of("items[i].selfLink").unwrap_or_default();
    let _ = json.string_of("items[i].projectNumber").unwrap_or_default();
    let _ = json.string_of("items[i].name").unwrap_or_default();
    let _ = json.string_of("items[i].timeCreated").unwrap_or_default();
    let _ = json.string_of("items[i].updated").unwrap_or_default();
    let _ = json.string_of("items[i].metageneration").unwrap_or_default();
    let _ = json.bool_of("items[i].iamConfiguration.bucketPolicyOnly.enabled");
    let _ = json.string_of("items[i].location").unwrap_or_default();
    let _ = json.string_of("items[i].storageClass").unwrap_or_default();
    let _ = json.string_of("items[i].etag").unwrap_or_default();
    i = i + 1;
}