Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Frame.io - Upload an Asset

See more Frame.io Examples

Upload an asset to Frame.io

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local 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"
-- }

local localFilePath = "qa_data/mp4/rotating_earth.mp4"
local fac = chilkat.FileAccess.new()
local fileSize = fac:file_size(localFilePath)

local json = chilkat.JsonObject.new()
json:update_int("filesize", fileSize)
json:update_string("filetype", "video/mp4")
json:update_string("name", "rotating_earth7")
json:update_string("type", "file")

http:set_request_header("content-type", "application/json")
-- Adds the "Authorization: Bearer <FRAME_IO_DEV_TOKEN>" header.
http.auth_token = "<FRAME_IO_DEV_TOKEN>"

-- Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- 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.
local numChunks = jResp:size_of_array("upload_urls")
local sizePerChunk = math.floor((fileSize + numChunks - 1) / numChunks)

print("numChunks = " .. numChunks)
print("sizePerChunk = " .. sizePerChunk)

success = fac:open_for_read(localFilePath)
if not success then
    print(fac.last_error_text)
    return
end

local bd = chilkat.BinData.new()
local httpForUpload = chilkat.Http.new()

httpForUpload:set_request_header("x-amz-acl", "private")

local i = 0
while i < numChunks do
    bd:clear()
    success = fac:read_block_bd(i, sizePerChunk, bd)

    jResp.i = i
    local uploadUrl = jResp:string_of("upload_urls[i]")

    -- Send the chunk in a PUT:

    print("PUT chunk " .. (i + 1))
    print("URL: " .. uploadUrl)

    -- PUT https://frameio-uploads-production.s3/etc/etc
    -- Content-Type: video/mp4
    -- x-amz-acl: private
    success = httpForUpload:http_bd("PUT", uploadUrl, bd, "video/mp4", resp)
    if not success then
        print(httpForUpload.last_error_text)
        return
    end

    print("response status: " .. resp.status_code)

    i = i + 1
end

fac:file_close()

print("File uploaded.")