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

Uploading to Kinvey is a two-step process. First, you must send a PUT or POST request to Kinvey. You can use this initial request to store the filename, ACLs, and any other metadata having to do with the file you wish to upload. Then, you will upload your file directly to Google Cloud Storage using a URL provided by Kinvey.

First, send a POST request to /blob/:appKey. Alternatively, you may send a PUT request to /blob/:appKey/:fileId. Your request body may optionally contain a _filename, ACLs and other metadata:

POST /blob/:appKey HTTP/1.1
Host: baas.kinvey.com
Content-Type: application/json
X-Kinvey-API-Version: 3
Authorization: [user credentials]

{
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata"
}

If a filename is not specified, a UUID will be used to populate the _filename property.

The response from Kinvey will contain the file's _id and _filename, any other metadata that you submitted, as well as an _uploadURL property containing a URL to which you will upload your file and an _expiresAt property containing the time at which the upload link will expire. The response may also contain a _requiredHeaders property, which is a hash of headers and their values. If this property exists as part of the response, you must include all headers specified in it, set to the respective values, when uploading your file to the _uploadURL.

HTTP/1.1 201 OK
Content-Type: application/json

{
    "_id": "<uuid>",
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata",
    "_uploadURL": <Google Cloud Storage upload URL>,
    "_expiresAt": "2013-06-18T23:07:23.394Z"
    "_requiredHeaders": {
        "x-goog-acl": "private"
    }
}

Next, extract the _uploadURL from the response body and upload the file by making a PUT request to this URL. Aside from any headers included in the _requiredHeaders property, only a Content-Length header is required, since the necessary authorization information is already embedded in the URL. For more details, please refer to the Google Cloud Storage PUT Object documentation.

_uploadURL is a temporary URL that will expire 30 seconds after your PUT or POST request to Kinvey. While you must begin your file upload within that time limit, the upload itself may take longer.

Recommended properties

The Files section of the Kinvey Console is capable of displaying the size and mime type of your files. However, in order to display this information for your files, you will need to store two extra properties with each one: size (in bytes) and mimeType.

POST /blob/:appKey HTTP/1.1
Host: baas.kinvey.com
Content-Type: application/json
X-Kinvey-API-Version: 3
Authorization: [user credentials]

{
    "_filename": "myFilename.png",
    "_acl": { ... },
    "size": 46530,
    "mimeType": "image/png"
}

Note that the JavaScript, iOS and Android libraries automatically add these properties when uploading files. These properties will also be automatically added when uploading a file through the Console.

Specifying a Content-Type

If you plan to include a Content-Type header when uploading your file to Google Cloud Storage, you must first include its value in the X-Kinvey-Content-Type header in your request to Kinvey.

Example Kinvey request:

POST /blob/:appKey HTTP/1.1
Host: baas.kinvey.com
Content-Type: application/json
X-Kinvey-API-Version: 3
Authorization: [user credentials]
X-Kinvey-Content-Type: "image/png"

{
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata"
}
HTTP/1.1 201 OK
Content-Type: application/json

{
    "_id": <uuid>,
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata",
    "_uploadURL": <Google Cloud Storage upload URL>,
    "_expiresAt": "2013-06-18T23:07:23.394Z"
}

Example Google Cloud Storage request:

PUT <Google Cloud Storage upload URL> HTTP/1.1
Content-Length: <content length>
Content-Type: "image/png"

<contents of the file to be uploaded>

Creating publicly-readable files

By default, files uploaded to Google Cloud Storage through the Kinvey File API are private. This means that every time you wish to download a file, you must first request it via a GET request to Kinvey, which will generate a signed URL that allows download access only for a limited time.

However, using the File API, you may optionally create a publicly-readable file. The download link to this file will be a regular non-signed URL, which will not expire until you delete the file through Kinvey or change the file's status to be private again. To create a publicly-readable file, set the _public flag to true when PUTing or POSTing the file's metadata to Kinvey

POST /blob/:appKey HTTP/1.1
Host: baas.kinvey.com
Content-Type: application/json
X-Kinvey-API-Version: 3
Authorization: [user credentials]

{
    "_id": <uuid>,
    "_filename": "myFilename.png",
    "_public": true
}

Once you have uploaded or updated a file in this way, follow the normal instructions to download the file. The _downloadURL property will contain a non-signed, publicly-accessible URL, which will not expire until you delete the file through Kinvey.

If you wish to make the file private again, repeat this process, but set _public to false.

Required headers for Kinvey (/blob/:appKey) request

  • Authorization
  • X-Kinvey-API-Version: 3

Required headers for actual upload to Google Cloud Storage

  • Content-Length
  • Any headers specified in _requiredHeaders

Downloading

Downloading from Kinvey is again a two-step process. First, you must send a GET request to Kinvey in order to retrieve the Google Cloud Storage URL associated with your file, as well as any metadata you stored when creating the file. Then, you will download your file directly from Google Cloud Storage.

First, send a GET request to /blob/:appKey or /blob/:appKey/:fileId. The file API supports complex queries; for more information, please refer to the querying guide.

GET /blob/:appKey/:fileId HTTP/1.1
Host: baas.kinvey.com
X-Kinvey-API-Version: 3
Authorization: [user credentials]

The response from Kinvey will contain the file _id and _filename, any other metadata that you submitted, as well as an _downloadURL property containing a URL from which you will download your file, and if the file is not public, an _expiresAt property containing the time at which the download link will expire:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "_id": "<uuid>",
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata",
    "_downloadURL": <Google Cloud Storage download URL>,
    "_expiresAt": "2013-06-18T23:07:23.394Z"
}

Next, extract the _downloadURL from the response body and download the file by making a GET request to this URL. Since the necessary authorization information is already embedded in the URL, no authorization header is required. For more details, please refer to the Google Cloud Storage GET Object documentation.

_downloadURL is a temporary URL that will expire one hour after your GET request to Kinvey. While you must begin your file download within that time limit, the download itself may take longer.

Specifying a custom expiration time

If you require the temporary download URL to last longer (or shorter) than one hour, you may optionally specify a custom expiration time (in seconds) using the ttl_in_seconds query parameter.

GET /blob/:appKey/:fileId?ttl_in_seconds=3600 HTTP/1.1
Host: baas.kinvey.com
X-Kinvey-API-Version: 3
Authorization: [user credentials]

If you would like to upload a publicly-readable file that never expires, please refer to the creating publicly-readable files section.

Required headers for Kinvey (/blob/:appKey) request

  • Authorization
  • X-Kinvey-API-Version: 3

Deleting

In contrast to the other two operations, deleting a file using the Kinvey File API is done in a single step. In order to delete a file, simply send a DELETE request to /blob/:appKey/:fileId.

DEL /blob/:appKey/:fileId HTTP/1.1
Host: baas.kinvey.com
X-Kinvey-API-Version: 3
Authorization: [user credentials]

If the deletion operation was successful, Kinvey will respond with a 200, and the number of files that have been deleted.

HTTP/1.1 200 OK
Content-Type: application/json

{
    "count": 1
}

Required headers

  • Authorization
  • X-Kinvey-API-Version: 3

Linking files to entities in collections

Many common uses for files involve associating them with entities that reside in collections. In order to make this experience as easy as possible, entities in collections may contain KinveyFile references. These references will be automatically resolved when the entity that contains them is retrieved through the Data Store API.

Creating a KinveyFile reference

A reference to a file stored using Kinvey, or a KinveyFile reference, is simply a JSON object with a _type of KinveyFile and an _id containing the ID of a file in Kinvey (as created through the File API).

To store a KinveyFile reference, create or update an entity in any collection with such a property. For example:

PUT /appdata/:appKey/:collectionName/:id HTTP/1.1
Host: baas.kinvey.com
Authorization: [user credentials]
Content-Type: application/json

{
    "dogName": "Bob",
    "furColor": "brown with black spots",
    "pawPrintPicture": {
        "_type": "KinveyFile",
        "_id": "325620e4-93dd-4a26-9f84-8a5e62c0db11"
    }
}

Please note that only two properties are allowed in a KinveyFile reference: _type and _id. If you attempt to save any additional properties as part of the KinveyFile JSON object, these properties will be ignored by Kinvey and not saved. Any additional properties that you wish to assiciate with the file should be a part of the file itself, and set through the File API.

A KinveyFile is only a reference to a file stored on Kinvey. You are responsible for uploading the file itself using the File API. It is good practice to first upload the file, and only then create the KinveyFile reference.

Downloading using a KinveyFile reference

When querying any collection through the Data Store API, Kinvey will resolve any KinveyFile reference and provide you with a _downloadURL.

GET /appdata/:appKey/:collectionName/:id HTTP/1.1
Host: baas.kinvey.com
Authorization: [user credentials]
HTTP/1.1 200 OK
Content-Type: application/json

{
    "_id": "3f583e9f-d064-4a25-a953-6cf0a3dc2ff1",
    "_acl": {...},
    "dogName": "Bob",
    "furColor": "brown with black spots",
    "pawPrintPicture": {
        "_type": "KinveyFile",        
        "_id": "325620e4-93dd-4a26-9f84-8a5e62c0db11",
        "_filename": "bobsPawPrint.png",
        "_acl": { ... },
        "_downloadURL": <Google Cloud Storage download URL>,
        "_expiresAt": "2013-06-18T23:07:23.394Z"
    }
}

You will then need to download the file directly from Google Cloud Storage by making a GET request to the _downloadURL, as explained above.

_downloadURL is a temporary URL that will expire one hour after your GET request to Kinvey. While you must begin your file download within that time limit, the download itself may take longer.

Specifying a custom expiration time

If you require the temporary download URL to last longer (or shorter) than one hour, you may optionally specify a custom expiration time (in seconds) using the kinveyfile_ttl query parameter.

GET /appdata/:appKey/:collectionName/:id?kinveyfile_ttl=3600 HTTP/1.1
Host: baas.kinvey.com
Authorization: [user credentials]

If you would like to upload a publicly-readable file that never expires, please refer to the uploading publicly-readable files section.

Securely communicating with GCS

By default, the upload and download URLs generated by Kinvey use the http protocol to communicate with Google Cloud Storage. However, you may optionally request an https URL using the tls query parameter.

While the following example uses POST, the same query parameter can also be applied to PUT or GET requests.

POST /blob/:appKey?tls=true HTTP/1.1
Host: baas.kinvey.com
Content-Type: application/json
X-Kinvey-API-Version: 3
Authorization: [user credentials]

{
    "_filename": "myFilename.png",
    "_acl": { ... },
    "myProperty": "some metadata",
    "someOtherProperty": "some more metadata"
}

When using KinveyFiles

Secure communication is available as a query parameter when retrieving KinveyFile references through the Data Store API, as well. To use https instead of http, set the kinveyfile_tls parameter to true.

Business Logic

You may create Business Logic code which will execute before or after each File API v3 route. To do so, simply add hooks to the _blob collection just like any other data collection. For more information on Business Logic, refer to the Business Logic guide.

Please note that Business Logic is only supported for File API version 3 routes. If you attempt to access a File API version 2 route that includes Business Logic, your request will fail.

Comprehensive reference

Properties

While you may include any number of custom metadata properties when saving your file, Kinvey uses certain properties to maintain its own metadata. This section contains a comprehensive list of all special Kinvey attributes that may be part of a file.

PropertyIncluded whenShort description
_idalwaysa unique identifier
_filenamealwaysa filename
_aclalwaysthe file's Access Control List
_kmdalwaysKinvey metadata, including lmt and ect
_publicalwaysif set to true, the file is publicly accessible through an unsigned URL. Defaults to false
_downloadURLdownloadinga link to GET the file
_uploadURLuploadinga link to PUT the file
_expiresAtdownloading or uploadingthe time at which the link will expire. Will not be included if the file's _public attribute is set to true
_requiredHeadersuploadinga hash of headers and their values. If this property exists and is not empty, you must include these headers set to the correct values when uploading the file to Google Cloud Storage

Special supported properties

The following properties, while optional, will allow the Kinvey Console to display more information about the file if you specify them as part of the file metadata.

PropertyShort description
mimeTypethe mime type of the file (e.g. text/plain, image/png, etc)
sizethe size of the file, in bytes

Headers

HeaderShort description
X-Kinvey-Content-Typemust be specified when making a POST or PUT request through the Kinvey File API if you wish to include a Content-Type header when uploading your file to Google Cloud Storage

Optional query parameters

ParameterApplies toDefaultShort description
queryGETexecute a complex query
ttl_in_secondsGET3600if set, Google Cloud Storage URLs will expire after the specified amount of time
tlsPOST, PUT, GETfalseif true, Google Cloud Storage URLs will use the https protocol instead of http
kinveyfile_ttlKinveyFiles3600if set, Google Cloud Storage URLs will expire after the specified amount of time
kinveyfile_tlsKinveyFilesfalseif true, Google Cloud Storage URLs will use the https protocol instead of http
Got a question?