Storage - TypeScript
The ThirdwebSDK
class includes storage
out of the box, available by using sdk.storage
:
sdk.storage.download
sdk.storage.downloadJSON
sdk.storage.resolveScheme
sdk.storage.upload
sdk.storage.uploadBatch
The @thirdweb-dev/storage
Package
Storage is also available as it’s own package, separate from the @thirdweb-dev/sdk
package.
This is useful if you want to use storage without the rest of the SDK.
- npm
- Yarn
- pnpm
npm install @thirdweb-dev/storage
yarn add @thirdweb-dev/storage
pnpm add @thirdweb-dev/storage
Once installed, import and instantiate the ThirdwebStorage
class:
import { ThirdwebStorage } from "@thirdweb-dev/storage";
const storage = new ThirdwebStorage({
secretKey: "YOUR_SECRET_KEY", // You can get one from dashboard settings
});
Configuration
Customize the behavior of the ThirdwebStorage
class by passing in a custom ThirdwebStorageConfig
object.
gatewayUrls
Provide your own IPFS gateway URLs to use to retrieve files from IPFS.
When calling download
or downloadJSON
,
the SDK will try to download the file from the first gateway URL in the list. If that fails, it will try the next URL in the list, and so on.
const storage = new ThirdwebStorage({
gatewayUrls: ["my-own-gateway.com", "some-other-gateway.com"],
});
The default value provides three gateways for IPFS:
{
"ipfs://": [
"https://gateway.ipfscdn.io/ipfs/",
"https://cloudflare-ipfs.com/ipfs/",
"https://ipfs.io/ipfs/",
],
}
downloader
Provide your own implementation of how to handle downloading from all schemes specified in the gatewayUrls
configuration.
// Can instantiate the downloader with the default gateway URLs
const downloader = new StorageDownloader();
const storage = new ThirdwebStorage({ downloader });
uploader
// Instantiate the uploader with default configuration
const uploader = new StorageUploader();
const storage = new ThirdwebStorage({ uploader });
Optionally, provide configuration:
const options = {
// Upload objects with resolvable URLs
uploadWithGatewayUrl: true,
};
const uploader = new StorageUploader(options);
const storage = new ThirdwebStorage({ uploader });