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

Frame.io - Upload an Asset

See more Frame.io Examples

Upload an asset to Frame.io

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL cLocalFilePath
LOCAL oFac
LOCAL nFileSize
LOCAL oJson
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL nNumChunks
LOCAL nSizePerChunk
LOCAL oBd
LOCAL oHttpForUpload
LOCAL i
LOCAL cUploadUrl

nSuccess := 0

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

oHttp := CreateObject("Chilkat.Http")

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

cLocalFilePath := "qa_data/mp4/rotating_earth.mp4"
oFac := CreateObject("Chilkat.FileAccess")
nFileSize := oFac:FileSize(cLocalFilePath)

oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateInt("filesize", nFileSize)
oJson:UpdateString("filetype", "video/mp4")
oJson:UpdateString("name", "rotating_earth7")
oJson:UpdateString("type", "file")

oHttp:SetRequestHeader("content-type", "application/json")
//  Adds the "Authorization: Bearer <FRAME_IO_DEV_TOKEN>" header.
oHttp:AuthToken := "<FRAME_IO_DEV_TOKEN>"

//  Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpJson("POST", "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", oJson, "application/json", oResp)
IF (nSuccess == 0)
    ? oHttp:LastErrorText
    oHttp:destroy()
    oFac:destroy()
    oJson:destroy()
    oResp:destroy()
    RETURN
ENDIF

oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0

? "Response Body:"
? oJResp:Emit()

nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
    ? "Response Header:"
    ? oResp:Header
    ? "Failed."
    oHttp:destroy()
    oFac:destroy()
    oJson:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

//  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.
nNumChunks := oJResp:SizeOfArray("upload_urls")
nSizePerChunk := (nFileSize + nNumChunks - 1) / nNumChunks

? "numChunks = " + Str(nNumChunks)
? "sizePerChunk = " + Str(nSizePerChunk)

nSuccess := oFac:OpenForRead(cLocalFilePath)
IF (nSuccess == 0)
    ? oFac:LastErrorText
    oHttp:destroy()
    oFac:destroy()
    oJson:destroy()
    oResp:destroy()
    oSbResponseBody:destroy()
    oJResp:destroy()
    RETURN
ENDIF

oBd := CreateObject("Chilkat.BinData")
oHttpForUpload := CreateObject("Chilkat.Http")

oHttpForUpload:SetRequestHeader("x-amz-acl", "private")

i := 0
DO WHILE (i < nNumChunks)
    oBd:Clear()
    nSuccess := oFac:ReadBlockBd(i, nSizePerChunk, oBd)

    oJResp:I := i
    cUploadUrl := oJResp:StringOf("upload_urls[i]")

    //  Send the chunk in a PUT:

    ? "PUT chunk " + Str(i + 1)
    ? "URL: " + cUploadUrl

    //  PUT https://frameio-uploads-production.s3/etc/etc
    //  Content-Type: video/mp4
    //  x-amz-acl: private
    nSuccess := oHttpForUpload:HttpBd("PUT", cUploadUrl, oBd, "video/mp4", oResp)
    IF (nSuccess == 0)
        ? oHttpForUpload:LastErrorText
        oHttp:destroy()
        oFac:destroy()
        oJson:destroy()
        oResp:destroy()
        oSbResponseBody:destroy()
        oJResp:destroy()
        oBd:destroy()
        oHttpForUpload:destroy()
        RETURN
    ENDIF

    ? "response status: " + Str(oResp:StatusCode)

    i := i + 1
ENDDO

oFac:FileClose()

? "File uploaded."

oHttp:destroy()
oFac:destroy()
oJson:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
oBd:destroy()
oHttpForUpload:destroy()