我正在尝试使用 Contentful Content Management API 创建图像并将其作为资产发布,但到目前为止一直没有成功,我已经能够成功创建和发布条目,但我无法弄清楚为什么资产无法正常工作。这是我创建资产的代码。最终目标是使这项工作适用于一系列图像/附件,但我试图在处理多个图像/附件之前只使用一个。
makeAsset = async (item) => {
try {
var MClient = createClient({
accessToken: process.env.REACT_APP_CONTENTFUL_MANAGEMENT_ACCESS_TOKEN,
});
let space = await MClient.getSpace(
process.env.REACT_APP_CONTENTFUL_API_SPACE
);
let environment = await space.getEnvironment(
process.env.REACT_APP_CONTENTFUL_ENVIRONMENT
);
const asset = await environment.createAsset({
fields: {
title: {
"en-US": item.imgNames[0],
},
file: {
"en-US": {
fileName: item.images[0].name,
contentType: item.images[0].type,
uploadFrom: item.imgUrls[0],
},
},
},
});
const processedAsset = await asset.processForAllLocales();
processedAsset.publish();
控制台中的错误是这样的:
ValidationFailed: {
"status": 422,
"statusText": "",
"message": "Validation error",
"details": {
"errors": [
{
"name": "type",
"value": "data:image/png;base64,iVBORw0KGg(The rest of the image URL from above)
"type": "Object",
"details": "The type of \"value\" is incorrect, expected type: Object",
"path": [
"fields",
"file",
"en-US",
"uploadFrom"
]
}
]
},
"request": {
"url": "assets",
"headers": {
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/vnd.contentful.management.v1+json",
"X-Contentful-User-Agent": "sdk contentful-management.js/5.26.1; platform browser; os Windows;",
"Authorization": "Bearer ...7ijJE"
},
"method": "post",
"payloadData": "{\"fields\":{\"title\":{\"en-US\":\"scuba_mask\"},\"file\":{\"en-US\":{\"fileName\":\"scuba_mask.png\",\"contentType\":\"image/png\",\"uploadFrom\":\"data:image/png;base64,iVBORw0KG(The rest of image URL)
我一直在尝试搜索其他示例,因为文档不完整,并且没有一个示例对我有用。我尝试的一种解决方案是使用 fs.createReadStream() 函数,但它已从 React 中删除。