How can I pin json to ipfs with pinata (pinJsonToIpfs method) info directory? React

320 views Asked by At
const metadata = {
                pinataContent: {
                    name: nftItem.name,
                    image: imageUrl,
                    description: nftItem.description,
                    external_url: nftItem.externalUrl,
                    attributes: nftItem.attributes || [],
                },
                pinataOptions: { wrapWithDirectory: nftList.length > 1 },
                pinataMetadata: {
                    name: `${nftItem.id}`,
                },
            };

            const pinataMetadata = await axios.post(
                'https://api.pinata.cloud/pinning/pinJSONToIPFS',
                metadata,
                {
                    headers: {
                        'Content-Type': 'application/json',
                        Authorization: `Bearer ${pinata.data.apiJwt}`,
                    },
                },
            );

here I mapping the nftList array, create metadata for every nftItem and trying to pin it. I know only that metadata have a spechial key "wrapWithDirectory", but a cant understand how it works.

The finish result what i wand - when nftList.length > 1 all json metadata files will have saved into 1 folder on pinata ploud account.

1

There are 1 answers

0
Steve Simkins On

I would actually not use the wrapWithDirectory for this kind of upload as it won't accomplish what you're trying to do. If you want all of the metadata files to be in one folder you would need to try something like this

const pinFileToIPFS = async () => {
  try {
      const folder = 'testing_json';
const json1 = { hello: 'world' }
const json2 = { hello: 'world2' }
const blob1 = new Blob([JSON.stringify(json1, null, 2)], { type: 'application/json' })
const blob2 = new Blob([JSON.stringify(json2, null, 2)], { type: 'application/json' })

const files = [
  new File([blob1], 'hello.json', { type: 'application/json' }),
  new File([blob2], 'hello2.json', { type: 'application/json' })
]
const data = new FormData();

Array.from(files).forEach((file) => {
  data.append('file', file, `${folder}/${file.name}`)
})

 const pinataMetadata = JSON.stringify({
      name: `${folder}`
    })
    data.append('pinataMetadata', pinataMetadata)
    const req = {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${PINATA_JWT}`,
          
      },
      body: data
    }
    
    const res = await fetch('https://api.pinata.cloud/pinning/pinFileToIPFS', req)
    console.log(req, res)
    const resData = await res.json()
    console.log(resData)
  } catch (error) {
    console.log(error)
  }
}

pinFileToIPFS()