通过 Shopware 6 API 添加图片 [英] Add images via Shopware 6 API

查看:36
本文介绍了通过 Shopware 6 API 添加图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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 和图像位置).

实体架构将媒体描述为:

 媒体":{名称":媒体",可翻译":[替代",标题",自定义字段"],属性":{身份证":{类型":字符串",格式":uuid"},用户 ID":{类型":字符串",格式":uuid"},mediaFolderId":{类型":字符串",格式":uuid"},mimeType":{类型":字符串",只读":真},文件扩展名":{类型":字符串",只读":真},上传时间":{类型":字符串",格式":日期时间",只读":真},文件名":{类型":字符串",只读":真},文件大小":{类型":整数",格式":int64",只读":真},元数据":{类型":对象",只读":真},媒体类型":{类型":对象",只读":真},alt":{类型":字符串";},标题":{类型":字符串";},网址":{类型":字符串";},hasFile":{类型":布尔型"},私人":{类型":布尔型"},自定义字段":{类型":对象"},创建于":{类型":字符串",格式":日期时间",只读":真},更新时间":{类型":字符串",格式":日期时间",只读":真},翻译":{类型":对象"},标签":{类型":数组",实体":标签"},缩略图":{类型":数组",实体":media_thumbnail";},用户":{类型":对象",实体":用户"},类别":{类型":数组",实体":类别";},产品制造商":{类型":数组",实体":product_manufacturer";},产品媒体":{类型":数组",实体":product_media"},头像用户":{类型":对象",实体":用户"},媒体文件夹":{类型":对象",实体":media_folder"},属性组选项":{类型":数组",实体":property_group_option"},邮件模板媒体":{类型":数组",实体":mail_template_media"},documentBaseConfigs":{类型":数组",实体":document_base_config"},运输方式":{类型":数组",实体":shipping_method";},支付方式":{类型":数组",实体":payment_method";},产品配置器设置":{类型":数组",实体":product_configurator_setting"},订单行项目":{类型":数组",实体":order_line_item";},cmsBlocks":{类型":数组",实体":cms_block"},cmsSections":{类型":数组",实体":cms_section"},cmsPages":{类型":数组",实体":cms_page"},文件":{类型":数组",实体":文件"}}},

但不清楚哪些字段是关键的.我是否需要先创建 product-media 文件夹,然后在向媒体端点发出 POST 请求时使用它的 ID?我可以只指定 URL 并将 Shopware 将图像本身下载到一个文件夹或继续指向我使用的 URL.我需要将图片放在 Shopware 中.

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

解决方案

一种可能的解决方案:

首先:创建一个新的媒体 POST /api/{apiVersion}/media?_response=true

第二个:上传图片"/api/{apiVersion}/_action/media/{mediaId}/upload?extension={extension}&fileName={imgName}&_response=true

更多信息可以在这里找到:https://forum.shopware.com/讨论/评论/278603/#Comment_278603

如果图像用于产品,请使用端点 POST /api/{apiVersion}/product-media 并设置 coverId

可通过 OpenAPI 架构获得所有路由的完整列表:[your-domain/localhost]/api/v3/_info/openapi3.json

也可以设置所有媒体和封面&通过一个请求创建产品期间的coverId.因此,设置产品Cover和产品Media

<代码>{coverId":3d5ebde8c31243aea9ecebb1cbf7ef7b",productNumber":SW10002",active":true,name":Test",描述":fasdf",媒体":[{productId":94786d894e864783b546fbf7c60a3640",mediaId":084f6aa36b074130912f476da1770504",位置":0,id":3d5ebde8c31243aea9ecebb1cbf7ef7b"},{productId":94786d894e864783b546fbf7c60a3640",mediaId":4923a2e38a544dc5a7ff3e26a37ab2ae",位置":1,id":600999c4df8b40a5bead55b75efe688c"}],id":94786d894e864783b546fbf7c60a3640"}

请记住通过检查以下示例来检查不记名令牌是否有效:

if (JwtToken.ValidTo >= DateTime.Now.ToUniversalTime() - new TimeSpan(0, 5, 0)){返回 Client.Get(request);}别的{//通过新的身份验证刷新令牌IntegrationAuthenticator(this.key, this.secret);}返回 Client.Get(request);

I have a Shopware 6.3 shop and need to migrate images to it using the integration API.

How should I construct a body for a media upload? Do I need to put a file somewhere or just pass in the link?

I have managed to push new products into Shopware via guide here: https://docs.shopware.com/en/shopware-platform-dev-en/admin-api-guide/writing-entities?category=shopware-platform-dev-en/admin-api-guide#creating-entities but I am not sure how to handle media. In this guide it is only explained how to create links between already uploaded media files to products in here https://docs.shopware.com/en/shopware-platform-dev-en/admin-api-guide/writing-entities?category=shopware-platform-dev-en/admin-api-guide#media-handling but no examples as to how to actually push the media files.

I have URL's for each image I need (in the database, along with produc id's and image positions).

The entity schema describes media as:

    "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"
            }
        }
    },

but it is not clear what fields are crucial. Do I need to create product-media folder first and then use it's id when making a POST request to media endpoint? Can I just specify the URL and will Shopware download the image itself to a folder or keep pointing to the URL I have used. I need to house the images inside the Shopware.

There is no problem for me to download the images from the URL and push them to Shopware but I am not sure how to use the API for it (there is a lot of images and they need to be done in bulk).

解决方案

One possible solution:

FIRST: create a new media POST /api/{apiVersion}/media?_response=true

SECOND: "Upload Image" /api/{apiVersion}/_action/media/{mediaId}/upload?extension={extension}&fileName={imgName}&_response=true

more information can be found here: https://forum.shopware.com/discussion/comment/278603/#Comment_278603

In CASE images are for products use the endpoint POST /api/{apiVersion}/product-media and set the coverId

A complete listing of all routes is available via the OpenAPI schema: [your-domain/localhost]/api/v3/_info/openapi3.json

It's also possible to set all the media and the cover & coverId during product creation by one request. Therefore, set the product Cover and product 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"
}

Keep in mind to check if the bearer token is valid by checking for example like this:

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 API 添加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆