rust-ipfs-api
rust-ipfs-api copied to clipboard
How can I add/retrieve a raw buffer to IPFS?
Just wondering if that usage is implemented. Thanks :)
When you say add a raw buffer, do you mean adding it as a file or creating a block?
I don't know what the best option would be. I just want to upload and retrieve raw data from a decentralized store. Suggestion?
I added an API to add and retrieve blocks to the new version. Let me know if there are any problems with it.
TODO: Write docs for the block api
Wow, well, thank you very much. Sadly, I didn't manage to use it. Am I doing something wrong? I've started IPFS with ipfs daemon
. Then the following code:
let api = IpfsApi::new("127.0.0.1", 5001);
let hash = api.block_put("Hello world".as_bytes()).unwrap();
assert_eq!(hash, "QmV8cfu6n4NT5xRr2AHdKxFMTZEJrA44qgrBCr739BN9Wb");
Results in the following error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Json(Error("missing field `Key`", line: 1, column: 42)), url: None }', libcore/result.rs:945:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
If I replace the URL by "gateway.ipfs.io", I instead get:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { kind: Json(Error("expected value", line: 1, column: 1)), url: None }', libcore/result.rs:945:5
note: Run with `RUST_BACKTRACE=1` for a backtrace.
Changing it to "ipfs.io" or "gateway.ipfs.io" results in "Connection error". The other test also doesn't work for similar reasons. Chances are this is some misunderstanding of my part about how IPFS works, but I'm not sure what it is, given that I'm used the same code on your tests, so it should work. Perhaps I'm opening IPFS wrongly, and also using the gateways incorrectly?
As you mentioned, that code is part of the tests. When I run the code on my machine, it passes successfully.
You can try to run IPFS from the command line with the ipfs block put
and ipfs block get
commands in order to see if your IPFS daemon is running correctly.
gateway.ipfs.io does not let you create blocks. It only allows you to download them. To connect to the public gateway, try connecting with IpfsApi::new("gateway.ipfs.io", 80);