Reputation: 648
I have been playing around with js-ipfs API and wanted to ask if anyone knows wheather js-ipfs supports only uploading of files/folders? Is there a way to upload, for example a javascript object like:
{
heading:"SomeHeading",
content:"somecontent"
}
or a string like
"{heading:\"SomeHeading\", content:\"somecontent\"}"
So far I have tried:
const ipfs = window.IpfsApi('localhost', 5001, {protocol: 'https'});
const buffer = ipfs.Buffer;
async function uploadToIpfs() {
let someObject = {
heading:"SomeHeading",
content:"someContent"
};
let objectString = JSON.stringify(someObject);
let bufferedString = await buffer.from(objectString);
await ipfs.add(bufferedString, (err, resp) => {
console.log(err);
console.log(resp);
});
}
Any help with resolving this problem or just a stright up answer of wheather it is possible to just upload a js object or string would be greatly appreciated!
Upvotes: 1
Views: 1092
Reputation: 20633
I tried your code and all you need to do is change the protocol
to http
since you're on localhost.
{protocol: 'http'}
Upvotes: 1
Reputation: 18950
If I understand your question correctly, you want to upload arbitrary object content; therefore, you want an abstract-blob-storage. This can be done using ipfs-blob-store.
Make sure to read the docs but to get the idea:
var ipfsBlobStore = require('ipfs-blob-store')
var options = {
port: 5001, // default value
host: '127.0.0.1', // default value
baseDir: '/', // default value
flush: true // default value
}
var store = ipfsBlobStore(options)
var ws = store.createWriteStream({
key: 'some/path/file.txt'
})
ws.write("{heading:\"SomeHeading\", content:\"somecontent\"}")
ws.end(function() {
var rs = store.createReadStream({
key: 'some/path/file.txt'
})
rs.pipe(process.stdout)
})
Upvotes: 0