Zig Requires Chilkat v11.0.0+
Zig
Frame.io - Upload an Asset
See more Frame.io Examples
Upload an asset to Frame.ioChilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
// 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"
// }
const local_file_path = "qa_data/mp4/rotating_earth.mp4";
const fac = try chilkat.FileAccess.init();
defer fac.deinit();
const file_size = fac.fileSize(local_file_path);
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateInt("filesize", file_size) catch {};
json.updateString("filetype", "video/mp4") catch {};
json.updateString("name", "rotating_earth7") catch {};
json.updateString("type", "file") catch {};
http.setRequestHeader("content-type", "application/json");
// Adds the "Authorization: Bearer <FRAME_IO_DEV_TOKEN>" header.
http.setAuthToken("<FRAME_IO_DEV_TOKEN>");
// Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", json, "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
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.
const num_chunks = j_resp.sizeOfArray("upload_urls");
const size_per_chunk = @divTrunc((file_size + num_chunks - 1), num_chunks);
std.debug.print("numChunks = {d}\n", .{num_chunks});
std.debug.print("sizePerChunk = {d}\n", .{size_per_chunk});
fac.openForRead(local_file_path) catch {
std.debug.print("{s}\n", .{try fac.getLastErrorText(alloc)});
return;
};
const bd = try chilkat.BinData.init();
defer bd.deinit();
const http_for_upload = try chilkat.Http.init();
defer http_for_upload.deinit();
http_for_upload.setRequestHeader("x-amz-acl", "private");
var i: i32 = 0;
while (i < num_chunks) {
bd.clear() catch {};
try fac.readBlockBd(i, size_per_chunk, bd);
j_resp.setI(i);
const upload_url = try j_resp.stringOf(alloc, "upload_urls[i]");
// Send the chunk in a PUT:
std.debug.print("PUT chunk {d}\n", .{i + 1});
std.debug.print("URL: {s}\n", .{upload_url});
// PUT https://frameio-uploads-production.s3/etc/etc
// Content-Type: video/mp4
// x-amz-acl: private
http_for_upload.httpBd("PUT", upload_url, bd, "video/mp4", resp) catch {
std.debug.print("{s}\n", .{try http_for_upload.getLastErrorText(alloc)});
return;
};
std.debug.print("response status: {d}\n", .{resp.getStatusCode()});
i = i + 1;
}
fac.fileClose();
std.debug.print("File uploaded.\n", .{});
}