Node.js API
在 Node.js 应用中使用同一上传引擎。先通过 CLI 配置并验证上传目标,再在应用里读取这份配置。
安装与初始化
bash
npm install [email protected]使用 ES 模块(.mjs,或项目设置 type: module):
js
import { PicGo } from 'piclist'
const client = await PicGo.create('/path/to/data.json')
const images = await client.upload(['/absolute/path/image.png'])
if (images instanceof Error) throw images
if (!images.length || !images.every(image => image.imgUrl)) {
throw new Error('Upload did not return all expected image URLs')
}
const urls = images.map(image => image.imgUrl)PicGo.create() 完成异步初始化。不传配置路径时使用 Core 默认路径。不传上传输入时读取剪贴板。
本次上传的图床与配置
js
const images = await client.upload(['/absolute/path/image.png'], {
picBed: 'aws-s3-plist',
configName: 'Docs'
})Node API 的属性为 picBed;CLI 使用 --picbed,HTTP 使用 picbed 查询参数。选择参数只影响本次调用,不改变已保存的默认配置。
第二图床的结果
upload() 返回主图床结果。需要已配置的第二图床上传时,调用 uploadReturnCtx():
js
const result = await client.uploadReturnCtx(['/absolute/path/image.png'])
const primaryImages = result.ctx.output
const secondaryImages = result.backupCtx?.output ?? []分别验证主图床和第二图床结果。第二图床失败可能只写入错误日志,不能仅凭主图床成功推断两份文件都已完成。
CommonJS
Core 3.0 使用 ES 模块。在 CommonJS 中用动态 import:
js
async function uploadImage() {
const { PicGo } = await import('piclist')
const client = await PicGo.create('/path/to/data.json')
return client.upload(['/absolute/path/image.png'])
}使用配置与扩展
getConfig(key) 读取配置;saveConfig(object) 按字段路径保存配置。不要将完整配置、上传上下文或含私有签名的 URL 直接写入公开日志。
插件注册与生命周期接口见 Core 类型定义。桌面脚本 API 与 Node API 有不同的运行上下文,详见脚本指南。