Skip to content

Overview

The AssetManager supports the (chunked) upload of File, Blob, ArrayBuffer, Uint8Array and number[].

Create an asset manager instance:

import { AssetManager } from "@icp-sdk/canisters/assets";
const assetManager = new AssetManager({
canisterId: ..., // Principal of assets canister
agent: ..., // Identity in agent must be authorized by the assets canister to make any changes
});

Select file and upload to asset canister from the browser:

const input = document.createElement('input');
input.type = 'file';
input.addEventListener('change', async e => {
const file = e.target.files[0];
const key = await assetManager.store(file);
});
input.click();

A config argument can be optionally passed as second argument in the store method. The fileName property is required when the data passed in the first argument is not a File, file path or custom Readable implementation.

List files stored in the asset canister:

const files = await assetManager.list();

Upload multiple files and delete an existing file as batch in Node.js:

import fs from 'fs';
const banana = fs.readFileSync('./banana.png');
const apple = fs.readFileSync('./apple.png');
const strawberry = fs.readFileSync('./strawberry.png');
const batch = assetManager.batch();
const keys = [
await batch.store(banana, { fileName: 'banana.png' }),
await batch.store(apple, { fileName: 'apple.png', path: '/directory/with/apples' }),
await batch.store(strawberry, { fileName: 'strawberry.png' }),
];
await batch.delete('/path/to/old/file.csv');
await batch.commit();

Read file from disk, compress with gzip and upload to asset canister in Node.js, GZIP compression is recommended for HTML and JS files:

import fs from 'fs';
const file = fs.readFileSync('./index.html');
const gzippedFile = // gzip the file here
const key = await assetManager.insert(gzippedFile, {
fileName: 'index.html',
contentEncoding: 'gzip',
});

Download image asset to blob and open in new browser tab:

const asset = await assetManager.get('/path/to/file/on/asset/canister/motoko.png');
const blob = await asset.toBlob();
const url = URL.createObjectURL(blob);
window.open(URL.createObjectURL(blob, '_blank'));

Download and write asset to path in Node.js:

const asset = await assetManager.get('/large_dataset.csv');
asset.write('/large_dataset.csv');

Defined in: packages/canisters/src/assets/index.ts:237

new AssetManager(config): AssetManager

Defined in: packages/canisters/src/assets/index.ts:247

Create assets canister manager instance

AssetManagerConfig

Additional configuration options, canister id is required

AssetManager

batch(): AssetManagerBatch

Defined in: packages/canisters/src/assets/index.ts:426

Create a batch assets operations instance, commit multiple operations in a single request

AssetManagerBatch

clear(): Promise<void>

Defined in: packages/canisters/src/assets/index.ts:391

Delete all files from assets canister

Promise<void>

delete(key): Promise<void>

Defined in: packages/canisters/src/assets/index.ts:384

Delete file from assets canister

string

The path to the file on the assets canister e.g. /folder/to/my_file.txt

Promise<void>

get(key, acceptEncodings?): Promise<Asset>

Defined in: packages/canisters/src/assets/index.ts:400

Get asset instance from assets canister

string

The path to the file on the assets canister e.g. /folder/to/my_file.txt

ContentEncoding[]

The accepted content encodings, defaults to [‘identity’]

Promise<Asset>

list(): Promise<object[]>

Defined in: packages/canisters/src/assets/index.ts:293

Get list of all files in assets canister.

Promise<object[]>

All files in asset canister

store(…args): Promise<string>

Defined in: packages/canisters/src/assets/index.ts:342

Store data on assets canister

StoreArgs

Arguments with either a file, blob, path, bytes or custom Readable implementation

Promise<string>

static toReadable(…args): Promise<Readable>

Defined in: packages/canisters/src/assets/index.ts:260

Create readable from store arguments

StoreArgs

Arguments with either a file, blob, path, bytes or custom Readable implementation

Promise<Readable>

Defined in: packages/canisters/src/assets/index.ts:219

Configuration that can be passed to set the canister id of the assets canister to be managed, inherits actor configuration and has additional asset manager specific configuration options.

  • ActorConfig

optional agent: Agent

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:21

An agent to use in this call, otherwise the actor or call will try to discover the agent to use.

ActorConfig.agent

optional blsVerify: VerifyFunc

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:58

Polyfill for BLS Certificate verification in case wasm is not supported

ActorConfig.blsVerify

canisterId: string | Principal

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:46

The Canister ID of this Actor. This is required for an Actor.

ActorConfig.canisterId

optional concurrency: number

Defined in: packages/canisters/src/assets/index.ts:224

Max number of concurrent requests to the Internet Computer

16

optional effectiveCanisterId: Principal

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:33

The effective canister ID.

ActorConfig.effectiveCanisterId

optional maxChunkSize: number

Defined in: packages/canisters/src/assets/index.ts:234

Size of each chunk in bytes when the asset manager has to chunk a file

1900000

optional maxSingleFileSize: number

Defined in: packages/canisters/src/assets/index.ts:229

Max file size in bytes that the asset manager shouldn’t chunk

1900000

optional pollingOptions: PollingOptions

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:62

Polling options to use when making update calls. This will override the default DEFAULT_POLLING_OPTIONS.

ActorConfig.pollingOptions

optional queryStrategy: QueryStrategy

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:69

Controls how query and composite_query methods are executed.

  • 'query' (default) — standard non-replicated query call.
  • 'update' — send query methods as update calls that go through consensus.
'query'

ActorConfig.queryStrategy

optional callTransform(methodName, args, callConfig): void | Partial<CallConfig>

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:50

An override function for update calls’ CallConfig. This will be called on every calls.

string

unknown[]

CallConfig

void | Partial<CallConfig>

ActorConfig.callTransform

optional queryTransform(methodName, args, callConfig): void | Partial<CallConfig>

Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:54

An override function for query calls’ CallConfig. This will be called on every query.

string

unknown[]

CallConfig

void | Partial<CallConfig>

ActorConfig.queryTransform


Defined in: packages/canisters/src/assets/index.ts:210

Arguments to commit batch in asset manager

optional onProgress: (progress) => void

Defined in: packages/canisters/src/assets/index.ts:211

Progress

void


Defined in: packages/canisters/src/assets/index.ts:135

Upload progress in bytes

current: number

Defined in: packages/canisters/src/assets/index.ts:136

total: number

Defined in: packages/canisters/src/assets/index.ts:137


Defined in: packages/canisters/src/assets/index.ts:143

Configuration that can be passed to set and override defaults and add progress callback

optional contentEncoding: ContentEncoding

Defined in: packages/canisters/src/assets/index.ts:168

Content encoding

'identity'

optional contentType: string

Defined in: packages/canisters/src/assets/index.ts:158

File content type

File/Blob object type or type from file name extension

optional fileName: string

Defined in: packages/canisters/src/assets/index.ts:148

File name

File object name or name in file path

optional headers: [string, string][]

Defined in: packages/canisters/src/assets/index.ts:163

Custom headers to be sent with the asset

[]

optional onProgress: (progress) => void

Defined in: packages/canisters/src/assets/index.ts:176

Callback method to get upload progress in bytes (current / total)

Progress

void

optional path: string

Defined in: packages/canisters/src/assets/index.ts:153

File path that file will be uploaded to

'/'

optional sha256: Uint8Array<ArrayBufferLike>

Defined in: packages/canisters/src/assets/index.ts:172

File hash generation will be skipped if hash is provided

ContentEncoding = "identity" | "gzip" | "compress" | "deflate" | "br"

Defined in: packages/canisters/src/assets/index.ts:125

Supported content encodings by asset canister


StoreArgs = StoreReadableArgs | StoreFileArgs | StoreBlobArgs | StorePathArgs | StoreBytesArgs

Defined in: packages/canisters/src/assets/index.ts:200

Arguments to store an asset in asset manager


StoreBlobArgs = [Blob, Omit<StoreConfig, "fileName"> & Required<Pick<StoreConfig, "fileName">>]

Defined in: packages/canisters/src/assets/index.ts:183


StoreBytesArgs = [Uint8Array | ArrayBuffer | number[], Omit<StoreConfig, "fileName"> & Required<Pick<StoreConfig, "fileName">>]

Defined in: packages/canisters/src/assets/index.ts:191


StoreFileArgs = [File, StoreConfig]

Defined in: packages/canisters/src/assets/index.ts:181


StorePathArgs = [string, StoreConfig]

Defined in: packages/canisters/src/assets/index.ts:189


StoreReadableArgs = [Readable, StoreConfig]

Defined in: packages/canisters/src/assets/index.ts:179