ipfs / js-ipfs

IPFS implementation in JavaScript
https://js.ipfs.tech
Other
7.44k stars 1.25k forks source link

can't upload file via ipfs.add(file) - can't get hash in response #2975

Closed zhgromov closed 4 years ago

zhgromov commented 4 years ago

Hello, guys. Few month ago I add ipfs for upload files, all worked correctly, but a week ago I have problem and can’t resolve it. I try to send file to IPFS and get hash of file. (react)

like this. connect ipfs :

  ipfs = await Ipfs.create({
      config: {
        Bootstrap: [
          '/dns4/ams-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd',
          '/dns4/lon-1.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3',
          '/dns4/sfo-3.bootstrap.libp2p.io/tcp/443/wss/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM',
          '/dns4/node0.preload.ipfs.io/tcp/443/wss/ipfs/QmZMxNdpMkewiVZLMRxaNxUeZpDUb34pWjZ1kZvsd16Zic',
          '/dns4/node1.preload.ipfs.io/tcp/443/wss/ipfs/Qmbut9Ywz9YEDrz8ySBSgWyJk41Uvm2QJPhwDJzJyGFsD6'
        ]
      }
    });

    console.timeEnd('IPFS Started');

and try to send file

  const {ipfs, ipfsInitError} = useIpfs({commands: ['id']});
  const addToIpfs = async (file) => {
    const hashOfFile = await ipfs.add(file);

    return hashOfFile[0].path;
};

so, when I try to upload, I have error

Unhandled Rejection (TypeError): undefined is not an object (evaluating ‘hashOfFile[0].path’)
in hashOfFile return function
AsyncGenerator {_invoke: function, next: function, throw: function, return: function, Symbol(Symbol.asyncIterator): function}

earlier it was hash of file.

could you help me?

chelneru commented 4 years ago

It's some wierd behavior. I get the same thing.

But if I try :

for await (const file of await ipfs.add({
                path: 'hello.txt',
                content: 'Hello World 101'
            })) {
                console.log(JSON.stringify(file));

            }

This works for me and I get : {"path":"hello.txt","cid":{"codec":"dag-pb","version":0,"hash":{"type":"Buffer","data":[18,32,138,210,135,65,209,1,129,203,63,121,61,77,238,44,52,218,162,227,202,125,215,181,195,134,251,174,21,62,164,24,241,64]}},"size":23,"mode":420}

I know it's a for loop for just an element. But it's the only way i got it working. Hope it helps as a workaround.

zhgromov commented 4 years ago

sorry, how I can get hash address from this data? I saved address in my database and next view for hash.

"data":[18,32,138,210,135,65,209,1,129,203,63,121,61,77,238,44,52,218,162,227,202,125,215,181,195,134,251,174,21,62,164,24,241,64

chelneru commented 4 years ago

Assuming you have file as a result, you can just do file.cid.toString(). This should return the address of the file.

zhgromov commented 4 years ago

thank you. if I use different file (not txt), I should write random hardcode path? Because I haven't path of my file, I just save send jpg from props

something like this

for await (const inputFile of await ipfs.add({ path: 'randompath.txt', content: file })) { return inputFile.cid.toString() }

in props I just send file

{name: "1411957083_w1600x1200_15333.jpg", lastModified: 1583487251496, lastModifiedDate: Fri Mar 06 2020 12:34:11 GMT+0300 , webkitRelativePath: "", size: 462128, …}

and get hash Qmf6SyXbY4F8YChTGiRu6wai33s9q71dWg4EX5V8n6axwY

it will be correct? Also this option working on Chrome and crash in safari Unhandled Rejection (TypeError): null is not an object (evaluating 'ipfs.add')

sorry for questions, I try to understand and fix it..

chelneru commented 4 years ago

You can also do it without providing the Path property.

I don't know what are the issues in the browsers.

One way to test it if the hash is ok is to just just test it with ipfs.get()

achingbrain commented 4 years ago

The API was updated to use async iterators as of v0.41.0, please see the migration guide.

Previously ipfs.add returned an array, which was great, unless you were adding a lot of files in which case your process could run out of memory. Now it returns an async iterator as you've found.

n.b. you don't need to do the double await:

// instead of
for await (const inputFile of await ipfs.add({ path: 'randompath.txt', content: file })) {

// do:
for await (const inputFile of ipfs.add({ path: 'randompath.txt', content: file })) {

I'm glad you figured it out though, in future please ask usage questions like this at https://discuss.ipfs.io so other people can discover the answer.