Overview
Quick Start
Section titled “Quick Start”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 hereconst 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');Classes
Section titled “Classes”AssetManager
Section titled “AssetManager”Defined in: packages/canisters/src/assets/index.ts:237
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AssetManager(
config):AssetManager
Defined in: packages/canisters/src/assets/index.ts:247
Create assets canister manager instance
Parameters
Section titled “Parameters”config
Section titled “config”Additional configuration options, canister id is required
Returns
Section titled “Returns”Methods
Section titled “Methods”batch()
Section titled “batch()”batch():
AssetManagerBatch
Defined in: packages/canisters/src/assets/index.ts:426
Create a batch assets operations instance, commit multiple operations in a single request
Returns
Section titled “Returns”AssetManagerBatch
clear()
Section titled “clear()”clear():
Promise<void>
Defined in: packages/canisters/src/assets/index.ts:391
Delete all files from assets canister
Returns
Section titled “Returns”Promise<void>
delete()
Section titled “delete()”delete(
key):Promise<void>
Defined in: packages/canisters/src/assets/index.ts:384
Delete file from assets canister
Parameters
Section titled “Parameters”string
The path to the file on the assets canister e.g. /folder/to/my_file.txt
Returns
Section titled “Returns”Promise<void>
get(
key,acceptEncodings?):Promise<Asset>
Defined in: packages/canisters/src/assets/index.ts:400
Get asset instance from assets canister
Parameters
Section titled “Parameters”string
The path to the file on the assets canister e.g. /folder/to/my_file.txt
acceptEncodings?
Section titled “acceptEncodings?”The accepted content encodings, defaults to [‘identity’]
Returns
Section titled “Returns”Promise<Asset>
list()
Section titled “list()”list():
Promise<object[]>
Defined in: packages/canisters/src/assets/index.ts:293
Get list of all files in assets canister.
Returns
Section titled “Returns”Promise<object[]>
All files in asset canister
store()
Section titled “store()”store(…
args):Promise<string>
Defined in: packages/canisters/src/assets/index.ts:342
Store data on assets canister
Parameters
Section titled “Parameters”Arguments with either a file, blob, path, bytes or custom Readable implementation
Returns
Section titled “Returns”Promise<string>
toReadable()
Section titled “toReadable()”
statictoReadable(…args):Promise<Readable>
Defined in: packages/canisters/src/assets/index.ts:260
Create readable from store arguments
Parameters
Section titled “Parameters”Arguments with either a file, blob, path, bytes or custom Readable implementation
Returns
Section titled “Returns”Promise<Readable>
Interfaces
Section titled “Interfaces”AssetManagerConfig
Section titled “AssetManagerConfig”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.
Extends
Section titled “Extends”ActorConfig
Properties
Section titled “Properties”agent?
Section titled “agent?”
optionalagent: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.
Inherited from
Section titled “Inherited from”ActorConfig.agent
blsVerify?
Section titled “blsVerify?”
optionalblsVerify: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
Inherited from
Section titled “Inherited from”ActorConfig.blsVerify
canisterId
Section titled “canisterId”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.
Inherited from
Section titled “Inherited from”ActorConfig.canisterId
concurrency?
Section titled “concurrency?”
optionalconcurrency:number
Defined in: packages/canisters/src/assets/index.ts:224
Max number of concurrent requests to the Internet Computer
Default
Section titled “Default”16effectiveCanisterId?
Section titled “effectiveCanisterId?”
optionaleffectiveCanisterId:Principal
Defined in: node_modules/@icp-sdk/core/lib/esm/agent/actor.d.ts:33
The effective canister ID.
Inherited from
Section titled “Inherited from”ActorConfig.effectiveCanisterId
maxChunkSize?
Section titled “maxChunkSize?”
optionalmaxChunkSize: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
Default
Section titled “Default”1900000maxSingleFileSize?
Section titled “maxSingleFileSize?”
optionalmaxSingleFileSize:number
Defined in: packages/canisters/src/assets/index.ts:229
Max file size in bytes that the asset manager shouldn’t chunk
Default
Section titled “Default”1900000pollingOptions?
Section titled “pollingOptions?”
optionalpollingOptions: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.
Inherited from
Section titled “Inherited from”ActorConfig.pollingOptions
queryStrategy?
Section titled “queryStrategy?”
optionalqueryStrategy: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.
Default
Section titled “Default”'query'Inherited from
Section titled “Inherited from”ActorConfig.queryStrategy
Methods
Section titled “Methods”callTransform()?
Section titled “callTransform()?”
optionalcallTransform(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.
Parameters
Section titled “Parameters”methodName
Section titled “methodName”string
unknown[]
callConfig
Section titled “callConfig”CallConfig
Returns
Section titled “Returns”void | Partial<CallConfig>
Inherited from
Section titled “Inherited from”ActorConfig.callTransform
queryTransform()?
Section titled “queryTransform()?”
optionalqueryTransform(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.
Parameters
Section titled “Parameters”methodName
Section titled “methodName”string
unknown[]
callConfig
Section titled “callConfig”CallConfig
Returns
Section titled “Returns”void | Partial<CallConfig>
Inherited from
Section titled “Inherited from”ActorConfig.queryTransform
CommitBatchArgs
Section titled “CommitBatchArgs”Defined in: packages/canisters/src/assets/index.ts:210
Arguments to commit batch in asset manager
Properties
Section titled “Properties”onProgress()?
Section titled “onProgress()?”
optionalonProgress: (progress) =>void
Defined in: packages/canisters/src/assets/index.ts:211
Parameters
Section titled “Parameters”progress
Section titled “progress”Returns
Section titled “Returns”void
Progress
Section titled “Progress”Defined in: packages/canisters/src/assets/index.ts:135
Upload progress in bytes
Properties
Section titled “Properties”current
Section titled “current”current:
number
Defined in: packages/canisters/src/assets/index.ts:136
total:
number
Defined in: packages/canisters/src/assets/index.ts:137
StoreConfig
Section titled “StoreConfig”Defined in: packages/canisters/src/assets/index.ts:143
Configuration that can be passed to set and override defaults and add progress callback
Properties
Section titled “Properties”contentEncoding?
Section titled “contentEncoding?”
optionalcontentEncoding:ContentEncoding
Defined in: packages/canisters/src/assets/index.ts:168
Content encoding
Default
Section titled “Default”'identity'contentType?
Section titled “contentType?”
optionalcontentType:string
Defined in: packages/canisters/src/assets/index.ts:158
File content type
Default
Section titled “Default”File/Blob object type or type from file name extensionfileName?
Section titled “fileName?”
optionalfileName:string
Defined in: packages/canisters/src/assets/index.ts:148
File name
Default
Section titled “Default”File object name or name in file pathheaders?
Section titled “headers?”
optionalheaders: [string,string][]
Defined in: packages/canisters/src/assets/index.ts:163
Custom headers to be sent with the asset
Default
Section titled “Default”[]onProgress()?
Section titled “onProgress()?”
optionalonProgress: (progress) =>void
Defined in: packages/canisters/src/assets/index.ts:176
Callback method to get upload progress in bytes (current / total)
Parameters
Section titled “Parameters”progress
Section titled “progress”Returns
Section titled “Returns”void
optionalpath:string
Defined in: packages/canisters/src/assets/index.ts:153
File path that file will be uploaded to
Default
Section titled “Default”'/'sha256?
Section titled “sha256?”
optionalsha256:Uint8Array<ArrayBufferLike>
Defined in: packages/canisters/src/assets/index.ts:172
File hash generation will be skipped if hash is provided
Type Aliases
Section titled “Type Aliases”ContentEncoding
Section titled “ContentEncoding”ContentEncoding =
"identity"|"gzip"|"compress"|"deflate"|"br"
Defined in: packages/canisters/src/assets/index.ts:125
Supported content encodings by asset canister
StoreArgs
Section titled “StoreArgs”StoreArgs =
StoreReadableArgs|StoreFileArgs|StoreBlobArgs|StorePathArgs|StoreBytesArgs
Defined in: packages/canisters/src/assets/index.ts:200
Arguments to store an asset in asset manager
StoreBlobArgs
Section titled “StoreBlobArgs”StoreBlobArgs = [
Blob,Omit<StoreConfig,"fileName"> &Required<Pick<StoreConfig,"fileName">>]
Defined in: packages/canisters/src/assets/index.ts:183
StoreBytesArgs
Section titled “StoreBytesArgs”StoreBytesArgs = [
Uint8Array|ArrayBuffer|number[],Omit<StoreConfig,"fileName"> &Required<Pick<StoreConfig,"fileName">>]
Defined in: packages/canisters/src/assets/index.ts:191
StoreFileArgs
Section titled “StoreFileArgs”StoreFileArgs = [
File,StoreConfig]
Defined in: packages/canisters/src/assets/index.ts:181
StorePathArgs
Section titled “StorePathArgs”StorePathArgs = [
string,StoreConfig]
Defined in: packages/canisters/src/assets/index.ts:189
StoreReadableArgs
Section titled “StoreReadableArgs”StoreReadableArgs = [
Readable,StoreConfig]
Defined in: packages/canisters/src/assets/index.ts:179