Last active
April 19, 2022 15:03
-
-
Save alanshaw/d3e7fdaba9bca37b36333cccfa8018c2 to your computer and use it in GitHub Desktop.
Store to NFT.Storage using a custom blockstore path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NFTStorage } from 'nft.storage' | |
import { getFilesFromPath } from 'files-from-path' | |
import { FsBlockStore as Blockstore } from 'ipfs-car/blockstore/fs' | |
import Path from 'path' | |
const token = 'YOUR_API_TOKEN' | |
// /////////////////////////////////////////////////////////////////////////// | |
// Save DAG to a custom location by changing this path... | |
const bsPath = './blockstore' | |
// /////////////////////////////////////////////////////////////////////////// | |
async function main () { | |
const path = process.argv.slice(2) | |
const files = await getFilesFromPath(path) | |
const storage = new NFTStorage({ token }) | |
const blockstore = new Blockstore() | |
blockstore.path = Path.resolve(bsPath) | |
console.log(`storing DAG in blockstore: ${blockstore.path}`) | |
try { | |
console.log(`encoding ${files.length} to DAG...`) | |
const { cid, car } = await NFTStorage.encodeDirectory(files, { blockstore }) | |
let storedSize = 0 | |
const totalSize = files.reduce((n, f) => n + f.size, 0) | |
console.log('storing data...') | |
await storage.storeCar(car, { | |
onStoredChunk: size => { | |
storedSize += size | |
console.log(`stored ${storedSize}/${totalSize} bytes`) | |
} | |
}) | |
console.log(`https://nftstorage.link/ipfs/${cid}`) | |
} finally { | |
// clean up the DAG | |
await blockstore.close() | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment