Storage

Rendley SDK offers a flexible storage solution that allows you to combine multiple storage providers together. This approach lets you use local storage, such as IndexedDB, for immediate asset access, while integrating remote storage solutions like AWS S3 for long-term asset management.

By default, no storage solution is configured, and all assets are stored in memory.

Implementing a Custom Storage Solution

To implement your own custom storage solution, you should extend the StorageProviderBase class. Below is an example showing how to create a custom solution:

// Extend the StorageProviderBase with your custom storage implementation.
export class MyCustomStorageSolution extends StorageProviderBase {
  constructor() {
    // Define if this storage is remote or local
    super(StorageProviderTypeEnum.REMOTE);
  }
 
  async init(projectId: string): Promise<void> {
    // Initialize the connection to your storage (if needed)
  }
 
  async destroy(): Promise<void> {
    // Clean up any resources or connections
  }
 
  async storeMedia(
    storageData: StorageMediaData
  ): Promise<StorageStoreResults> {
    const { hash, data } = storageData;
 
    // Upload the asset to your server.
    // The 'hash' serves as the asset's unique identifier, and 'data' contains the asset's buffer.
  }
 
  async hasMedia(mediaHash: string): Promise<boolean> {
    // Return true if the asset exists, false otherwise.
  }
 
  async getMedia(mediaHash: string): Promise<StorageMediaData | null> {
    // Retrieve the media's buffer data using its hash.
  }
 
  async removeMedia(mediaHash: string): Promise<boolean> {
    // Remove the media and return true on success.
  }
 
  async getMediaHashList(): Promise<string[]> {
    // Return a list of all media hashes stored.
  }
 
  async sync(master: StorageProviderBase): Promise<boolean> {
    // Define how this storage solution synchronizes with others.
  }
 
  isActive(): boolean {
    // Return true if the storage server is active, false if inactive.
  }
}

You can see an example of how we've implemented AWS S3 storage using presigned URLs in our AWS S3 Example Repository (opens in a new tab).

Connecting a Storage Solution

To connect your storage solution to the SDK, you simply need to initialize the Engine with your storage providers:

import { Engine, StorageIndexedDB } from "@rendley/sdk";
 
Engine.init({
  storages: [new StorageIndexedDB(), new MyCustomStorageSolution()],
});

This configuration allows you to use multiple storage solutions simultaneously. For instance, assets will be stored and quickly accessed via IndexedDB locally, while they are also uploaded and managed remotely using your custom storage solution.

Uploading Assets

To upload an asset to the library, use the mediaData.store() function. This will trigger the upload workflow, handling the storage process according to the storage solution(s) you've configured. Additionally, you can use the restore() method to reload or recover assets when needed.

Below is an example that demonstrates how to upload a file:

import { Engine } from "@rendley/sdk";
 
// Add the media file to the library
const mediaData = await Engine.getInstance().getLibrary().addMedia(myFile);
 
// Trigger the upload process for the added media
await mediaData.store();

Available Storage Solutions

Here’s a list of the available storage providers:

SyntaxDescription
IndexedDBBuilt-in
AWS S3 with Presigned URLsGithub (opens in a new tab)

For a comprehensive list of all supported properties and methods, be sure to check out the API Reference.