Object Storage

Setup

Here is how to use the Object Storage service of the Crewdle SDK. Each user wanting to join must open a bucket with the same name.

import { PayloadAction, StorageEventType } from '@crewdle/web-sdk-types';

const objectStore = await cluster.openObjectStoreBucket('objectStore');

You can now either subscribe to the object store to allow you to catch events being fired.

objectStore.subscribe(({ event, payload}) => {
  switch (event) {
    // Fired when a new file is written or updated
    case StorageEventType.FileWrite:
      console.log('FileWrite', payload.file);
      break;
    // Fired when a file is deleted
    case StorageEventType.FileDelete:
      console.log('FileDelete', payload.pathName);
      break;
    // Fired when a file is moved or renamed
    case StorageEventType.FileMove:
      console.log('FileMove', payload.pathName, payload.oldPathName);
      break;
    // Fired when a new folder is created
    case StorageEventType.FolderCreate:
      console.log('FolderCreate', payload.folder);
      break;
    // Fired when a folder is deleted
    case StorageEventType.FolderDelete:
      console.log('FolderDelete', payload.pathName);
      break;
    // Fired when a folder is moved or renamed
    case StorageEventType.FolderMove:
      console.log('FolderMove', payload.pathName, payload.oldPathName);
      break;
  }
});

Or publish actions to be performed on the object store.

let file = new File(['Writing', 'a', 'file'], 'crewdleFile');
const path = '/';
const newPath = '/newPath';

await objectStore.publish({ action: PayloadAction.File, file, path });
await objectStore.publish({ action: PayloadAction.Folder, path: `${path}crewdle-folder`});
await objectStore.publish({ action: PayloadAction.Move, path, newPath });

If you need to delete an element from the object store, you can simply unpublish it.

await objectStore.unpublish(newPath);

You can also get a list of files and folders available in the bucket. The search can be recursive which means you will get a folder and all its subfolders along with the files or not recursive which will only give you the files and folders of the specified folder.

await objectStore.list(newPath);
await objectStore.list(path, true);

file = await objectStore.get(newPath);

When you no longer need to interact with the object store, you can either unsubscribe or close it altogether.

objectStore.unsubscribe();

objectStore.close();

Additional Resources

See the Getting Started section for more information about initiating the SDK and a cluster.

To see a full example of our Object Store being used, checkout our Object Store sample. For more information on the services used in this page, head to our SDK reference