Skip to main content

uploadBatch

Batch upload arbitrary files or JSON data using the configured decentralized storage system (IPFS with pinning by default).

Usage

Upload an array of file or JSON data to the decentralized storage system.

If uploading an array of JSON objects, the method will automatically upload any file data within the objects and replace it with a hash.

// Example, use Node.js readFileSync to upload files from disk
const files = [readFileSync("../file1.jpg"), readFileSync("../file2.jpg")];
const fileUris = await storage.uploadBatch(files);

// OR, upload JSON objects
const objects = [{ name: "JSON 1" }, { name: "JSON 2" }];
const jsonUris = await storage.uploadBatch(objects);

Configuration

data

An array of the data you want to upload.

Accepts any data type (File, object, string, etc).

By default, the data is uploaded and pinned to IPFS for you.

options (optional)

Customize the upload behaviour by passing in an options object as the second parameter.

const uris = await storage.uploadBatch(
[
{ name: "JSON 1", image: files[0] },
{ name: "JSON 2", image: files[1] },
],
{
alwaysUpload: false, // Optionally, always reupload even if the file already exists
onProgress: (progress) => {}, // Callback that gets triggered when file upload progresses
metadata: {}, // Optional metadata to associate with this upload
rewriteFileNames: undefined, // If specified, will rewrite file names to numbers for use on-chain. Useful to use with NFT contracts that map token IDs to files.
uploadWithGatewayUrl: false, // If specified, any URLs with schemes will be replaced with resolved URLs before upload
uploadWithoutDirectory: false, // If specified, will upload a single file without wrapping it in a directory
},
);

Return Value

Returns an array of the URIs of the uploaded data (ex: ["ipfs://<CID1>", "ipfs://<CID2>"]).

The URIs returned are in the same order as the data you passed in.

string;