【问题标题】:IPFS file extension for GLBGLB 的 IPFS 文件扩展名
【发布时间】:2022-11-12 00:04:06
【问题描述】:

我正在使用 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 结尾的链接?

【问题讨论】:

    标签: ipfs nft opensea js-ipfs ipfs-http-client


    【解决方案1】:

    找到了解决方案。它确实涉及创建一个目录,即返回的 CID,以便我们可以在最后附加文件名及其扩展名。结果是https://ipfs.io/ipfs/&lt;directory_hash&gt;/&lt;file_name_with_extension&gt;

    因此,更正上面的代码会得到以下结果:

    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}`;
    

    【讨论】:

      猜你喜欢
      • 2023-02-11
      • 1970-01-01
      • 2012-12-24
      • 2013-12-19
      • 1970-01-01
      • 2011-05-07
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      相关资源
      最近更新 更多