如何使用仅具有纯 javascript 的 js-ipfs 创建目录并将文件添加到 ipfs
How to create a directory and add files to ipfs using js-ipfs with pure javascript only
我在研究中能找到的唯一答案是从 PC 上的目录上传多个文件。那不是我想要做的。我正在尝试在 ipfs 中创建一个目录,然后使用 js-ipfs 将新文件添加到该目录,仅使用 javascript,通常一次一个文件。
我从概念上理解 ipfs 中的目录只是另一个文件。但我不明白如何创建该目录(文件)并将其他文件添加到其中以供以后检索,尤其是使用 js-ipfs 和纯 javascript 代码。
我隐含地没有使用 node.js,因此既没有反应,也没有 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>
如何创建一个目录并首先向其中添加一个文件,然后再向创建的目录添加另一个文件(伪代码)?
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)
}
}
看完js-ipfs documentation,终于找到答案了
只创建一个目录:
await ipfs.files.mkdir('/my/beautiful/directory')
创建目录路径并同时向其添加文件的完整工作示例:
<!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}`)
//create a variable with the directory path '/my/beautiful/directory'
// and a file 'awesomesause.txt' with the content 'ABC'
var files = [{
path: '/my/beautiful/directory/firstfile.txt',
content: 'ABC'
}]
addFile(files) //add the first file
//update the 'files' variable to add another file to the directory path '/mybeautiful/directory' in ipfs
files = [{
path: '/my/beautiful/directory/secondfile.txt',
content: 'DEF'
}]
addFile(files) //add the sectond file
//function to add the files
async function addFile (files) {
for await (const result of node.add(files)) {
console.log(result)
}
}
})
</script>
<body>
</body>
</html>
结果:
generating 2048-bit RSA keypair...
js-ipfs_dirs_and_files.html:10 Your node: ipfs-[my random node ID]
js-ipfs_dirs_and_files.html:13 Node status: online
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 420
mtime: undefined
path: "my/beautiful/directory/firstfile.txt"
size: 11
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 420
mtime: undefined
path: "my/beautiful/directory/secondfile.txt"
size: 11
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful/directory"
size: 70
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful/directory"
size: 71
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful"
size: 125
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful"
size: 126
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my"
size: 180
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my"
size: 181
__proto__: Object
我在研究中能找到的唯一答案是从 PC 上的目录上传多个文件。那不是我想要做的。我正在尝试在 ipfs 中创建一个目录,然后使用 js-ipfs 将新文件添加到该目录,仅使用 javascript,通常一次一个文件。
我从概念上理解 ipfs 中的目录只是另一个文件。但我不明白如何创建该目录(文件)并将其他文件添加到其中以供以后检索,尤其是使用 js-ipfs 和纯 javascript 代码。
我隐含地没有使用 node.js,因此既没有反应,也没有 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>
如何创建一个目录并首先向其中添加一个文件,然后再向创建的目录添加另一个文件(伪代码)?
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)
}
}
看完js-ipfs documentation,终于找到答案了
只创建一个目录:
await ipfs.files.mkdir('/my/beautiful/directory')
创建目录路径并同时向其添加文件的完整工作示例:
<!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}`)
//create a variable with the directory path '/my/beautiful/directory'
// and a file 'awesomesause.txt' with the content 'ABC'
var files = [{
path: '/my/beautiful/directory/firstfile.txt',
content: 'ABC'
}]
addFile(files) //add the first file
//update the 'files' variable to add another file to the directory path '/mybeautiful/directory' in ipfs
files = [{
path: '/my/beautiful/directory/secondfile.txt',
content: 'DEF'
}]
addFile(files) //add the sectond file
//function to add the files
async function addFile (files) {
for await (const result of node.add(files)) {
console.log(result)
}
}
})
</script>
<body>
</body>
</html>
结果:
generating 2048-bit RSA keypair...
js-ipfs_dirs_and_files.html:10 Your node: ipfs-[my random node ID]
js-ipfs_dirs_and_files.html:13 Node status: online
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 420
mtime: undefined
path: "my/beautiful/directory/firstfile.txt"
size: 11
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 420
mtime: undefined
path: "my/beautiful/directory/secondfile.txt"
size: 11
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful/directory"
size: 70
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful/directory"
size: 71
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful"
size: 125
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my/beautiful"
size: 126
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my"
size: 180
__proto__: Object
js-ipfs_dirs_and_files.html:35
Object
cid: i {version: 0, codec: "dag-pb", multihash: Uint8Array(34), multibaseName: "base58btc", _buffer: Uint8Array(34), …}
mode: 493
mtime: undefined
path: "my"
size: 181
__proto__: Object