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.

Uploading

The library's File API supports uploading a byte[] directly as well as streaming content from an Stream. To upload a file, use myClient.File().upload(). Pass a FileMetaData object, the file contents. FileMetaData lets you set ACLs, mark files as public, and set any other custom attributes your app may need.

Stream

var content = System.IO.File.ReadAllBytes(your_file_path);
var contentSize = content.Length * sizeof(byte);
var fileMetaData = new FileMetaData
{
    fileName = your_file_name,
    _public = true,
    size = contentSize
};
var streamContent = new MemoryStream(content);
var fmd = await kinveyClient.File().uploadAsync(fileMetaData, streamContent);

Byte array (Upload)

var byteArrayContent = System.IO.File.ReadAllBytes(your_file_path);
var contentSize = (content.Length) * sizeof(byte);
var fileMetaData = new FileMetaData
{
    fileName = your_file_name,
    _public = true,
    size = contentSize
};
var fmd = await kinveyClient.File().uploadAsync(fileMetaData, byteArrayContent);

Uploading publicly readable files

Use the method fileMetaData._public = true if you want to upload a publicly-readable file. This means that the download link to the file will not expire until you delete the file through Kinvey.

var fileMetaData = new FileMetaData
{
    _public = true
};

Downloading

The library's File API supports downloading a File into a byte[] directly as well as streaming content from an Stream. To download a file, use myClient.File().download(). Pass a FileMetaData object, and the location of the file you want. FileMetaData requires an _id to determine which file to download.

Stream

You can download a file directly to a Stream.

var downloadMetaData = await kinveyClient.File().downloadMetadataAsync(uploadFMD.id);
downloadMetaData.id = uploadFMD.id;
using (var downloadStreamContent = new MemoryStream())
{
    var downloadFMD = await kinveyClient.File().DownloadAsync(downloadMetaData, downloadStreamContent);
    using (var fs = new FileStream(downloadStreamFilePath, FileMode.Create))
    {
        downloadStreamContent.WriteTo(fs);
    }
}

Byte array (Download)

You can download a file directly into a byte[].

var downloadMetaData = await kinveyClient.File().downloadMetadataAsync(uploadFMD.id);
downloadMetaData.id = uploadFMD.id;
var downloadContent = new byte[downloadMetaData.size];
var downloadFMD = await kinveyClient.File().DownloadAsync(downloadMetaData, downloadContent);
System.IO.File.WriteAllBytes(your_file_path, content);

Deleting

You can permanently remove a file using the delete() method.

KinveyDeleteResponse kdr = await kinveyClient.File().delete(fileMetaData.id);