Js-ipfs: Read file error on Windows

Created on 6 Sep 2017  路  9Comments  路  Source: ipfs/js-ipfs

  • Version: 0.4.10
  • Platform: OSX

1.

const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    path: 'hello.txt',
    content: require('fs').readFileSync(path.join(__dirname, './ipfs.js'))
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash);
  })
})

then https://gateway.ipfs.io/ipfs/....hash
the browser will keep loading and not showing the file.

2.Is there an example that using Readstream to add file to ipfs?

something like

const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

Because in the test example https://github.com/ipfs/interface-ipfs-core/blob/master/src/files.js

ipfs.files.createAddStream....

then load file using 

loadFixture(__dirname, '../test/fixtures/test-folder/pp.txt', 'interface-ipfs-core'),

but the loadFixture function still using

require('fs').readFileSync(path.join(dirname, file))

https://github.com/ipfs/aegir/blob/master/src/fixtures.js

kinquestion

All 9 comments

Hi @EasonWang01! I was unable to replicate the issue, could you try again?

As for the question on the Readable Stream example, you just need to pass a Readable Stream to content, see https://github.com/ipfs/interface-ipfs-core/tree/master/API/files#add.

Let me know if that works :)

Thanks for reply.
Sending a Read stream like this will get some error.

const IPFS = require('ipfs')
const stream = require('stream');
const path = require('path');

const node = new IPFS()
node.on('ready', (cb) => {
  node.files.add({
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { return cb(err) }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

You need to add a path: ''

{
  path: ''
  content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
}

Also, it seems you forgot to require fs and ready doesn't provide any callback. A correct way of doing that should be like:

const IPFS = require('ipfs')
const fs = require('fs');

const node = new IPFS()
node.on('ready', () => {
  node.files.add({
    path: ''
    content: fs.createReadStream('sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { throw err }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})

Thanks,but it still got this error

      throw new Error(`Invalid key: ${this.toString()}`)
      ^

Error: Invalid key: /CIQPX75ZVS7OPRFWDXJVWID53Z3P6NP5N5MB6J44ACZEFEYSAGOJKVQ
    at new Key (C:\Users\yi.w\Desktop\node_modules\interface-datastore\src\key.js:45:13)
    at keyFromBuffer (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:20:10)
    at cidToDsKey (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:30:10)
    at Object.has (C:\Users\yi.w\Desktop\node_modules\ipfs-repo\src\blockstore.js:129:17)
    at waterfall (C:\Users\yi.w\Desktop\node_modules\ipfs-bitswap\src\index.js:286:31)
    at nextTask (C:\Users\yi.w\Desktop\node_modules\async\waterfall.js:16:14)
    at exports.default (C:\Users\yi.w\Desktop\node_modules\async\waterfall.js:26:5)
    at Bitswap.put (C:\Users\yi.w\Desktop\node_modules\ipfs-bitswap\src\index.js:285:5)
    at BlockService.put (C:\Users\yi.w\Desktop\node_modules\ipfs-block-service\src\index.js:61:28)
    at waterfall (C:\Users\yi.w\Desktop\node_modules\ipld-resolver\src\index.js:380:28)

Ah! You are using Windows. We don't fully support Windows yet, see https://github.com/ipfs/js-ipfs/issues/861 for the full thread.

Closing this one as a duplicate. Apologies for the hurdles, help us achieve Windows support!

Thanks,will using OSX when come back home.

Hi,it running without problem,but the link got no content.Thanks

Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMs


const IPFS = require('ipfs')
const fs = require('fs');

const node = new IPFS()
node.on('ready', () => {
  node.files.add({
    path: '',
    content: fs.createReadStream('./sample.txt', { start: 90, end: 99 })
  }, (err, result) => {
    if (err) { throw err }

    console.log('\nAdded file:', result[0].path, result[0].hash)
    // process.exit()
  })
})
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7
Swarm listening on /ip4/192.168.1.6/tcp/4002/ipfs/QmbPVEbSsLUhXRN9vJQh2a4PvMDMsiFQvFF9kRi9V1rva7

Added file: QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP

https://gateway.ipfs.io/ipfs/QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP
or
ipfs get QmfJMCvenrj4SKKRc48DYPxwVdS44qCUCqqtbqhJuSTWXP

the file content is empty

I believe that is because you are adding 9 bytes which look like not to exist in the original file:

image

thanks!

Was this page helpful?
0 / 5 - 0 ratings