Skip to content

Instantly share code, notes, and snippets.

@alanshaw
Last active April 19, 2022 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanshaw/d3e7fdaba9bca37b36333cccfa8018c2 to your computer and use it in GitHub Desktop.
Save alanshaw/d3e7fdaba9bca37b36333cccfa8018c2 to your computer and use it in GitHub Desktop.
Store to NFT.Storage using a custom blockstore path
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