Sample code for 30+ languages & platforms
SQL Server

Frame.io - Upload an Asset

See more Frame.io Examples

Upload an asset to Frame.io

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

    DECLARE @localFilePath nvarchar(4000)
    SELECT @localFilePath = 'qa_data/mp4/rotating_earth.mp4'
    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    DECLARE @fileSize int
    EXEC sp_OAMethod @fac, 'FileSize', @fileSize OUT, @localFilePath

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateInt', @success OUT, 'filesize', @fileSize
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'filetype', 'video/mp4'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'name', 'rotating_earth7'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'file'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'content-type', 'application/json'
    -- Adds the "Authorization: Bearer <FRAME_IO_DEV_TOKEN>" header.
    EXEC sp_OASetProperty @http, 'AuthToken', '<FRAME_IO_DEV_TOKEN>'

    -- Uploading to asset ID: 039845e8-bffe-4d6b-88d3-c780bae06342
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpJson', @success OUT, 'POST', 'https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children', @json, 'application/json', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody
    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        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.
    DECLARE @numChunks int
    EXEC sp_OAMethod @jResp, 'SizeOfArray', @numChunks OUT, 'upload_urls'
    DECLARE @sizePerChunk int
    SELECT @sizePerChunk = (@fileSize + @numChunks - 1) / @numChunks


    PRINT 'numChunks = ' + @numChunks

    PRINT 'sizePerChunk = ' + @sizePerChunk

    EXEC sp_OAMethod @fac, 'OpenForRead', @success OUT, @localFilePath
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @fac
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END

    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    DECLARE @httpForUpload int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @httpForUpload OUT

    EXEC sp_OAMethod @httpForUpload, 'SetRequestHeader', NULL, 'x-amz-acl', 'private'

    DECLARE @i int
    SELECT @i = 0
    WHILE (@i < @numChunks)
      BEGIN
        EXEC sp_OAMethod @bd, 'Clear', @success OUT
        EXEC sp_OAMethod @fac, 'ReadBlockBd', @success OUT, @i, @sizePerChunk, @bd

        EXEC sp_OASetProperty @jResp, 'I', @i
        DECLARE @uploadUrl nvarchar(4000)
        EXEC sp_OAMethod @jResp, 'StringOf', @uploadUrl OUT, '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
        EXEC sp_OAMethod @httpForUpload, 'HttpBd', @success OUT, 'PUT', @uploadUrl, @bd, 'video/mp4', @resp
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @httpForUpload, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @fac
            EXEC @hr = sp_OADestroy @json
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @sbResponseBody
            EXEC @hr = sp_OADestroy @jResp
            EXEC @hr = sp_OADestroy @bd
            EXEC @hr = sp_OADestroy @httpForUpload
            RETURN
          END


        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'response status: ' + @iTmp0

        SELECT @i = @i + 1
      END

    EXEC sp_OAMethod @fac, 'FileClose', NULL


    PRINT 'File uploaded.'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp
    EXEC @hr = sp_OADestroy @bd
    EXEC @hr = sp_OADestroy @httpForUpload


END
GO