我正在尝试从JavaScript的客户端上传一个已经上传到IPFS的文件的元数据( NextJS对象)。我能够在Infura's HTTP API包的帮助下使用ipfs-http-client将文件(PDF)上传到IPFS,但我不能使用JSON吗?我也尝试过使用pinata SDK上传,但没有成功。如何将JavaScript对象上传到IPFS?
发布于 2022-09-20 22:42:41
简短的回答:对JavaScript对象执行JSON.stringify()并将返回的值上传到IPFS。您可以使用恩弗拉的HTTP端点,也可以使用ipfs-http-client,这是IPFS的客户端库。
长篇大论:
从用户磁盘上传文件:-
1.使用ipfs-core包:
yarn add ipfs-coreimport * as IPFS from 'ipfs-core'
const ipfs = await IPFS.create() //creating an IPFS node
//passing the file object extracted from the HTML input.
const { cid } = await ipfs.add(file)
console.info(cid) // QmXXY5ZxbtuYj6DnfApLiGstzPN7fvSyigrRee3hDWPCaf2.使用公共指规数网关
将JavaScript对象上传为JSON:-
JSON.stringify(<JS_OBJECT>)对JSON进行字符串化,并将此函数的返回值上传到IPFS。发布于 2022-11-24 22:26:57
您可以使用没有第三方库的皮纳塔-api。
从客户端向服务器发送元数据,服务器向pinata发送post请求。
const url = `https://api.pinata.cloud/pinning/pinJSONToIPFS`;
const res = await axios.post(
url,
{
pinataMetadata: {
name: "add a name",
},
// assuming client sends `nftMeta` json
pinataContent: req.body.nftMeta,
},
{
headers: {
pinata_api_key: yourPinataApiKey,
pinata_secret_api_key: yourPinataSecretApiKey,
},
}
);https://stackoverflow.com/questions/73793326
复制相似问题