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

Frame.io - Upload an Asset

See more Frame.io Examples

Upload an asset to Frame.io

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.FileAccess;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example assumes the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new 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"
        // }

        String localFilePath = "qa_data/mp4/rotating_earth.mp4";
        FileAccess fac = new FileAccess();
        int fileSize = fac.fileSize(localFilePath);

        JsonObject json = new JsonObject();
        json.updateInt("filesize", fileSize);
        json.updateString("filetype", "video/mp4");
        json.updateString("name", "rotating_earth7");
        json.updateString("type", "file");

        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
        HttpResponse resp = new HttpResponse();
        try {
            http.httpJson("POST", "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", json, "application/json", resp);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        StringBuilder sbResponseBody = new StringBuilder();
        resp.getBodySb(sbResponseBody);
        JsonObject jResp = new JsonObject();
        jResp.loadSb(sbResponseBody);
        jResp.setEmitCompact(false);

        System.out.println("Response Body:");
        System.out.println(jResp.emit());

        int respStatusCode = resp.getStatusCode();
        System.out.println("Response Status Code = " + respStatusCode);
        if (respStatusCode >= 400) {
            System.out.println("Response Header:");
            System.out.println(resp.getHeader());
            System.out.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.
        int numChunks = jResp.sizeOfArray("upload_urls");
        int sizePerChunk = (fileSize + numChunks - 1) / numChunks;

        System.out.println("numChunks = " + numChunks);
        System.out.println("sizePerChunk = " + sizePerChunk);

        try {
            fac.openForRead(localFilePath);
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        BinData bd = new BinData();
        Http httpForUpload = new Http();

        httpForUpload.setRequestHeader("x-amz-acl", "private");

        int i = 0;
        while (i < numChunks) {
            bd.clear();
            fac.readBlockBd(i, sizePerChunk, bd);

            jResp.setI(i);
            String uploadUrl = jResp.stringOf("upload_urls[i]");

            // Send the chunk in a PUT:

            System.out.println("PUT chunk " + (i + 1));
            System.out.println("URL: " + uploadUrl);

            // PUT https://frameio-uploads-production.s3/etc/etc
            // Content-Type: video/mp4
            // x-amz-acl: private
            try {
                httpForUpload.httpBd("PUT", uploadUrl, bd, "video/mp4", resp);
            } catch (ChilkatException e) {
                System.out.println(e.getLastErrorText());
                return;
            }

            System.out.println("response status: " + resp.getStatusCode());

            i = i + 1;
        }

        fac.fileClose();

        System.out.println("File uploaded.");
    }
}