通过Shopware 6 API添加图像

如何解决通过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 上还提供了官方文档。

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

  1. (可选)检查媒体对象是否存在
  2. 使用端点 media-file 创建一个 GET /media-files/ 对象
  3. 如果存在,则使用新的 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 中创建一个新的媒体对象。

  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 的步骤。我们将使用内容类型为 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 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res