ipfs-shipyard / py-ipfs-http-client

A python client library for the IPFS API
MIT License
678 stars 199 forks source link

Add bytes or json with filename without saving it to a temporary file #295

Open fahadh4ilyas opened 2 years ago

fahadh4ilyas commented 2 years ago

Is there any way to add bytes or JSON data without saving it to a temporary file before adding it to IPFS? I wanna do something like

sample_dict = {
    "some_key": "some_value"
}

client.add_json(sample_dict, filename="some_data.json")

Instead, I have to do this

sample_dict = {
    "some_key": "some_value"
}

with open('some_data.json', 'w') as f:
    json.dump(sample_dict, f)

client.add('some_data.json')
aturX commented 2 years ago

@fahadh4ilyas I can do it by use a File object, when I use javascript. So I think python can use the same way.

  async function onChange(e) {
    var json = `{ "a": 1 }`;
    var file = new File([json], 'test.json', {
        type: 'application/json'
    });

    try {
      const added = await client.add(file)
      const url = `https://ipfs.infura.io/ipfs/${added.path}`
      updateFileUrl(url)
    } catch (error) {
      console.log('Error uploading file: ', error)
    }  
  }