S.Tushinov
S.Tushinov

Reputation: 648

Is it possible to upload a string/js object without having it stored in a file? - IPFS

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);
    });
}

but I get enter image description here

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

Answers (2)

Miguel
Miguel

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

wp78de
wp78de

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

Related Questions