Created
          June 25, 2022 15:05 
        
      - 
      
- 
        Save stevedylandev/070da3a0a90554ded572cf9730b3bc0a to your computer and use it in GitHub Desktop. 
    Upload a Folder with Pinata API
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import fs from "fs" | |
| import FormData from "form-data" | |
| import rfs from "recursive-fs" | |
| import basePathConverter from "base-path-converter" | |
| import got from 'got' | |
| const pinDirectoryToPinata = async () => { | |
| const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`; | |
| const src = "PATH_TO_FOLDER"; | |
| var status = 0; | |
| try { | |
| const { dirs, files } = await rfs.read(src); | |
| let data = new FormData(); | |
| for (const file of files) { | |
| data.append(`file`, fs.createReadStream(file), { | |
| filepath: basePathConverter(src, file), | |
| }); | |
| } | |
| const response = await got(url, { | |
| method: 'POST', | |
| headers: { | |
| "Content-Type": `multipart/form-data; boundary=${data._boundary}`, | |
| "Authorization": "Bearer PINATA_API_JWT" | |
| }, | |
| body: data | |
| }) | |
| .on('uploadProgress', progress => { | |
| console.log(progress); | |
| }); | |
| console.log(JSON.parse(response.body)); | |
| } catch (error) { | |
| console.log(error); | |
| } | |
| }; | |
| pinDirectoryToPinata() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment