Storage

SveltePak includes support for storage. You can use the storage module to integrate storage services and track custom events.

It can be used both on server-side and client-side, since it basically calls an API endpoint.

Unlike the other modules, we’ve designed the storage as a simple useStorage() that allows you to change the storage provider without changing your code.

This decision was made to allow you to easily switch between storage providers, depending on your needs and context. It’s not uncommon to use different storage providers for different types of files.

Providers

Local Storage

The default storage provider is the local storage. It’s a simple file system storage that is used for development and testing.

import useStorage from "$modules/storage";
const storage = useStorage("local");

// Upload a file
const file = new File(["hello"], "hello.txt", { type: "text/plain" });
const { url } = await storage.upload(file);

// Download a file
const blob = await storage.download(url);

// Delete a file
await storage.delete(url);

// uploadFromUrl
const { url } = await storage.uploadFromUrl("https://example.com/hello.txt");

S3

Coming soon.

Helper Functions

The storage module also includes helper function that returns the URL of the file.

import {storage} from "$modules/storage";

<img src={storage("avatars", user.avatar)} />