Create a file

Call the .submit() function to create a store object and save a file.

Returns

The function returns the store object that was just created with a couple of extra attributes.

Usage

import { store } from 'aleph-js'

await store.submit(address, {file_hash: null, storage_engine: 'storage', account: null})

Required parameters

ParameterDescription

address - string

Most likely your address

Other parameters

In addition to the required parameters above, an object of optional parameters can be passed following the address like so:

store.submit(address,{api_server: "https://api2.aleph.im"})

  • file_hash - string - REQUIRED if no fileobject Default: None file_hash is ignored if a fileobject is passed in. Use this if you already have uploaded the file to storage or ipfs yourself.

  • fileobject - bytes - REQUIRED if no file_hash Default: None

    The file you want to store.

    This takes precedence over the file_hash

  • api_server - string Default: "https://api1.aleph.im Select an API server accepting files.

    ex: https://api1.aleph.im and "https://api2.aleph.im" are two available API servers accepting files.

  • chain - string Default: null The chain used by the sender's account. Value can be NULS2, ETH, DOT, CSDK, SOL, AVAX.

  • channel - string Default: null Channel of the message. Ideally, an application would decide and use one channel.

  • storage_engine - string Default: "storage" Storage engine to use. Use "storage" for aleph.im built-in storage or "ipfs" for an ipfs compatible storage. Possible values: storage, ipfs

  • account - Account

    Default: null

    Account to use for signing.

    ℹī¸ Needed if you want to send the message. Without it, it is a dry run.

  • extra_fields - object Default: {}

    Any custom fields to save alongside the file in the message item_content.

If you've already stored a file in an IPFS storage, you can still create a file using the .submit() to "pin" it, by passing the: { ..., storage_engine: "ipfs" , file_hash: _ <_ipfs item hash> , ... }

See how to push a file in IPFS file storage.

Aleph file storage example

REQUEST
// Creating a file we need to store

// Tthis file object can also be obtained 
// from an upload form input (if you don't want to create it 
// programmatically).

var myfile = new File(
  ["This is just a test."],
  "test.txt",
  {type: "text/plain"})
        
await store.submit(
  account.address,
  {
    'fileobject': myfile,
     'account': account,
     'channel': 'TEST',
     'api_server': 'https://api2.aleph.im'
  }
)
RESPONSE
{
  "chain": "NULS2",
  "channel": "TEST",
  "sender": "NULSd6HgcLR5Yjc7yyMiteQZxTpuB6NYRiqWf",
  "type": "STORE",
  "time": 1582562109.316,
  "item_type": "inline",
  "item_content": "{\"address\":\"NULSd6HgcLR5Yjc7yyMiteQZxTpuB6NYRiqWf\",\"item_type\":\"storage\",\"item_hash\":\"11dfc1e6953dac4bd02d8faa06878f51eea3421fa58d7148e808d425cff2a921\",\"time\":1582562109.316}",
  "item_hash": "fde8effa834d12ce127e7f82ac317639505af36b34b3b40a2d108b9e1bfb3b2b",
  "signature": "HLzL+XlkNkCOo8UReVo7Qh3mMzVn5/imD9J5xbzBejS4b9BjKDTiGfcnhJQPGd47lcmPg3jtBcVOTPNSPVwb3Ws=",
  "content": {
    "address": "NULSd6HgcLR5Yjc7yyMiteQZxTpuB6NYRiqWf",
    "item_type": "storage",
    "item_hash": "11dfc1e6953dac4bd02d8faa06878f51eea3421fa58d7148e808d425cff2a921",
    "time": 1582562109.316
  }
}

In the response, content.item_hash can be used to retrieve the stored file via a direct URL as follow:

https://<API SERVER URL>/api/v0/storage/raw/<FILE ITEM HASH HERE>

Api server can be any api server accepting files.

IPFS file storage example

var msg = await store.submit(
  account.address,
  {
    'fileobject': myfile,
    'account': account,
    'channel': 'TEST',
    'storage_engine': 'ipfs',
    'api_server': 'https://api2.aleph.im'
  }
)
msg.content.item_hash
// => QmQkv43jguT5HLC8TPbYJi2iEmr4MgLgu4nmBoR4zjYb3L

To retrieve file via direct url:

https://ipfs.io/ipfs/QmQkv43jguT5HLC8TPbYJi2iEmr4MgLgu4nmBoR4zjYb3L

https://api2.aleph.im/api/v0/storage/raw/QmQkv43jguT5HLC8TPbYJi2iEmr4MgLgu4nmBoR4zjYb3L

Files are available at two URLs in this case:

  • The IPFS URL: https://ipfs.io/ipfs/<FILE ITEM HASH HERE>

  • The API server URL: https://<FILE API_SERVER>/api/v0/storage/raw/<FILE ITEM HASH HERE>

Last updated