Open rmlearney-digicatapult opened 3 days ago
If you are creating a kubo-rpc-client
and have a background Kubo running via a separate process, you should be able to do it like this:
import { create } from 'kubo-rpc-client'
const client = create()
const result = await client.add('hello world', { cidVersion: 1 })
console.log(result)
/**
{
path: 'bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e',
cid: CID(bafkreifzjut3te2nhyekklss27nh3k72ysco7y32koao5eei66wof36n5e),
size: 11
}
*/
const textDecoder = new TextDecoder()
for await (const content of client.cat(result.cid)) {
console.log(textDecoder.decode(content)) // 'hello world'
}
If you do not have a background Kubo process running, you will get something like this:
TypeError: fetch failed
at node:internal/deps/undici/undici:12345:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Client.fetch (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/lib/http.js:67:30)
at async addAll (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/add-all.js:21:25)
at async file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/it-last/dist/src/index.js:40:30
at async KuboRPCClient.add (file:///Users/sgtpooki/code/work/ipshipyard/ipfs/service-worker-gateway/node_modules/kubo-rpc-client/dist/src/add.js:9:24) {
cause: Error: connect ECONNREFUSED 127.0.0.1:5001
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1595:16) {
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 5001
}
}
If you need to manage your kubo instance with javascript, check out https://github.com/ipfs/js-ipfsd-ctl
Thanks @SgtPooki but your example is for posting content and recieving the CID.
How do I then programmatically retrieve that original content given the CID when using the RPC client?
@rmlearney-digicatapult there is an example of getting the content there. Maybe you looked before my edit?
Basically, you get the content with .cat or .get. Both return an async iterable.
If your question still isn't answered, I'm not sure I understand what you're trying to do.
My apologies @SgtPooki I must have commented before your edit.
Thanks so much!
Would it be possible to add this simple example to the docs for future users?
Would it be possible to add this simple example to the docs for future users?
Sure. I'll mark this as a doc enhancement issue
Have looked for examples and code snippets but can't figure out how to programmatically retrieve data from IPFS using
.get
methods. Have had to revert to HTTPfetch
withnode-fetch
oraxios
instead.E.g.
These methods would seem to be in the RPC but am I just using them incorrectly?