如何在ipfs中创建文件夹并上传图像

时间:2020-07-20 12:51:59

标签: ipfs

https://proto.school/#/mutable-file-system
我已经看过此链接,但是不知道如何在node.js中做同样的事情


我已经在IPFS网络中添加了“ hello”字样,并且可以正常工作,并且还使用了图像将其上传到ipfs中,但是我想知道如何在ipfs网络中创建文件夹并将图像上传到该文件夹​​中

所以我的问题是如何创建文件夹并将图片上传到该文件夹​​。

这是我的代码。

     const addFile = async () => {
            //const Added = await ipfs.add('hello');
              const fsReadImgData = fs.readFileSync('image1.jpg');
               var ipfsSave = await ipfs.add({ 
                                    path:image1.jpg, 
                                    content: fsReadImgData 
                              });
            return fsReadImgData;
        }
        const fileHash = await addFile();

1 个答案:

答案 0 :(得分:1)

首先将文件读取到缓冲区中(或替换为要获取的图像数据:

const imgdata = fs.readFileSync('/yourfile.jpg');

常规IPFS文件方法(不可变,您不希望更新这些方法):

let added = await ipfs.add({
  path: 'images/yourfile.jpg',
  content: imgdata
}, { wrapWithDirectory: true })

可变文件系统方法(您希望更新和更改文件):

await ipfs.files.mkdir('/images')

await ipfs.files.write(
  '/images/yourfile.jpg',
  imgdata,
  {create: true})