Shopify API 通过 python 上传本地图片
Shopify API upload a local image via python
import shopify
from base64 import b64encode
#omitted the shopify key
shopify.ShopifyResource.set_site(shop_url)
path = "product.jpg"
new_product = shopify.Product()
new_product.title = "title"
new_product.body_html = "This is a test"
image = shopify.Image()
with open(path, "rb") as f:
filename = path.split("/")[-1:][0]
#encoded = b64encode(f.read()) (I tried this one as well)
encoded = f.read()
image.attach_image(encoded, filename=filename)
new_product.images = image
new_product.save()
我测试了两种方法:
encoded = b64encode(f.read())
encoded = f.read()
在这两个测试中,输出是相同的:
产品已成功创建,但是没有图像。
我还注意到 image
returns image(None)
和 new_products.images
returns image(None)
。
你太接近了 - new_product.images
属性必须是 Image
个实例的 list,而不是 Image
个实例。此外,如果您查看 attach_image()
的 the source,您可以看到他们为您进行了 base64 编码。
import shopify
#omitted the shopify key
shopify.ShopifyResource.set_site(shop_url)
path = "product.jpg"
new_product = shopify.Product()
new_product.title = "title"
new_product.body_html = "This is a test"
image = shopify.Image()
with open(path, "rb") as f:
filename = path.split("/")[-1:][0]
encoded = f.read()
image.attach_image(encoded, filename=filename)
new_product.images = [image] # Here's the change
new_product.save()
self.fake("products/632910392/images",方法='POST',body=self.load_fixture('image'),headers= {'Content-type': 'application/json'})
图片=shopify.Image({'product_id':632910392})
image.position = 1
binary_in=base64.b64解码("R0lGODlhbgCMAPf/APbr48VySrxTO7IgKt2qmKQdJeK8lsFjROG5p/nz7Zg3MNmnd7Q1MLNVS9GId71hSJMZIuzTu4UtKbeEeakhKMl8U8WYjfr18YQaIbAf==")
image.attach_image(数据=binary_in, 文件名='ipod-nano.png')
image.save()
import shopify
from base64 import b64encode
#omitted the shopify key
shopify.ShopifyResource.set_site(shop_url)
path = "product.jpg"
new_product = shopify.Product()
new_product.title = "title"
new_product.body_html = "This is a test"
image = shopify.Image()
with open(path, "rb") as f:
filename = path.split("/")[-1:][0]
#encoded = b64encode(f.read()) (I tried this one as well)
encoded = f.read()
image.attach_image(encoded, filename=filename)
new_product.images = image
new_product.save()
我测试了两种方法:
encoded = b64encode(f.read())
encoded = f.read()
在这两个测试中,输出是相同的:
产品已成功创建,但是没有图像。
我还注意到 image
returns image(None)
和 new_products.images
returns image(None)
。
你太接近了 - new_product.images
属性必须是 Image
个实例的 list,而不是 Image
个实例。此外,如果您查看 attach_image()
的 the source,您可以看到他们为您进行了 base64 编码。
import shopify
#omitted the shopify key
shopify.ShopifyResource.set_site(shop_url)
path = "product.jpg"
new_product = shopify.Product()
new_product.title = "title"
new_product.body_html = "This is a test"
image = shopify.Image()
with open(path, "rb") as f:
filename = path.split("/")[-1:][0]
encoded = f.read()
image.attach_image(encoded, filename=filename)
new_product.images = [image] # Here's the change
new_product.save()
self.fake("products/632910392/images",方法='POST',body=self.load_fixture('image'),headers= {'Content-type': 'application/json'})
图片=shopify.Image({'product_id':632910392})
image.position = 1
binary_in=base64.b64解码("R0lGODlhbgCMAPf/APbr48VySrxTO7IgKt2qmKQdJeK8lsFjROG5p/nz7Zg3MNmnd7Q1MLNVS9GId71hSJMZIuzTu4UtKbeEeakhKMl8U8WYjfr18YQaIbAf==")
image.attach_image(数据=binary_in, 文件名='ipod-nano.png')
image.save()