|
1 | 1 | IPFS Data Importing
|
2 | 2 | ===================
|
3 | 3 |
|
| 4 | +> Import data into an IPFS DAG Service. |
| 5 | +
|
4 | 6 | [](http://ipn.io)
|
5 | 7 | [](http://webchat.freenode.net/?channels=%23ipfs)
|
6 | 8 | [](https://travis-ci.org/ipfs/js-ipfs-data-importing)
|
7 | 9 | 
|
8 | 10 | [](https://david-dm.org/ipfs/js-ipfs-data-importing)
|
9 | 11 | [](https://github.com/feross/standard)
|
10 | 12 |
|
11 |
| -> JavaScript implementation of the layout and chunking mechanisms used by IPFS |
| 13 | +## Example |
| 14 | + |
| 15 | +Let's create a little directory to import: |
| 16 | +```sh |
| 17 | +$ cd /tmp |
| 18 | +$ mkdir foo |
| 19 | +$ echo 'hello' > foo/bar |
| 20 | +$ echo 'warld' > foo/quux |
| 21 | +``` |
| 22 | + |
| 23 | +And write the importing logic: |
| 24 | +```js |
| 25 | +// Dependencies to create a DAG Service (where the dir will be imported into) |
| 26 | +var memStore = require('abstract-blob-store') |
| 27 | +var ipfsRepo = require('ipfs-repo') |
| 28 | +var ipfsBlocks = require('ipfs-blocks') |
| 29 | +var ipfsMerkleDag = require('ipfs-merkle-dag') |
| 30 | + |
| 31 | +var repo = new ipfsRepo('', { stores: memStore }) |
| 32 | +var blocks = new ipfsBlocks.BlockService(repo) |
| 33 | +var dag = new ipfsMerkleDag.DAGService(blocks) |
| 34 | + |
| 35 | + |
| 36 | +var ipfsData = require('ipfs-data-importing') |
| 37 | + |
| 38 | +// Import /tmp/foo |
| 39 | +ipfsData.import('/tmp/foo', dag, { |
| 40 | + recursive: true |
| 41 | +}, done) |
| 42 | + |
| 43 | +// A root DAG Node is received upon completion |
| 44 | +function done (err, rootStat) { |
| 45 | + if (err) { throw err } |
| 46 | + console.log(rootStat) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +When run, the stat of root DAG Node is outputted: |
| 51 | + |
| 52 | +``` |
| 53 | +{ Hash: <Buffer 12 20 bd e2 2b 57 3f 6f bd 7c cc 5a 11 7f 28 6c a2 9a 9f c0 90 e1 d4 16 d0 5f 42 81 ec 0c 2a 7f 7f 93>, |
| 54 | + Size: 59843, |
| 55 | + Name: 'foo' } |
| 56 | +``` |
| 57 | + |
| 58 | +## API |
| 59 | + |
| 60 | +```js |
| 61 | +var importer = require('ipfs-data-importing') |
| 62 | +``` |
| 63 | + |
| 64 | +### importer.import(target, dagService, opts, cb) |
| 65 | + |
| 66 | +`target` can be a `string`, `Buffer`, or `Stream`. When it's a string, the file |
| 67 | +or directory structure rooted on the filesystem at `target` is imported, with |
| 68 | +the hierarchy preserved. If a Buffer or Stream, a single DAG node will be |
| 69 | +imported representing the buffer or stream's contents. |
| 70 | + |
| 71 | +Uses the [DAG Service](https://github.com/vijayee/js-ipfs-merkle-dag/) instance |
| 72 | +`dagService`. Accepts the following `opts`: |
| 73 | + |
| 74 | +- `recursive`: whether to recurse into directories. Defaults to `false`. |
| 75 | + |
| 76 | +Calls the callback `cb(err, stat)` on completion or error, where `stat` is an |
| 77 | +object with the `Hash`, `Size`, and `Name` of the root |
| 78 | +[`DAGNode`](https://github.com/vijayee/js-ipfs-merkle-dag/). |
| 79 | + |
| 80 | +## install |
| 81 | + |
| 82 | +With [npm](https://npmjs.org/) installed, run |
| 83 | + |
| 84 | +``` |
| 85 | +$ npm install ipfs-data-importing |
| 86 | +``` |
| 87 | + |
| 88 | +## license |
12 | 89 |
|
| 90 | +ISC |
0 commit comments