Files

You can use Kinvey to store and retrieve binary files of size up to 5TB. Files can be of any format.

The files are automatically enabled for download using a Content Delivery Network (CDN) for massive scale and performance.

Kinvey does not directly serve or accept files. Instead, the Kinvey Files API works by providing a short-lived URL to a third-party cloud storage service from which file(s) can be uploaded or downloaded. Currently, the third-party service used is Google Cloud Storage.

You would typically use the Files API to upload and download:

  • images
  • video files
  • other application-specific files.

Kinvey can transmit to and from our file store service and place files in memory or at a supplied path. These are most commonly images, videos, and audio files, but can be any data. The service also supports streaming media.

The Apple App Store guidelines cover cases where apps may be rejected from the App Store due to transmitting/receiving large files. Please read the App Store guidelines for guidance.

File operations are done through the FileStore class using the overloaded upload() and download() methods.

Both upload and download operations are resumable. To resume a download operation, use the same File instance. To resume an upload operation, the File instance just needs to have the same fileId in order to resume a previous upload operation.

Upload

Uploading a file is done calling the upload() method and passing in a File instance, which represents the metadata of the file, and a second parameter which can be a String path, NSData, UIImage (which will be saved as a PNG file) or NSInputStream.

let fileStore = FileStore<File>()

let file = File()
file.publicAccessible = true
let path = "<#...#>" //String path for the file that will be upload
//Uploads a file using a file string path
fileStore.upload(file, path: path) { file, error in
    if let file = file {
        //success
        print("File: \(file)")
    } else {
        //fail
    }
}

Download

In order to download a file, the fileId is required.

let fileStore = FileStore<File>()

let file = File()
file.fileId = "<#...#>" //File ID returned after you upload the file
fileStore.download(file) { (file, data: NSData?, error) in
    if let file = file, let data = data {
        //success
        print("File: \(file)")
        print("Data: \(data.length)")
    } else {
        //fail
    }
}
let fileStore = FileStore<File>()

let file = File()
file.fileId = "<#...#>" //File ID returned after you upload the file
fileStore.download(file) { (file, data: Data?, error) in
    if let file = file, let data = data {
        //success
        print("File: \(file)")
        print("Data: \(data.count)")
    } else {
        //fail
    }
}

By default, files are cached in order to improve performance.

let fileStore = FileStore<File>()
let file = File()
file.fileId = "<#...#>" //File ID returned after you upload the file
fileStore.download(file) { (file, url: NSURL?, error) in
    if let file = file, let url = url {
        //success
        print("File: \(file)")
        print("URL: \(url)")
    } else {
        //fail
    }
}
let fileStore = FileStore<File>()
let file = File()
file.fileId = "<#...#>" //File ID returned after you upload the file
fileStore.download(file) { (file, url: URL?, error) in
    if let file = file, let url = url {
        //success
        print("File: \(file)")
        print("URL: \(url)")
    } else {
        //fail
    }
}

To disable the ability of caching files please use the storeType optional parameter.

fileStore.download(file, storeType: .Network) { (file, url: NSURL?, error) in
    if let file = file, let url = url {
        //success
        print("File: \(file)")
        print("URL: \(url)")
    } else {
        //fail
    }
}
fileStore.download(file, storeType: .network) { (file, url: URL?, error) in
    if let file = file, let url = url {
        //success
        print("File: \(file)")
        print("URL: \(url)")
    } else {
        //fail
    }
}

Progress

In both operations, upload() or download(), there's a progress handler that allows you to be reported about the progress status of your request. The progress block can be called multiple times during the period of your request and will contain an instance of ProgressStatus which contains all the information that you need to be update your progress User Interface.

Uploading with progress:

let fileStore = FileStore<File>()
let file = File()
file.publicAccessible = true
let path = "<#...#>" //String path for the file that will be upload
//Uploads a file using a file string path
let request = fileStore.upload(file, path: path) { (file, error) in
    //completion handler
}

request.progress = {
    //progress handler
    print("Upload: \($0.countOfBytesSent)/\($0.countOfBytesExpectedToSend)")
}

Downloading with progress:

let request = fileStore.download(file) { (file, url: NSURL?, error) in
    //completion handler
}

request.progress = {
    //progress handler
    print("Download: \($0.countOfBytesReceived)/\($0.countOfBytesExpectedToReceive)")
}
let request = fileStore.download(file) { (file, url: URL?, error) in
    //completion handler
}

request.progress = {
    //progress handler
    print("Download: \($0.countOfBytesReceived)/\($0.countOfBytesExpectedToReceive)")
}
Got a question?