GridFS

From DreamFactory
Jump to: navigation, search

GridFS is a specification within MongoDB that allows for storing and retrieving files that exceed the BSON-document size limit of 16 MB. DreamFactory's GridFS service uses a MongoDB connection to create a Remote File Service, so you can interact with as you would with any other remote file service, such as AWS S3 or Azure Blob Storage.

Service Config

Even though it’s a file storage service, you'll notice that the service config tab looks very similar to a MongoDB database service. On the Config tab, enter in any MongoDB specific connection parameters.

GridFS Service Config Example

The database you select in the config will be the database where the two GridFS collections are located. These will be by default, fs.files and fs.chunks. Leaving the Bucket field empty will result in the default collections being created, however, you can choose another name for the collection to replace the "fs" portion of the collection(s) if you wish. This will allow for multiple GridFS collections on the same database if you have multiple services running.

Two GridFS Collections in a Single Database

Using GridFS

GridFS is primarily used for blog storage and is set up for streaming. However, the DreamFactory implementation of GridFS includes the ability to add folders and files within folders if you so choose. A simple GET call to your service endpoint will retrieve the top-level root contents. Keep in mind that you can also use the Files tab in DreamFactory Admin App to quickly navigate your GridFS service.

GET http://192.168.10.10/api/v2/test_gridfs
GET call root level

Making a call to the same endpoint with the parameter "full_tree" as true will result in every file and virtual folder in the collection:

GET http://192.168.10.10/api/v2/test_gridfs?full_tree=true
GET call root level

Notice that you'll find there is now an mp4 movie stored within our folder. As a reminder, you do not have to use virtual folders with GridFS.

Retrieving Blobs

With the GridFS service, a GET call to a specific file will result in the streaming of that data. For example, if we wanted to retrieve the movie data of our sample file, we would simply make a GET call to the endpoint associated with the file:

GET http://192.168.10.10/api/v2/test_gridfs/SampleVideo_1280x720_5mb.mp4

Whatever blob data is in GridFS will stream to the caller. Content-disposition is automatically resolved as well as type is identified if possible. In this example, we are streaming a movie through Postman:

GET stream movie

Creating Directories

To create a virtual directory in DreamFactory's GridFS service, simply POST to a new endpoint with the folder's name and a trailing slash "/".

POST http://192.168.10.10/api/v2/test_gridfs/myVirtualDirectory/
{
    "name": "myVirtualDirectory",
    "path": "myVirtualDirectory/"
}

Creating Files