Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Berkay
Berkay

Posted on • Originally published atraufsamestone.com

     

How to send an image from Unsplash to IPFS?

I suggest you include theweb3 environment in your growth strategy. Therefore, you should use technical implementations decentralized networks likesubgraph orIPFS.


In this guide, you'll learn how to send image files toIPFS usingipfs-http-client and Nextjs

You can see the demohere or jump to directlyGitHub

Demo

Pre-requests

  • Node 15 >

Getting started

Create a simple next js app:

npxcreate-next-appipfs
Enter fullscreen modeExit fullscreen mode

Add IPFS HTTP client library using yarn:

yarnaddipfs-http-client
Enter fullscreen modeExit fullscreen mode

Create a new constant forIPFS gateway. In this case, we will useInfura API.

constclient=create("https://ipfs.infura.io:5001/api/v0");
Enter fullscreen modeExit fullscreen mode

Get a random image URL fromUnsplash. You can see more detail inhere.

asyncfunctiongetImage(){fetch(https://source.unsplash.com/500x500/?beach).then((response) => {letimageURL=response.url;setImageURL(imageURL);});}
Enter fullscreen modeExit fullscreen mode

Or you can use your own fetching solution like custom form data, Cloudinary etc.

Then upload your file to IPFS.

The critical thing is, you should fetch theimage URL as ablob object.

constblobFile=awaitfetch(imageURL).then((r)=>r.blob());
Enter fullscreen modeExit fullscreen mode

Create a new function asuploadFile

asyncfunctionuploadFile(){constblobFile=awaitfetch(imageURL).then((r)=>r.blob());try{constadded=awaitclient.add(blobFile);consturl=https://ipfs.infura.io/ipfs/${added.path};updateIPFSURL(url);}catch(error){console.log("Error uploading file:",error);}}
Enter fullscreen modeExit fullscreen mode

Finally, you are ready to fetch and image image object from URL and send to IPFS.

Clone the full repositoryhere and run;

yarndev
Enter fullscreen modeExit fullscreen mode

You can see the more detailed cases about Web3 from Nader Dabit’s postshere.


Thanks for reading. 👋 Don't forget to subscribe me onMedium orDev.to

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

co-founder at Hardal
  • Location
    İstanbul, Turkey
  • Work
    Developer
  • Joined

More fromBerkay

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp