blockchain:dappsからipfsにファイルをアップロードする
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:dappsからipfsにファイルをアップロードする [2022/06/06 02:02] – dot | blockchain:dappsからipfsにファイルをアップロードする [2022/06/16 12:54] (現在) – dot | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== DAppsからIPFSにファイルをアップロードする ====== | ||
| + | |||
| + | React で Web アプリケーションを実装し、HTML の FILE API で指定されたファイルを IPFS にアップロードする機能を実現します。 | ||
| + | |||
| + | ===== React プロジェクトの作成 ===== | ||
| <code JavaScript> | <code JavaScript> | ||
| 行 6: | 行 11: | ||
| IPFS> npx create-react-app ipfs-web --template typescript | IPFS> npx create-react-app ipfs-web --template typescript | ||
| IPFS> cd .\ipfs-web\ | IPFS> cd .\ipfs-web\ | ||
| - | IPFS\ipfs-web> | + | IPFS\ipfs-web> |
| </ | </ | ||
| + | |||
| + | 「[[https:// | ||
| + | |||
| + | ===== 参考 ===== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | ===== IPFSUploader.tsx の実装 ===== | ||
| + | |||
| + | IPFS にファイルをアップロードする機能を提供する IPFSUploader.tsx コンポーネントを実装します。 | ||
| + | |||
| + | <code PowerShell> | ||
| + | IPFS\ipfs-web> | ||
| + | IPFS\ipfs-web> | ||
| + | </ | ||
| + | |||
| + | <code JavaScript> | ||
| + | import React, { useState, useEffect } from ' | ||
| + | import type { IPFS } from ' | ||
| + | import * as IPFSCore from ' | ||
| + | |||
| + | let ipfs: IPFS; | ||
| + | |||
| + | function IPFSUploader() { | ||
| + | const [isIpfsReady, | ||
| + | const [version, setVersion] = useState("" | ||
| + | const [cid, setCID] = useState("" | ||
| + | |||
| + | useEffect(() => { | ||
| + | startIpfs(); | ||
| + | return function cleanup () { | ||
| + | if (ipfs && ipfs.stop) { | ||
| + | console.log(' | ||
| + | ipfs.stop().catch((err: | ||
| + | //ipfs = undefined; | ||
| + | setIpfsReady(false); | ||
| + | } | ||
| + | } | ||
| + | }, []); | ||
| + | |||
| + | const startIpfs = async () => { | ||
| + | if(ipfs){ | ||
| + | console.log(' | ||
| + | /* | ||
| + | } else if (window.ipfs && window.ipfs.enable) { | ||
| + | console.log(' | ||
| + | ipfs = await window.ipfs.enable({ commands: [' | ||
| + | */ | ||
| + | } else { | ||
| + | ipfs = await IPFSCore.create(); | ||
| + | console.log(" | ||
| + | const _version = await ipfs.version(); | ||
| + | setVersion(_version.version); | ||
| + | } | ||
| + | setIpfsReady(Boolean(ipfs)); | ||
| + | }; | ||
| + | const fileSelected = async (event: React.ChangeEvent< | ||
| + | if(!isIpfsReady) return; | ||
| + | |||
| + | const targetFile = event.target.files? | ||
| + | if(targetFile){ | ||
| + | const _result = await ipfs.add(targetFile); | ||
| + | //const _result = await ipfs.add(' | ||
| + | setCID(_result.path); | ||
| + | } | ||
| + | }; | ||
| + | |||
| + | return ( | ||
| + | <div> | ||
| + | < | ||
| + | < | ||
| + | <input id=" | ||
| + | <div> | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | export default IPFSUploader; | ||
| + | </ | ||
| + | |||
| + | ===== App.tsx に IPFSUploader を追加 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | IPFS\ipfs-web> | ||
| + | </ | ||
| + | |||
| + | 不要な記述を削除し、IPFSUploader コンポーネントを追加します。 | ||
| + | |||
| + | <code JavaScript> | ||
| + | import React from ' | ||
| + | import ' | ||
| + | import IPFSUploader from ' | ||
| + | |||
| + | function App() { | ||
| + | return ( | ||
| + | <div className=" | ||
| + | <header className=" | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | export default App; | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== 実行 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | IPFS\ipfs-web> | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | ===== IPFSDirectoryUploader ===== | ||
| + | |||
| + | MFS を利用してファイルをアップロードする。 | ||
| + | <code PowerShell> | ||
| + | IPFS\ipfs-web> | ||
| + | </ | ||
| + | |||
| + | <code JavaScript> | ||
| + | import React, { useState, useEffect } from ' | ||
| + | import type { IPFS } from ' | ||
| + | import * as IPFSCore from ' | ||
| + | import { MFSEntry } from ' | ||
| + | |||
| + | let ipfs: IPFS; | ||
| + | |||
| + | function IPFSDirectoryUploader() { | ||
| + | const [isIpfsReady, | ||
| + | const [version, setVersion] = useState("" | ||
| + | const [MFSEntlies, | ||
| + | |||
| + | useEffect(() => { | ||
| + | startIpfs(); | ||
| + | return function cleanup () { | ||
| + | if (ipfs && ipfs.stop) { | ||
| + | console.log(' | ||
| + | ipfs.stop().catch((err: | ||
| + | //ipfs = undefined; | ||
| + | setIpfsReady(false); | ||
| + | } | ||
| + | } | ||
| + | }, []); | ||
| + | |||
| + | const startIpfs = async () => { | ||
| + | if(ipfs){ | ||
| + | console.log(' | ||
| + | /* | ||
| + | } else if (window.ipfs && window.ipfs.enable) { | ||
| + | console.log(' | ||
| + | ipfs = await window.ipfs.enable({ commands: [' | ||
| + | */ | ||
| + | } else { | ||
| + | ipfs = await IPFSCore.create(); | ||
| + | console.log(" | ||
| + | const _version = await ipfs.version(); | ||
| + | setVersion(_version.version); | ||
| + | |||
| + | const lsResult = await ipfs.files.ls("/" | ||
| + | let uploadDirectoryExist = false; | ||
| + | for await (const file of lsResult){ | ||
| + | if(file.name === " | ||
| + | console.log("/ | ||
| + | uploadDirectoryExist = true; | ||
| + | } | ||
| + | } | ||
| + | if(!uploadDirectoryExist){ | ||
| + | console.log(" | ||
| + | await ipfs.files.mkdir("/ | ||
| + | } | ||
| + | } | ||
| + | setIpfsReady(Boolean(ipfs)); | ||
| + | }; | ||
| + | const fileSelected = async (event: React.ChangeEvent< | ||
| + | if(!isIpfsReady) return; | ||
| + | |||
| + | const targetFile = event.target.files? | ||
| + | if(targetFile){ | ||
| + | const fileReader = new window.FileReader(); | ||
| + | fileReader.readAsArrayBuffer(targetFile); | ||
| + | fileReader.onloadend = async () => { | ||
| + | let ab = fileReader.result as ArrayBuffer; | ||
| + | const u8a = new Uint8Array(ab); | ||
| + | console.log(`upload / | ||
| + | await ipfs.files.write(`/ | ||
| + | await ls("/ | ||
| + | }; | ||
| + | } | ||
| + | }; | ||
| + | const ls = async (path: string) => { | ||
| + | const lsResult = await ipfs.files.ls(path); | ||
| + | for await (const file of lsResult){ | ||
| + | console.log(`${file.name}: | ||
| + | } | ||
| + | }; | ||
| + | const MFSStat = async () => { | ||
| + | if(!isIpfsReady) return; | ||
| + | |||
| + | const lsResult = await ipfs.files.ls('/ | ||
| + | let _mfs_entlies: | ||
| + | for await (const file of lsResult){ | ||
| + | console.log(`${file.name}: | ||
| + | _mfs_entlies.push(file); | ||
| + | } | ||
| + | setMFSEntlies(_mfs_entlies); | ||
| + | }; | ||
| + | |||
| + | return ( | ||
| + | <div> | ||
| + | < | ||
| + | < | ||
| + | <input id=" | ||
| + | <div> | ||
| + | < | ||
| + | <p> | ||
| + | { MFSEntlies? | ||
| + | <div> | ||
| + | < | ||
| + | <p> | ||
| + | <a href={`https:// | ||
| + | <img src={`https:// | ||
| + | </p> | ||
| + | </ | ||
| + | ))} | ||
| + | </p> | ||
| + | </ | ||
| + | </ | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | export default IPFSDirectoryUploader; | ||
| + | </ | ||
| + | |||
| + | App.tsx に IPFSDirectoryUploader コンポーネントを記述する。 | ||
blockchain/dappsからipfsにファイルをアップロードする.1654480966.txt.gz · 最終更新: by dot
