我正在使用ipfs-http-client模块与IPFS交互。我的问题是,我需要我生成的链接上的文件扩展名,而且我似乎只能使用wrapWithDirectory标志(-w和命令行)来获得它。但是到目前为止,这个标志使得结果是空的。关于IPFS的文档仅仅是关于命令行的,我只了解了一些关于如何实现它的教程,但是使用了JS以外的其他工具,或者手动上传文件夹。我需要通过一个JS脚本,一个文件来完成这个任务。其动机是,我希望为NFT生成元数据,而元数据字段需要指向具有特定扩展名的文件。
详细信息:我需要在Opensea上添加一个GLB文件。GLB就像GLTF,它是3D文件的标准。Opensea可以检测NFT元数据的animation_url字段并呈现该文件。但它需要以.glb结束。翻译,我的NFT需要它的元数据看起来像这样:
{
name: <name>,
description: <description>,
image: <image>,
animation_url: 'https://ipfs.io/ipfs/<hash>.glb' // Opensea requires the '.glb' ending.
}到目前为止,我这样做的方式如下:
import { create } from 'ipfs-http-client';
const client = create({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https',
headers: { authorization },
});
const result = await client.add(file); // {path: '<hash>', cid: CID}
const link = `https://ipfs.io/ipfs/${result.path}` // I can't add an extension here.在这段代码中,我可以将animation_url: link放在元数据对象中,但是OpenSea不会识别它。我也尝试添加上述选项:
const result = await client.add(file, {wrapWithDirectory: true}); // {path: '', cid: CID}但是result.path是一个空字符串。如何生成以.glb结尾的链接
发布于 2022-10-28 08:25:00
找到了解决办法。它确实涉及创建一个目录,这是返回的CID,这样我们就可以将文件名的扩展名追加到末尾。结果是https://ipfs.io/ipfs/<directory_hash>/<file_name_with_extension>。
因此,纠正上面的代码会给出以下内容:
import { create } from 'ipfs-http-client';
const client = create({
host: 'ipfs.infura.io',
port: 5001,
protocol: 'https',
headers: { authorization },
});
const content = await file.arrayBuffer(); // The file needs to be a buffer.
const result = await client.add(
{content, path: file.name},
{wrapWithDirectory: true}
);
// result.path is empty, it needs result.cid.toString(),
// and then one can manually append the file name with its extension.
const link = `https://ipfs.io/ipfs/${result.cid.toString()}/${result.name}`;https://stackoverflow.com/questions/74213629
复制相似问题