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.

When using the library, a file can be represented by:

  • A Blob, File, or ArrayBuffer object. This is the recommended way of representing a file.
  • A simple value like a string.

The library always communicates with Google Cloud Storage using the https protocol. If you want to use http instead, the options argument in the methods described below allow for a tls: false flag.

Recommended tutorials to learn more about uploading files are:

Download

To download a file given its _id, use Kinvey.Files.download. The response will contain the file metadata, with the file itself available on the _data property.

import { Kinvey } from 'kinvey-angular2-sdk';

Kinvey.Files.download('<file-id>')
  .then((file: {}) => {
    const fileContent = file._data;
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Stream

For web applications, downloading the actual file is not a common use case. Often, just obtaining a download URL is sufficient. This holds for a variety of use cases, including:

  • Prompt a file for download
  • Embed an application-resource, such as a stylesheet
  • Display an image

To stream a file, use Kinvey.Files.stream. The response will contain the file metadata, with the download URL available as _downloadURL property.

import { Kinvey } from 'kinvey-angular2-sdk';

Kinvey.Files.stream('<file-id>')
  .then((file: {}) => {
    const url = file._downloadURL;
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Query

To retrieve a list of files, the Querying interface is available for files.

The snippet below retrieves a list of all PNG images. The response is a list of file metadata.

import { Kinvey } from 'kinvey-angular2-sdk';

const query = new Kinvey.Query();
query.equalTo('mimeType', 'image/png');
Kinvey.Files.find(query)
  .then((files: {}[]) => {
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Optionally, you can add the download: true flag to download every file found. The file itself will then become available under the _data property of each file in the list.

Be careful when using the download flag. Every file matching the query will be downloaded regardless of its size, which can lead to significant overhead and performance degradation of your application.

The file metadata returned by any of the methods explained above contains a _downloadURL. Depending on your use case, you might want to re-download a file. The Kinvey.Files.downloadByUrl() method allows you to do just that. The method accepts either a _downloadURL, or a JSON object literal containing a _downloadURL property.

import { Kinvey } from 'kinvey-angular2-sdk';

Kinvey.Files.download('<file-id>')
  .then((file: {}) => {
    // After some time passed, redownload the file.
    return Kinvey.Files.downloadByUrl(file);
  })
  .then((file: {}) => {
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Specifying a custom expiration time

If you require the temporary download URL to last longer (or shorter) than the default of one hour, you may optionally specify a custom expiration time (in seconds) using the ttl option. If the file is public, the returned download URL never expires.

import { Kinvey } from 'kinvey-angular2-sdk';

Kinvey.Files.download('<file-id>', {
  ttl: 7200 // Two hours in ms
})
  .then((file: {}) => {
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Upload

Use Kinvey.Files.upload() to upload the file and, optionally, save any associated metadata along with it. The example below uploads a file represented as a string. The _id and _filename properties will be auto-generated by Kinvey if left out.

import { Kinvey } from 'kinvey-angular2-sdk';

const fileContent = '<file-content>';
const metadata ={
  _id: '<file-id>',
  filename: '<filename>',
  mimeType: '<mime-type>',
  size: fileContent.length
};
Kinvey.Files.upload(fileContent, metadata)
  .then((file: {}) => {
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });

Delete

To delete a file, use Kinvey.Files.removeById.

Kinvey.Files.removeById('<file-id>')
  .then(() => {
    // ...
  })
  .catch((error: Kinvey.KinveyBaseError) => {
    // ...
  });
Got a question?