You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Sanity IO批量上传图片及创建产品文档方法咨询

如何在Sanity中批量上传图片并创建产品文档

完全不需要在Sanity Studio里逐个操作,你可以通过CLI工具或者自定义脚本实现批量上传图片+创建产品文档,下面是具体步骤:

1. 前期准备

  • 先确认你的Sanity项目已经定义好product类型的schema,示例如下(可按需调整):
    // schemas/product.js
    export default {
      name: 'product',
      type: 'document',
      title: '产品',
      fields: [
        {
          name: 'name',
          type: 'string',
          title: '产品名称'
        },
        {
          name: 'price',
          type: 'number',
          title: '价格'
        },
        {
          name: 'image',
          type: 'image',
          title: '产品图片'
        }
      ]
    }
    
  • 整理本地资源:把所有产品图片放在同一个文件夹(比如./product-images),同时准备一个JSON文件(比如product-data.json),记录每个产品的名称、价格和对应图片的文件名,示例JSON:
    [
      {
        "name": "纯棉T恤",
        "price": 79.9,
        "imageFilename": "tshirt-white.jpg"
      },
      {
        "name": "休闲牛仔裤",
        "price": 129.9,
        "imageFilename": "jeans-blue.jpg"
      }
    ]
    

2. 用Sanity CLI快速批量导入(适合非开发者)

步骤1:上传所有图片到Sanity资产库

打开终端,进入Sanity项目根目录,执行命令:

sanity assets upload ./product-images --dataset your-dataset-name

执行后会返回每个图片的_id(即asset的引用ID),把这些ID对应更新到product-data.json里,更新后的产品数据格式如下:

[
  {
    "_type": "product",
    "name": "纯棉T恤",
    "price": 79.9,
    "image": {
      "_type": "image",
      "asset": {
        "_ref": "image-abc123-def456-ghi789-jkl012-mno34567890-jpg"
      }
    }
  }
]

步骤2:导入产品文档到Sanity

执行以下命令完成文档导入:

sanity dataset import ./product-data.json your-dataset-name

3. 用JavaScript脚本批量处理(更灵活,适合开发者)

如果觉得手动更新asset ID麻烦,可以写个脚本自动完成图片上传+文档创建:

步骤1:安装依赖

在项目根目录执行:

npm install @sanity/client

步骤2:编写导入脚本

创建import-products.js文件,内容如下:

import { createClient } from '@sanity/client';
import { readdirSync, readFileSync } from 'fs';
import path from 'path';

// 替换成你的项目信息
const client = createClient({
  projectId: '你的项目ID',
  dataset: '你的数据集名称',
  token: '你的写入权限token', // 在Sanity管理面板的API设置中生成
  useCdn: false,
  apiVersion: '2024-01-01' // 使用最新API版本
});

async function importProducts() {
  const imagesDir = './product-images';
  const products = JSON.parse(readFileSync('./product-data.json', 'utf8'));

  for (const product of products) {
    try {
      // 上传图片到Sanity资产库
      const imagePath = path.join(imagesDir, product.imageFilename);
      const asset = await client.assets.upload('image', readFileSync(imagePath), {
        filename: product.imageFilename
      });

      // 创建产品文档
      await client.create({
        _type: 'product',
        name: product.name,
        price: product.price,
        image: {
          _type: 'image',
          asset: { _ref: asset._id }
        }
      });

      console.log(`已导入产品:${product.name}`);
    } catch (error) {
      console.error(`导入产品${product.name}失败:`, error);
    }
  }

  console.log('所有产品导入完成!');
}

// 执行导入
importProducts().catch(console.error);

步骤3:运行脚本

node import-products.js

注意事项

  • 确保产品schema已正确部署到Sanity项目(执行sanity deploy更新schema)。
  • 使用脚本时,写入权限token需要有数据集的编辑权限,避免上传失败。
  • 单张图片大小尽量控制在10MB以内,支持JPG、PNG、WebP等常见格式。

内容的提问来源于stack exchange,提问作者John Smith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 12:00:26