Files

The content in this guide refers to the newly released version 3 of the library. You can still access the version 1.x guides here.

Version 1.x is now deprecated. We will continue to support 1.x through March 31, 2017 (End of Support - EOS). If you are currently using 1.x in your apps, you will need to upgrade your apps to 3.x before the EOS date. We have created a migration guide to help you.

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

FileMetaData fileMetaData = new FileMetaData();
fileMetaData.fileName = your_file_name;
bool publicAccess = true;
fileMetaData._public = publicAccess;

byte[] content = System.IO.File.ReadAllBytes(your_file_path);
int contentSize = (content.Length) * sizeof(byte);
fileMetaData.size = contentSize;

MemoryStream streamContent = new MemoryStream(content);

FileMetaData fmd = await kinveyClient.File().uploadAsync(fileMetaData, streamContent);

Byte array

FileMetaData fileMetaData = new FileMetaData();
fileMetaData.fileName = your_file_name;
bool publicAccess = true;
fileMetaData._public = publicAccess;

byte[] byteArrayContent = System.IO.File.ReadAllBytes(your_file_path);
int contentSize = (content.Length) * sizeof(byte);
fileMetaData.size = contentSize;

FileMetaData 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.

FileMetaData fileMetaData = new FileMetaData();
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.

FileMetaData downloadMetaData = new FileMetaData();
downloadMetaData = await kinveyClient.File().downloadMetadataAsync(uploadFMD.id);
downloadMetaData.id = uploadFMD.id;
MemoryStream downloadStreamContent = new MemoryStream();

FileMetaData downloadFMD = await kinveyClient.File().downloadAsync(downloadMetaData, downloadStreamContent);

FileStream fs = new FileStream(downloadStreamFilePath, FileMode.Create);
downloadStreamContent.WriteTo(fs);
downloadStreamContent.Close();
fs.Close();

Byte array

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

FileMetaData downloadMetaData = new FileMetaData();
downloadMetaData = await kinveyClient.File().downloadMetadataAsync(uploadFMD.id);
downloadMetaData.id = uploadFMD.id;
byte[] downloadContent = new byte[downloadMetaData.size];

FileMetaData 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);
Got a question?