Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Frame.io - Upload an Asset
See more Frame.io Examples
Upload an asset to Frame.ioChilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.FileAccess,
Chilkat.HttpResponse,
Chilkat.StringBuilder,
Chilkat.JsonObject,
Chilkat.Http,
Chilkat.BinData;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
http: THttp;
localFilePath: string;
fac: TFileAccess;
fileSize: Integer;
json: TJsonObject;
resp: THttpResponse;
sbResponseBody: TStringBuilder;
jResp: TJsonObject;
respStatusCode: Integer;
numChunks: Integer;
sizePerChunk: Integer;
bd: TBinData;
httpForUpload: THttp;
i: Integer;
uploadUrl: string;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := THttp.Create;
// 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"
// }
localFilePath := 'qa_data/mp4/rotating_earth.mp4';
fac := TFileAccess.Create;
fileSize := fac.FileSize(localFilePath);
json := TJsonObject.Create;
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.AuthToken := '<FRAME_IO_DEV_TOKEN>';
// Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
resp := THttpResponse.Create;
success := http.HttpJson('POST','https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children',json,'application/json',resp);
if (success = False) then
begin
WriteLn(http.LastErrorText);
Exit;
end;
sbResponseBody := TStringBuilder.Create;
resp.GetBodySb(sbResponseBody);
jResp := TJsonObject.Create;
jResp.LoadSb(sbResponseBody);
jResp.EmitCompact := False;
WriteLn('Response Body:');
WriteLn(jResp.Emit());
respStatusCode := resp.StatusCode;
WriteLn('Response Status Code = ' + respStatusCode);
if (respStatusCode >= 400) then
begin
WriteLn('Response Header:');
WriteLn(resp.Header);
WriteLn('Failed.');
Exit;
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.
numChunks := jResp.SizeOfArray('upload_urls');
sizePerChunk := (fileSize + numChunks - 1) / numChunks;
WriteLn('numChunks = ' + numChunks);
WriteLn('sizePerChunk = ' + sizePerChunk);
success := fac.OpenForRead(localFilePath);
if (success = False) then
begin
WriteLn(fac.LastErrorText);
Exit;
end;
bd := TBinData.Create;
httpForUpload := THttp.Create;
httpForUpload.SetRequestHeader('x-amz-acl','private');
i := 0;
while (i < numChunks) do
begin
bd.Clear();
success := fac.ReadBlockBd(i,sizePerChunk,bd);
jResp.I := i;
uploadUrl := jResp.StringOf('upload_urls[i]');
// Send the chunk in a PUT:
WriteLn('PUT chunk ' + i + 1);
WriteLn('URL: ' + uploadUrl);
// PUT https://frameio-uploads-production.s3/etc/etc
// Content-Type: video/mp4
// x-amz-acl: private
success := httpForUpload.HttpBd('PUT',uploadUrl,bd,'video/mp4',resp);
if (success = False) then
begin
WriteLn(httpForUpload.LastErrorText);
Exit;
end;
WriteLn('response status: ' + resp.StatusCode);
i := i + 1;
end;
fac.FileClose();
WriteLn('File uploaded.');
http.Free;
fac.Free;
json.Free;
resp.Free;
sbResponseBody.Free;
jResp.Free;
bd.Free;
httpForUpload.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.