Sample code for 30+ languages & platforms
Rust

Create Folder

See more Google Drive Examples

In the Drive API, a folder is essentially a file — one identified by the special folder MIME type application/vnd.google-apps.folder

See Google Drive Files: create for more details.

Also See Working with Folders for more details.

Chilkat Rust Downloads

Rust

// Creating a folder is technically an upload of a 0-length file
// having a MIME type of "application/vnd.google-apps.folder"

let _ = true;

// It 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 
// Google Drive scope.

let g_auth = chilkat::AuthGoogle::new();
g_auth.set_access_token("GOOGLE-DRIVE-ACCESS-TOKEN");

let rest = chilkat::Rest::new();

// Connect using TLS.
let b_auto_reconnect = true;
let _ = rest.connect("www.googleapis.com", 443, true, b_auto_reconnect).is_ok();

// Provide the authentication credentials (i.e. the access token)
let _ = rest.set_auth_google(&g_auth);

// A multipart upload to Google Drive needs a multipart/related Content-Type
let _ = rest.add_header("Content-Type", "multipart/related");

// Specify each part of the request.

// The 1st part is JSON with information about the folder.
rest.set_part_selector("1");
let _ = rest.add_header("Content-Type", "application/json; charset=UTF-8");

let json = chilkat::JsonObject::new();
let _ = json.append_string("name", "testFolder");
let _ = json.append_string("description", "A folder to contain test files.");
let _ = json.append_string("mimeType", "application/vnd.google-apps.folder");
let _ = rest.set_multipart_body_string(&json.emit().unwrap_or_default());

// The 2nd part would be the file content.
// Since this is a folder, skip the 2nd part entirely and go straight to the upload..

let Ok(json_response) = rest.full_request_multipart("POST", "/upload/drive/v3/files?uploadType=multipart") else {
    println!("{}", rest.last_error_text());
    return;
};

// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
    println!("response status code = {}", rest.response_status_code());
    println!("response status text = {}", rest.response_status_text());
    println!("response header: {}", rest.response_header());
    println!("response JSON: {}", json_response);
    return;
}

// Show the JSON response.
let _ = json.load(&json_response);

// Show the full JSON response.
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());

// A successful response looks like this:
// {
//   "kind": "drive#file",
//   "id": "0B53Q6OSTWYolY2tPU1BnYW02T2c",
//   "name": "testFolder",
//   "mimeType": "application/vnd.google-apps.folder"
// }

// Get the fileId:
println!("fileId: {}", json.string_of("id").unwrap_or_default());