cancel
Showing results for 
Search instead for 
Did you mean: 

Question about API: how to attach existing image to SKU?

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Question about API: how to attach existing image to SKU?

Hello,

this is my first message so I hope to be in the right section. Many of my SKU's share identical images so I would like to avoid duplicate images on my server. I do everything through the API, so my idea is to:

 

1- create the SKU without image (with a POST to /V1/products)

 

2- calculate the md5sum of the associated image before uploading it via the API

 

3- check whether this image (renamed with its md5sum) already exists on the server

 

    3a- if NO: upload the image and attach it to the SKU (with a POST to /V1/products/:sku/media)

 

    3b- if YES: attach the image already on the server to the SKU


But I can't achieve step "3b":

 

- If I PUT to /V1/products/:sku with the correct image path as "file" but an empty "content" object; I receive a response 400 with error "The image content is invalid. Verify the content and try again."

 

- If I PUT to /V1/products/:sku with the correct image path as "file" and correct "content" (base64_encoded_data, type and name); I receive a response 200 but with an empty "media_gallery_entries": [] (so the image is not attached even if the response is 200)

 

- If I POST to /V1/products/:sku/media with the correct image path as "file", correct data in "content" (base64_encoded_data, type and name) and "id" as the media/:entryId for the same image in another sku; I receive a response 400 with error "The new media gallery entry failed to save."

 

- If I POST to /V1/products/:sku/media with the correct image path as "file", correct data in "content" (base64_encoded_data, type and name) and an empty "id"; I receive a response 200 with the id of a new media/:entryId. Unfortunately, this action creates a duplicate of the image on the server (with _x appended to the filename)


Same problem is described here: https://magento.stackexchange.com/questions/275420/magento-2-rest-api-copy-image-from-another-sku

 

I am using Magento 2.3.4 and I am out of ideas.

Can someone tell me how I can "reuse" an existing image and thus avoid duplicate images on the server?

Thanks for any help!

5 REPLIES 5

Re: Question about API: how to attach existing image to SKU?

Don't get too exited OP! I am just here to second this question.

I too am trying to attach existing images to existing products.

In my case we have many invisible child products without images and I want to copy the media gallery entries from the parent to the children like for like

 

If I PUT custom_attributes ['image','short_description','small_image','thumbnail','swatch_image']

Magento actually uses the image in the backend product search but I can't for the life of me populate the actual media gallery entries.

 

In my case I don't even know how to populate "content" as the GET request on the parent does not supply the "content" object in the media gallery entries.

Re: Question about API: how to attach existing image to SKU?

I indeed got exited to see an answer but I realize you face the same problem...

I had to put my project in pause because of this stupid limitation. It just sounds crazy to me that we can't re-use an image for another SKU...

Re: Question about API: how to attach existing image to SKU?

I have gone for a bit of a poor work around at the moment...

Here is the rough python code so you might be able to work out what I did...

 

    for e in other_product['media_gallery_entries']:
        image_path = f"https://www.site_url.com/media/catalog/product{e['file']}"
        b64 = base64.b64encode(requests.get(image_path).content)
        byte_string = bytes.decode(b64)
        mime = requests.get(image_path).headers['Content-Type']
        name = e['file'].split('/')[-1]
        e.update({'content':{
            "base64_encoded_data": byte_string,
            "type": mime,
            "name": name
        }})
        e.pop('id')
        entry = {"entry":e}
        p.add_media_gallery_entry(entry) #as defined next:

    def add_media_gallery_entry(self, data_dict):
        data = json.dumps(data_dict)
        post_media_url = f"https://www.site_url.com/rest/all/V1/products/{self.sku}/media"
        auth_header = magento_auth_header()
        response = json.loads((requests.post(post_media_url,data=data,headers=auth_header)).content)
        return response

Effectively I am adding the "content" part back into the media gallery entry but...

 

It is a bad option for a few reasons, chiefly:

- Fetching then encoding and uploading a duplicate byte_string is very slow per image

- Its a silly waste of space having all those duplicates

 

But it served my purpose for now incase that helps

Re: Question about API: how to attach existing image to SKU?

Hi

Any chance you managed to solve this issue?

 

Cheers

Rob

Re: Question about API: how to attach existing image to SKU?


@spiral wrote:

I indeed got exited to see an answer but I realize you face the same problem...

I had to put my project in pause because of this jbbess stupid limitation. It just sounds crazy to me that we can't re-use an image for another SKU...


 

 

Hi, I am facing an issue do you have time to listen to my problem please? I'll be very very thankful to you