Skip to content
This repository has been archived by the owner on Mar 14, 2019. It is now read-only.

How to: Customize the file key folders in the S3 Bucket

Philipp edited this page Mar 1, 2015 · 1 revision

You can add fileKeyMaker option on your S3 store, and set it to a function that returns the file key given a fileObj (FS.File instance). For now, you actually need to add some code to your function checking to see if a file key is already assigned. Hopefully we can handle that internally eventually.

Here's an example that would generate the same fileKey as default:

var imageStore = new FS.Store.S3("images", {
  fileKeyMaker: function (fileObj) {
    // Lookup the copy
    var store = fileObj && fileObj._getInfo(name);
    // If the store and key is found return the key
    if (store && store.key) return store.key;

    // TO CUSTOMIZE, REPLACE CODE AFTER THIS POINT

    var filename = fileObj.name();
    var filenameInStore = fileObj.name({store: name});

    // If no store key found we resolve / generate a key
    return fileObj.collectionName + '/' + fileObj._id + '-' + (filenameInStore || filename);
  }
});

The function receives only the fileObj so if you need the userId, you'll need to set it on every file when uploading in order to use it in your path.

S3 automatically creates the necessary folders based on the slashes in the string you return.