Uploading video to IPFS

import { getFilesFromPath, Web3Storage } from "web3.storage";

/* ********************** Token *************************** */

const apiToken = <YOUR API TOKEN>

/* *********************** Image ************************** */

/* Function gateway URL for image */
function makeGatewayURLImage(vidCID, vidName) {
    return `https://${vidCID}.ipfs.w3s.link/${vidName}`;
  }
  
/* Construct with token and endpoint */
const client = new Web3Storage({ token: apiToken });
  
/* Get file from path for image */
const vid = await getFilesFromPath("./CatVideo.mp4");
/* Get image name from path */
const vidName = vid[0].name;
  
/* get imgCID from web3.storage */
const vidCID = await client.put(vid, { name: "vidName" });
console.log("vidCID: ", vidCID);
/* Make gateway URL for image */
const vidURL = makeGatewayURLImage(vidCID, vidName);
console.log("vidURL: ", vidURL);
node uploadFunction.js

https://lh5.googleusercontent.com/U2gVw4m6ag2YZwq-DQKf9N6nqzsNdeyuMCrK3fj-wJjEQbO2w_tmRcTFwJ03Qd7jeEbVpO2_weOI4JvHAndXwQPA4r3iB22uuOmgMSXLdAcGyQkYXMwYOgCxcorTie1VuLeJiNKhZo7fGQg6Ntd99y8

Again, we get the respective CDN and URL of the video uploaded to the IPFS Network. You did it again mate well done.

Uploading multiple files:

Here the code doesn't change much, only the part where we upload the file, we pass it as an array buffer in [ file1, file2, …. fileN] Square brackets.

If we want to upload metadata, video or say image simultaneously we can do that too.

import { getFilesFromPath, Web3Storage } from "web3.storage";
import fs from "fs";

/* **************** Token and Client ************************* */
const apiToken = <YOUR API TOKEN>

/* Construct client with token and endpoint */
const client = new Web3Storage({ token: apiToken });

/* ***************** Video + JSON Files******************** */

/* metadata for json */
const metadata = {
  name: "Video Title",
  description: "Video Description goes here",
};

/* conversion to json file using fs*/
fs.writeFileSync("metadata.json", JSON.stringify(metadata));

/* Here we are uploading multiple files through an array specified */
const main = await getFilesFromPath(["./metadata.json", "./video.mp4"]);

/* Name of file */
const jsonName = main[0].name;
const videoName = main[1].name;

/* **************** URLS Functions to be called **************** */
function parentURL(CID) {
  return `https://${CID}.ipfs.w3s.link`;
}
function videoURL(CID, videoName) {
  return `https://${CID}.ipfs.w3s.link/${videoName}`;
}
function jsonURL(CID, jsonName) {
  return `https://${CID}.ipfs.w3s.link/${jsonName}`;
}

/* ****************** Upload to Web3 Storage ******************* */

/* get CID from web3.storage */
const CID = await client.put(main);
console.log("CID: ", CID);

/* ****************** Generate and console URLS ***************** */

/* get Parent URL from CID */
const parent = parentURL(CID);
console.log("Parent URL: ", parent);

/* get Video URL from CID */
const video = videoURL(CID, videoName);
console.log("Video URL: ", video);

/* get JSON URL from CID */
const json = jsonURL(CID, jsonName);
console.log("JSON URL: ", json);

Kudos guys, you did it, you can again run this file in node. You’ll get the parent URL containing all the files and the specific child URL containing each Metadata and Video respectively.