0

I've deployed smart contract for nft , I'm using pinata to store my nft asset but it shows an error of Error occurred while pinning file to IPFS: Error: Request failed with status code 403.

I've exported node env to dev already

runscript.js
const pinFileToIPFS = require('./pinFileToIPFS');

const filePath = path.join(__dirname, '../assets/vish.gif');
// const filePath = path.join(__dirname, '../data/metadata.json');

pinFileToIPFS(filePath);

pinFileTOIPFS.js

require('dotenv').config();
const fs = require('fs');
const axios = require('axios');
const FormData = require('form-data');
const { storeDataToFile } = require('./ipfsHelper.js');

// Calls Pinata API's to pin file to IPFS
const pinFileToIPFS = async (filePath) => {
  const pinataEndpoint = process.env.PINATA_ENDPOINT;
  const pinataApiKey = process.env.PINATA_API_KEY;
  const pinataApiSecret = process.env.PINATA_API_SECRET;
  const form_data = new FormData();
  try {
    form_data.append('file', fs.createReadStream(filePath));
    const request = {
      method: 'post',
      url: pinataEndpoint,
      maxContentLength: 'Infinity',
      headers: {
        pinata_api_key: pinataApiKey,
        pinata_secret_api_key: pinataApiSecret,
        'Content-Type': `multipart/form-data; boundary=${form_data._boundary}`,
      },
      data: form_data,
    };
    console.log('request:', request);
    const response = await axios(request);
    console.log('Successfully pinned file to IPFS : ', response);
    await storeDataToFile(response.data);
    console.log('Successfully added IPFS response to json file');
  } catch (err) {
    console.log('Error occurred while pinning file to IPFS: ', err);
  }
};

module.exports = pinFileToIPFS;

ipfsHelper.js

const fs = require('fs').promises;
const path = require('path');
const config = require('config');

// https://stackoverflow.com/questions/36856232/write-add-data-in-json-file-using-node-js
// Accepts json data and stores in specified filePath.
// If the file does not exists in specified location, it creates it
const storeDataToFile = async (jsonData) => {
  try {
    const filePath = path.join(__dirname, config.get('ipfsFile.location'));
    const ipfsFileExists = await fileExists(filePath);
    if (!ipfsFileExists) {
      console.log('ipfsFileExists: ', ipfsFileExists);
      // First time creating an empty file with [].
      // We will be storing all ipfsHashes as array of objects
      await fs.writeFile(filePath, JSON.stringify([]));
    }
    const data = await fs.readFile(filePath, 'utf8');
    const json = JSON.parse(data);
    json.push(jsonData);
    await fs.writeFile(filePath, JSON.stringify(json));
  } catch (err) {
    console.log('Error occured while storing data to file', err);
  }
};

async function fileExists(path) {
  try {
    const res = await fs.access(path);
    return true;
  } catch (err) {
    // no such file or directory. File really does not exist
    if (err.code == 'ENOENT') {
      return false;
    }
    console.log('Exception fs.statSync (' + path + '): ' + err);
    // some other exception occurred
    throw err;
  }
}

module.exports = {
  storeDataToFile,
  fileExists,
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.