Rust Requires Chilkat v11.0.0+
Rust
Frame.io - Upload an Asset
See more Frame.io Examples
Upload an asset to Frame.ioChilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// Implements the following CURL command:
// curl --request POST \
// --url https://api.frame.io/v2/assets/<ASSET_ID>/children \
// --header 'authorization: Bearer <FRAME_IO_DEV_TOKEN>' \
// --header 'content-type: application/json' \
// --data '{"filesize":1570024 ,"filetype":"video/mp4","name":"rotating_earth","type":"file"}'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "filesize": 1570024,
// "filetype": "video/mp4",
// "name": "rotating_earth",
// "type": "file"
// }
let local_file_path = "qa_data/mp4/rotating_earth.mp4".to_string();
let fac = chilkat::FileAccess::new();
let file_size = fac.file_size(&local_file_path);
let json = chilkat::JsonObject::new();
let _ = json.update_int("filesize", file_size);
let _ = json.update_string("filetype", "video/mp4");
let _ = json.update_string("name", "rotating_earth7");
let _ = json.update_string("type", "file");
http.set_request_header("content-type", "application/json");
// Adds the "Authorization: Bearer <FRAME_IO_DEV_TOKEN>" header.
http.set_auth_token("<FRAME_IO_DEV_TOKEN>");
// Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", &json, "application/json", &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// Upload in chunks to the pre-signed S3 upload URLs.
// There are ways to do this in parallel, but for simplicity we'll show how to upload
// one chunk after another.
let num_chunks = j_resp.size_of_array("upload_urls");
let size_per_chunk = (file_size + num_chunks - 1) / num_chunks;
println!("numChunks = {}", num_chunks);
println!("sizePerChunk = {}", size_per_chunk);
if fac.open_for_read(&local_file_path).is_err() {
println!("{}", fac.last_error_text());
return;
}
let bd = chilkat::BinData::new();
let http_for_upload = chilkat::Http::new();
http_for_upload.set_request_header("x-amz-acl", "private");
let mut i = 0;
while i < num_chunks {
let _ = bd.clear();
let _ = fac.read_block_bd(i, size_per_chunk, &bd).is_ok();
j_resp.set_i(i);
let upload_url = j_resp.string_of("upload_urls[i]").unwrap_or_default();
// Send the chunk in a PUT:
println!("PUT chunk {}", i + 1);
println!("URL: {}", upload_url);
// PUT https://frameio-uploads-production.s3/etc/etc
// Content-Type: video/mp4
// x-amz-acl: private
if http_for_upload.http_bd("PUT", &upload_url, &bd, "video/mp4", &resp).is_err() {
println!("{}", http_for_upload.last_error_text());
return;
}
println!("response status: {}", resp.status_code());
i = i + 1;
}
fac.file_close();
println!("File uploaded.");