I am trying to upload local images to a Shopify product via node.js and shopify-api-node.
This is the part of the code, where I am uploading the images.
product.images.forEach(async image => {
Logger.toFile("product.image", image);
try {
await this._api.productImage.create(shopifyProduct.id, {
src: image.src
});
} catch (error) {
Logger.error("Error uploading images.");
this._handleErrors(error);
}
})
If I use an external URL for image.src
, everything works just fine.
Here is the logger with working external URL:
[2022-02-01 16:44:38 UTC][Products] product.image {
src: 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/Image_created_with_a_mobile_phone.png/220px-Image_created_with_a_mobile_phone.png',
position: 1
}
And the not working local src:
product.image {
src: '/Users/radoslav/Server/projects/tmp/pics/264-426.png',
position: 1
}
The path is correct and the image 264-426.png
exists.
Any suggestions, what I am doing wrong, or is it possible to upload an image to Shopify from a local link?
You are trying to upload an image from a local relative path which is wrong at the moment.
But I'm not sure if the API handles local images with the proper path and even if you use absolute path.
The best approach will be to read the image with Node.js and convert it to Base64
and pass it that way. You can refer to the docs here: https://shopify.dev/api/admin-rest/2021-07/resources/product-image#[post]/admin/api/2021-07/products/{product_id}/images.json
In addition please note that async/await
doesn't work in a forEach
function. So at the moment you are stacking all the uploads at the same time and the API will not be happy.
PS: Give my regards to Zoro/Marto & Evgeni. ;)