Skip to content

Instantly share code, notes, and snippets.

@ac12644
Created November 1, 2022 09:05
Show Gist options
  • Save ac12644/7915a068ba56a64e3fb1f2b593243bcf to your computer and use it in GitHub Desktop.
Save ac12644/7915a068ba56a64e3fb1f2b593243bcf to your computer and use it in GitHub Desktop.
import { create } from 'ipfs-http-client';
const auth =
'Basic ' + Buffer.from(INFURA_IPFS_PROJECT_ID + ':' + INFURA_IPFS_PROJECT_SECRET_KEY).toString('base64');
const client = create({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https',
headers: {
authorization: auth,
},
});
async function saveToIpfs(e) {
const file = e.target.files[0];
try {
const added = await client.add(file, {
progress: (prog) => console.log(`received: ${prog}`),
});
const fileUrl = `${INFURA_IPFS_SUBDOMAIN_GATEWAY}/ipfs/${added.path}`;
console.log(added, '----', added.path);
} catch (error) {
console.log('Error uploading file: ', error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment