There is an index.js file that allows you to retrieve a link to an image stored in the pinata.

Here is the code:

const pinataSDK = require('@pinata/sdk');
require('dotenv').config();
const fs = require('fs');

const pinata = new pinataSDK(process.env.PINATA_API_KEY, process.env.PINATA_API_SERCRET);
const readableStreamForFile = fs.createReadStream('./images/cool-bear-#1.png');

const options = {
    pinataMetadata: {
        name: "Cool Bear #1",
        keyvalues: {
            customKey: 'customValue',
            customKey2: 'customValue2'
        }
    },
    pinataOptions: {
        cidVersion: 0
    }
};

const pinFileToIPFS = () => {
    return pinata.pinFileToIPFS(readableStreamForFile, options).then((result) => {
        return 'https://gateway.pinata.cloud/ipfs/${result.IpfsHash}'

    }).catch((err) => {
        
        console.log(err);
    });
}

const getMetadata = async () => {
    const imageUrl = await pinFileToIPFS()
    console.log(imageUrl)
}

getMetadata()

And after I run node index.js instead of a link to the picture, I get only https://gateway.pinata.cloud/ipfs/${result.IpfsHash}. I mean the link without IpfsHash. Why?

I have result of ipfs:

IpfsHash: 'Qme5RqGiPZPFA65xNdvinZisirAU1VisAzAwZFWAzatSJ8',
PinSize: 1204410,
Timestamp: '2023-02-27T15:45:03.015Z'

And I need to add IpfsHash (Qme5RqGiPZPFA65xNdvinZisirAU1VisAzAwZFWAzatSJ8) to "https://gateway.pinata.cloud/ipfs/" and get the https://gateway.pinata.cloud/ipfs/Qme5RqGiPZPFA65xNdvinZisirAU1VisAzAwZFWAzatSJ8.

How to do that? Why ${result.IpfsHash} is not working?

1

There are 1 best solutions below

0
On

Change the quotes (') to backticks (`):

return `https://gateway.pinata.cloud/ipfs/${result.IpfsHash}`;