Swift
Swift
Frame.io - Upload an Asset
See more Frame.io Examples
Upload an asset to Frame.ioChilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// 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"
// }
var localFilePath: String? = "qa_data/mp4/rotating_earth.mp4"
let fac = CkoFileAccess()!
var fileSize: Int = fac.fileSize(path: localFilePath).intValue
let json = CkoJsonObject()!
json.updateInt(jsonPath: "filesize", value: fileSize)
json.updateString(jsonPath: "filetype", value: "video/mp4")
json.updateString(jsonPath: "name", value: "rotating_earth7")
json.updateString(jsonPath: "type", value: "file")
http.setRequestHeader(name: "content-type", value: "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
let resp = CkoHttpResponse()!
success = http.httpJson(verb: "POST", url: "https://api.frame.io/v2/assets/039845e8-bffe-4d6b-88d3-c780bae06342/children", json: json, contentType: "application/json", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: sbResponseBody)
jResp.emitCompact = false
print("Response Body:")
print("\(jResp.emit()!)")
var respStatusCode: Int = resp.statusCode.intValue
print("Response Status Code = \(respStatusCode)")
if respStatusCode >= 400 {
print("Response Header:")
print("\(resp.header!)")
print("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.
var numChunks: Int = jResp.size(ofArray: "upload_urls").intValue
var sizePerChunk: Int = (fileSize + numChunks - 1) / numChunks
print("numChunks = \(numChunks)")
print("sizePerChunk = \(sizePerChunk)")
success = fac.open(forRead: localFilePath)
if success == false {
print("\(fac.lastErrorText!)")
return
}
let bd = CkoBinData()!
let httpForUpload = CkoHttp()!
httpForUpload.setRequestHeader(name: "x-amz-acl", value: "private")
var i: Int = 0
while (i < numChunks) {
bd.clear()
success = fac.readBlockBd(blockIndex: i, blockSize: sizePerChunk, bd: bd)
jResp.i = i
var uploadUrl: String? = 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.httpBd(verb: "PUT", url: uploadUrl, bd: bd, contentType: "video/mp4", response: resp)
if success == false {
print("\(httpForUpload.lastErrorText!)")
return
}
print("response status: \(resp.statusCode.intValue)")
i = i + 1
}
fac.fileClose()
print("File uploaded.")
}