Rust
Rust
Google Cloud Storage List Bucket Objects
See more Google Cloud Storage Examples
List the objects in a Google Cloud Storage bucket.Chilkat Rust Downloads
// 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"
// In this example, Get Google Cloud Storage OAuth2 Access Token,
// the service account access token was saved to a text file. This example fetches the access token from the file..
let sb_token = chilkat::StringBuilder::new();
let _ = sb_token.load_file("qa_data/tokens/googleCloudStorageAccessToken.txt", "utf-8");
let http = chilkat::Http::new();
http.set_auth_token(&sb_token.get_as_string().unwrap_or_default());
// For more info see Cloud Storage Documentation - Objects: list
//
let _ = http.set_url_var("bucket", "chilkat-ocean").is_ok();
let _ = http.set_url_var("PROJECT_NAME", "ChilkatCloud").is_ok();
let resp = chilkat::HttpResponse::new();
if http.http_no_body("GET", "https://www.googleapis.com/storage/v1/b/{$bucket}/o?project={$ChilkatCloud}", &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#objects",
// "items": [
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/file with space char.jpg/1555776690752366",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/file%20with%20space%20char.jpg?generation=1555776690752366&alt=media",
// "name": "file with space char.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555776690752366",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "CO6WguiH3+ECEAE=",
// "timeCreated": "2019-04-20T16:11:30.752Z",
// "updated": "2019-04-20T16:11:30.752Z",
// "timeStorageClassUpdated": "2019-04-20T16:11:30.752Z"
// },
// {
// "kind": "storage#object",
//
// "id": "chilkat-ocean/penguins.jpg/1555775749593296",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/penguins.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/penguins.jpg?generation=1555775749593296&alt=media",
// "name": "penguins.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1555775749593296",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "777835",
// "md5Hash": "nTd7EM53jEk4s8fixjoimg==",
// "crc32c": "ixxYVw==",
// "etag": "CNCxnqeE3+ECEAE=",
// "timeCreated": "2019-04-20T15:55:49.593Z",
// "updated": "2019-04-20T15:55:49.593Z",
// "timeStorageClassUpdated": "2019-04-20T15:55:49.593Z"
// },
// {
// "kind": "storage#object",
// "id": "chilkat-ocean/starfish.jpg/1628958192639968",
// "selfLink": "https://www.googleapis.com/storage/v1/b/chilkat-ocean/o/starfish.jpg",
// "mediaLink": "https://www.googleapis.com/download/storage/v1/b/chilkat-ocean/o/starfish.jpg?generation=1628958192639968&alt=media",
// "name": "starfish.jpg",
// "bucket": "chilkat-ocean",
// "generation": "1628958192639968",
// "metageneration": "1",
// "contentType": "image/jpeg",
// "storageClass": "MULTI_REGIONAL",
// "size": "6229",
// "md5Hash": "LpxZ2/JmI2fcl9/dqF2gSA==",
// "crc32c": "9RjgwQ==",
// "etag": "COC/tJP2sPICEAE=",
// "timeCreated": "2021-08-14T16:23:12.660Z",
// "updated": "2021-08-14T16:23:12.660Z",
// "timeStorageClassUpdated": "2021-08-14T16:23:12.660Z"
// }
// ]
// }
// Use this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let mut kind = json.string_of("kind").unwrap_or_default();
let mut i = 0;
let count_i = json.size_of_array("items");
while i < count_i {
json.set_i(i);
kind = 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].mediaLink").unwrap_or_default();
let _ = json.string_of("items[i].name").unwrap_or_default();
let _ = json.string_of("items[i].bucket").unwrap_or_default();
let _ = json.string_of("items[i].generation").unwrap_or_default();
let _ = json.string_of("items[i].metageneration").unwrap_or_default();
let _ = json.string_of("items[i].contentType").unwrap_or_default();
let _ = json.string_of("items[i].storageClass").unwrap_or_default();
let _ = json.string_of("items[i].size").unwrap_or_default();
let _ = json.string_of("items[i].md5Hash").unwrap_or_default();
let _ = json.string_of("items[i].crc32c").unwrap_or_default();
let _ = json.string_of("items[i].etag").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].timeStorageClassUpdated").unwrap_or_default();
i = i + 1;
}