如何解决通过Shopware 6 API添加图像
我有一个Shopware 6.3商店,需要使用集成API将图像迁移到它。
我应该如何为媒体上载构建主体?我需要将文件放在某个地方还是只是通过链接?
我已经通过https://docs.shopware.com/en/shopware-platform-dev-en/admin-api-guide/writing-entities?category=shopware-platform-dev-en/admin-api-guide#creating-entities上的指南设法将新产品推入Shopware,但我不确定如何处理媒体。在本指南中,仅在https://docs.shopware.com/en/shopware-platform-dev-en/admin-api-guide/writing-entities?category=shopware-platform-dev-en/admin-api-guide#media-handling中说明了如何在已上传的媒体文件与产品之间创建链接,但没有有关如何实际推送媒体文件的示例。
我需要的每个图像都有URL(在数据库中,以及产品ID和图像位置)。
实体架构将媒体描述为:
"media": {
"name": "media","translatable": [
"alt","title","customFields"
],"properties": {
"id": {
"type": "string","format": "uuid"
},"userId": {
"type": "string","mediaFolderId": {
"type": "string","mimeType": {
"type": "string","readOnly": true
},"fileExtension": {
"type": "string","uploadedAt": {
"type": "string","format": "date-time","fileName": {
"type": "string","fileSize": {
"type": "integer","format": "int64","metaData": {
"type": "object","mediaType": {
"type": "object","alt": {
"type": "string"
},"title": {
"type": "string"
},"url": {
"type": "string"
},"hasFile": {
"type": "boolean"
},"private": {
"type": "boolean"
},"customFields": {
"type": "object"
},"createdAt": {
"type": "string","updatedAt": {
"type": "string","translated": {
"type": "object"
},"tags": {
"type": "array","entity": "tag"
},"thumbnails": {
"type": "array","entity": "media_thumbnail"
},"user": {
"type": "object","entity": "user"
},"categories": {
"type": "array","entity": "category"
},"productManufacturers": {
"type": "array","entity": "product_manufacturer"
},"productMedia": {
"type": "array","entity": "product_media"
},"avatarUser": {
"type": "object","mediaFolder": {
"type": "object","entity": "media_folder"
},"propertyGroupOptions": {
"type": "array","entity": "property_group_option"
},"mailTemplateMedia": {
"type": "array","entity": "mail_template_media"
},"documentBaseConfigs": {
"type": "array","entity": "document_base_config"
},"shippingMethods": {
"type": "array","entity": "shipping_method"
},"paymentMethods": {
"type": "array","entity": "payment_method"
},"productConfiguratorSettings": {
"type": "array","entity": "product_configurator_setting"
},"orderLineItems": {
"type": "array","entity": "order_line_item"
},"cmsBlocks": {
"type": "array","entity": "cms_block"
},"cmsSections": {
"type": "array","entity": "cms_section"
},"cmsPages": {
"type": "array","entity": "cms_page"
},"documents": {
"type": "array","entity": "document"
}
}
},
但不清楚哪些字段至关重要。向媒体端点发出POST请求时,是否需要先创建product-media文件夹,然后使用其ID?我可以仅指定URL,然后Shopware会将图像本身下载到文件夹中,还是继续指向我使用的URL。我需要将图像保存在Shopware中。
对于我来说,从URL下载图像并将其推送到Shopware并没有问题,但是我不确定如何使用该API(存在很多图像,它们需要批量处理)。
解决方法
一种可能的解决方案:
第一:创建新的媒体发布/api/{apiVersion}/media?_response=true
第二:“上传图片” /api/{apiVersion}/_action/media/{mediaId}/upload?extension={extension}&fileName={imgName}&_response=true
更多信息可以在这里找到:https://forum.shopware.com/discussion/comment/278603/#Comment_278603
在CASE中,用于产品的图像使用端点POST /api/{apiVersion}/product-media
并设置coverId
可通过OpenAPI架构获得所有路由的完整列表:[您的域/本地主机] /api/v3/_info/openapi3.json
在一个产品创建过程中,也可以通过一个请求设置所有介质以及Cover&CoverId。因此,设置产品Cover
和产品Media
{
"coverId":"3d5ebde8c31243aea9ecebb1cbf7ef7b","productNumber":"SW10002","active":true,"name":"Test","description":"fasdf","media":[{
"productId":"94786d894e864783b546fbf7c60a3640","mediaId":"084f6aa36b074130912f476da1770504","position":0,"id":"3d5ebde8c31243aea9ecebb1cbf7ef7b"
},{
"productId":"94786d894e864783b546fbf7c60a3640","mediaId":"4923a2e38a544dc5a7ff3e26a37ab2ae","position":1,"id":"600999c4df8b40a5bead55b75efe688c"
}],"id":"94786d894e864783b546fbf7c60a3640"
}
请牢记通过检查以下示例来检查承载令牌是否有效:
if (JwtToken.ValidTo >= DateTime.Now.ToUniversalTime() - new TimeSpan(0,5,0))
{
return Client.Get(request);
}
else
{
// refresh the token by new authentication
IntegrationAuthenticator(this.key,this.secret);
}
return Client.Get(request);
,
这适用于 Shopware 6.4
作为一般性建议,视情况而定。 API 自 6.4 以来略有变化,https://shopware.stoplight.io/docs/admin-api/docs/guides/media-handling.md 上还提供了官方文档。
然而,我认为拥有一个现实生活中的例子总是容易一些。我在我们的生产环境中所做的基本上就是这些步骤。
- (可选)检查媒体对象是否存在
- 使用端点
media-file
创建一个GET /media-files/
对象 - 如果存在,则使用新的 media-id 引用上传图像。
让我们假设文件名是 yourfilename.jpg
。您还需要一个 media-folder-id
,它将引用 Shopware 中的图像文件夹。这可以通过 Admin > Content > Media > Product Media
在 Shopware 中获得。
步骤 0
在将图片上传到 Shopware 之前,您要确保该图片不存在,以便您可以跳过它。
此步骤是可选的,因为创建图像不是强制性的。但是,您希望在生产环境中拥有某种验证机制。
请求正文
POST api/search/media
这将针对 Shopware-API 运行一个带有响应的请求。
{
"filter":[
{
"type":"equals","field":"fileName","value":"yourfilename"
},{
"type":"equals","field":"fileExtension","value":"jpg"
},"field":"mediaFolderId","value":"d798f70b69f047c68810c45744b43d6f"
}
],"includes":{
"media":[
"id"
]
}
}
第一步
创建一个新的 media-file
请求正文
POST api/_action/sync
此请求将在 Shopware 中创建一个新的媒体对象。
-
media_id
的值必须是任何 UUID。我将使用这个值:94f83a75669647288d4258f670a53e69
-
customFields
属性是可选的。我只是用它来保留哈希值的引用,我可以用它来验证更改的值。 - 媒体文件夹 ID 的值是您将从 Shopware-Backend 获得的值。
{
"create-media": {
"entity": "media","action": "upsert","payload": [
{
"id": "{{media_id}}","customFields": {"hash": "{{file.hash}}"},"mediaFolderId": "{{mediaFolderId}}"
}
]
}
}
回复
响应会告诉您一切都按预期进行。
{
"success":true,"data":{
"create-media":{
"result":[
{
"entities":{
"media":[
"94f83a75669647288d4258f670a53e69"
],"media_translation":[
{
"mediaId":"94f83a75669647288d4258f670a53e69","languageId":"2fbb5fe2e29a4d70aa5854ce7ce3e20b"
}
]
},"errors":[
]
}
],"extensions":[
]
}
},"extensions":[
]
}
第 2 步
这是我们将图片上传到 Shopware 的步骤。我们将使用内容类型为 image/jpg
的变体。但是,带有 URL-Attribute 的负载也可以使用。详见官方文档。
请求正文
POST api/_action/media/94f83a75669647288d4258f670a53e69/upload?extension=jpg&fileName=yourfilename
请注意,media-id
是 URL 的一部分。还有 filename
但没有 file-extension JPG
!
这个主体非常简单,在我们的例子中没有有效负载,因为我们使用带有 Content-Type: "image/jpeg"
的上传。
如果您想使用 URL 作为资源,这将是一个有效负载:
{
"url": "<url-to-your-image>"
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。