如何仅使用纯 javascript 使用 js-ipfs 创建目录并将文件添加到 ipfs

Dsh*_*hiz 5 javascript ipfs js-ipfs

我在研究中能找到的唯一答案是从 PC 上的目录上传多个文件。这不是我想要做的。我正在尝试在 ipfs 中创建一个目录,然后仅使用纯 JavaScript 的 js-ipfs 将新文件添加到该目录中,通常一次一个文件。

我从概念上理解 ipfs 中的目录只是另一个文件。但我不明白如何创建该目录(文件)并将其他文件添加到其中以供以后检索,尤其是使用 js-ipfs 和纯 javascript 代码。

我隐式地不使用 node.js,因此也不使用 react、angular 或 vue。

这适用于 ipfs 上没有目录的单个文件:

<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
</head>
<script>
    document.addEventListener('DOMContentLoaded', async () => {
        const nodeId = 'ipfs-' + Math.random()
        const node = await Ipfs.create({ repo: nodeId })
        console.log("Your node: " + nodeId)
        window.node = node
        const status = node.isOnline() ? 'online' : 'offline'
        console.log(`Node status: ${status}`)
        async function addFile () {
            for await (const { cid } of node.add('Some file content!')) {
                console.log('successfully stored', cid)
                cidhash=cid.toBaseEncodedString()
                console.log(cidhash)
            }
        }
        addFile()
    })
</script>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

如何使这项工作首先创建一个目录并向其中添加一个文件,然后再将另一个文件添加到创建的目录中(伪代码-ish)?

  async function addFile () {
    for await (const { directory, filename } of node.add('/someDirectory/someFilename','Some file content!')) {
      console.log('successfully stored', directory, filename)             
      console.log(directory, filename)
    }
  }
Run Code Online (Sandbox Code Playgroud)

Dsh*_*hiz 4

翻阅了js-ipfs文档,终于找到了答案。

\n

仅创建目录:

\n
await ipfs.files.mkdir(\'/my/beautiful/directory\')\n
Run Code Online (Sandbox Code Playgroud)\n

创建目录路径并同时向其中添加文件的完整工作示例:

\n
<!DOCTYPE html>\n<html>\n<head>\n  <script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>\n</head>\n<script>\n    document.addEventListener(\'DOMContentLoaded\', async () => {\n        const nodeId = \'ipfs-\' + Math.random()\n        const node = await Ipfs.create({ repo: nodeId })\n        console.log("Your node: " + nodeId)\n        window.node = node\n        const status = node.isOnline() ? \'online\' : \'offline\'\n        console.log(`Node status: ${status}`)\n\n        //create a variable with the directory path \'/my/beautiful/directory\' \n        // and a file \'awesomesause.txt\' with the content \'ABC\' \n        var files = [{\n            path: \'/my/beautiful/directory/firstfile.txt\',\n            content: \'ABC\'\n        }]\n\n        addFile(files) //add the first file       \n\n        //update the \'files\' variable to add another file to the directory path \'/mybeautiful/directory\' in ipfs\n        files = [{\n            path: \'/my/beautiful/directory/secondfile.txt\',\n            content: \'DEF\'\n        }]\n        \n        addFile(files) //add the sectond file\n\n        //function to add the files\n        async function addFile (files) {\n            for await (const result of node.add(files)) {\n                console.log(result)\n            }\n        }\n    })\n</script>\n<body>\n</body>\n</html>\n
Run Code Online (Sandbox Code Playgroud)\n

结果:

\n
generating 2048-bit RSA keypair...\njs-ipfs_dirs_and_files.html:10 Your node: ipfs-[my random node ID]\njs-ipfs_dirs_and_files.html:13 Node status: online\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 420\nmtime: undefined\npath: "my/beautiful/directory/firstfile.txt"\nsize: 11\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 420\nmtime: undefined\npath: "my/beautiful/directory/secondfile.txt"\nsize: 11\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my/beautiful/directory"\nsize: 70\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my/beautiful/directory"\nsize: 71\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my/beautiful"\nsize: 125\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my/beautiful"\nsize: 126\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my"\nsize: 180\n__proto__: Object\njs-ipfs_dirs_and_files.html:35 \nObject\ncid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), \xe2\x80\xa6}\nmode: 493\nmtime: undefined\npath: "my"\nsize: 181\n__proto__: Object\n
Run Code Online (Sandbox Code Playgroud)\n