2017-01-05 9 views
1
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() 

介してローカル画像をアップロードする:両方の試験においてShopify APIは、私は両方の方法試験パイソン

  • encoded = b64encode(f.read())
  • encoded = f.read()

を、出力が同じであった。

ただし、イメージは作成されませんでした。

imageimage(None)new_products.imagesが返されていることに気付きました。image(None)も返されます。

答えて

1

あなたはそうnew_product.images属性がリストImageのインスタンスではなく、ImageインスタンスでなければなりませんCLOSE-ました。また、the sourceattach_image()の場合、それらが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() 
+0

どのように2枚の絵はどうしたら? –

0

self.fake( "製品/ 632910392 /画像"、メソッド= 'POST'、本体= self.load_fixture( '画像')、ヘッダ= { 'コンテンツタイプ': 'アプリケーション/ JSON' })

画像= shopify.Image({ 'PRODUCT_ID':632910392})

image.position = 1

binary_in = base64.b64decode( "R0lGODlhbgCMAPf/APbr48VySrxTO7IgKt2qmKQdJeK8lsFjROG5p/nz7Zg3MNmnd7Q1MLNVS9GId71hSJMZIuzTu4UtKbeEeakhKMl8U8WYjfr18YQaIbAf ==")

image.attach_image(データ= binary_in、ファイル名= 'のipod-nano.png')

image.save()

関連する問題