1

我有一个 Shopware 6.3 商店,需要使用集成 API 将图像迁移到它。

我应该如何构建媒体上传的主体?我需要将文件放在某个地方还是只传递链接?

我已设法通过此处的指南将新产品推入 Shopware:https ://docs.shopware.com/en/shopware-platform-dev-en/admin-api-guide/writing-entities?category=shopware-platform-dev -en/admin-api-guide#creating-entities但我不确定如何处理媒体。在本指南中,仅说明了如何在此处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",
                "format": "uuid"
            },
            "mediaFolderId": {
                "type": "string",
                "format": "uuid"
            },
            "mimeType": {
                "type": "string",
                "readOnly": true
            },
            "fileExtension": {
                "type": "string",
                "readOnly": true
            },
            "uploadedAt": {
                "type": "string",
                "format": "date-time",
                "readOnly": true
            },
            "fileName": {
                "type": "string",
                "readOnly": true
            },
            "fileSize": {
                "type": "integer",
                "format": "int64",
                "readOnly": true
            },
            "metaData": {
                "type": "object",
                "readOnly": true
            },
            "mediaType": {
                "type": "object",
                "readOnly": true
            },
            "alt": {
                "type": "string"
            },
            "title": {
                "type": "string"
            },
            "url": {
                "type": "string"
            },
            "hasFile": {
                "type": "boolean"
            },
            "private": {
                "type": "boolean"
            },
            "customFields": {
                "type": "object"
            },
            "createdAt": {
                "type": "string",
                "format": "date-time",
                "readOnly": true
            },
            "updatedAt": {
                "type": "string",
                "format": "date-time",
                "readOnly": true
            },
            "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",
                "entity": "user"
            },
            "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 请求时使用它的 ID?我可以只指定 URL,Shopware 会将图像本身下载到文件夹中还是继续指向我使用的 URL。我需要将图像存放在 Shopware 中。

我从 URL 下载图像并将它们推送到 Shopware 没有问题,但我不知道如何使用它的 API(有很多图像,需要批量完成)。

4

2 回答 2

8

一种可能的解决方案:

FIRST:创建一个新的媒体 POST/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 模式获得:[your-domain/localhost]/api/v3/_info/openapi3.json

也可以在产品创建期间通过一个请求设置所有媒体和封面和封面ID。因此,设置产品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);
于 2020-09-04T09:48:15.763 回答
4

这适用于 Shopware 6.4

作为一般建议,这取决于。自 6.4 以来,API 发生了一些变化,并且在https://shopware.stoplight.io/docs/admin-api/docs/guides/media-handling.md上还有一个官方文档。

但是,我认为拥有一个现实生活中的例子总是更容易一些。我在我们的生产环境中所做的基本上就是这些步骤。

  1. (可选)检查媒体对象是否存在
  2. media-file使用端点创建对象GET /media-files/
  3. 如果存在,则使用新的 media-id 引用上传图像。

让我们假设文件名是yourfilename.jpg. 您还需要一个media-folder-id,它将引用 Shopware 中的图像文件夹。这可以通过 Shopware 在 Shopware 中获得Admin > Content > Media > Product Media

步骤 0

在将图像上传到 Shopware 之前,您要确保该图像不存在,以便您可以跳过它。

此步骤是可选的,因为创建图像不是强制性的。但是,您希望在生产环境中拥有某种验证机制。

请求体

POST api/search/media

这将针对 Shopware-API 运行一个带有响应的请求。

{
   "filter":[
      {
         "type":"equals",
         "field":"fileName",
         "value":"yourfilename"
      },
      {
         "type":"equals",
         "field":"fileExtension",
         "value":"jpg"
      },
      {
         "type":"equals",
         "field":"mediaFolderId",
         "value":"d798f70b69f047c68810c45744b43d6f"
      }
   ],
   "includes":{
      "media":[
         "id"
      ]
   }
}

第1步

创建一个新的media-file

请求体

POST api/_action/sync

此请求将在 Shopware 中创建一个新的媒体对象。

  1. 的值media_id必须是任何 UUID。我将使用这个值:94f83a75669647288d4258f670a53e69
  2. customFields属性是可选的。我只是用它来保留哈希值的引用,我可以用它来验证更改的值。
  3. 媒体文件夹 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 的步骤。我们将使用带有 content-type 的变体image/jpg。但是,带有 URL 属性的有效负载也可以工作。详见官方文档。

请求体

POST api/_action/media/94f83a75669647288d4258f670a53e69/upload?extension=jpg&fileName=yourfilename

请注意,这media-id是 URL 的一部分。还有filename但没有file-extension JPG

这个 body 非常简单,在我们的例子中没有有效负载,因为我们使用Content-Type: "image/jpeg".

如果您想使用 URL 作为资源,这将是一个有效负载:

{
  "url": "<url-to-your-image>"
}
于 2021-07-30T13:30:34.910 回答