How to upload files to files section in admin panel via API?

Alex_Potapov
Shopify Partner
2 0 0

Hello. I am trying to upload image to files section in admin panel via python API, but I can't find any solution to do this.  Can someone help? Thank you.

0 Likes
Alex_Potapov
Shopify Partner
2 0 0

In attachment image you can see the window that I need to upload files into.

0 Likes
Jason
Shopify Expert
10037 119 1872

Shopify doesn't currently have a Files API to let you do this.
The files must be manually added via the Admin.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Stuart_Clarke
Tourist
6 0 1

Thanks for your answer Jason. However, I think there must be a workaround. I base this on the fact that the Wordpress Importer App seems to post images to the file section in its operation, how would that work?

I'm trying to write a script that imports my Instagram posts into a hidden blog with the captions as paragraphs and images throughout, I'll edit it once uploaded to make it flow better but it would give me a starting point for blogs. This really should be straight forward but at the moment I'm considering hosting the images with some thirdparty service, this really makes no sense. I handle videos by uploading them to YouTube and so they work well embedded, I'm thinking of something similar for photos if I can't upload. But if the Wordpress Imported App can manage it I'm convinced it is possible.

Any ideas would be most welcome,

Cheers,

Stu.

0 Likes
Stuart_Clarke
Tourist
6 0 1

I have a pretty bad hack, but it works (kind of).

What I do is post an article for each image, an article allows one feature image to be uploaded. This image is associated with the article but once it has been referenced to seems to remain accessible (I assume for the purpose of avoiding broken links). So once the article is posted, I get the image link and download a copy to the server so that it has been referenced and will continue to be accessible. I then delete the article.

Pretty simple and for now I am unsure how long the link stays accessible but I assume indefinitely, maybe a poor assumption and time will tell.

As I said its a hack but it does end up in the admin/file section and not as an asset. Unfortunately there is no way to view the details of the image, for my purpose this is fine but maybe not for others.

Here is my code:

fr = open("photo.jpeg", 'rb')
image = base64.b64encode(fr.read())
fr.close()

article = shopify.Article()
response = article.create({blog_id':blogID,'title':'To Be Deleted','published': 'false','body_html': '', "image":{"attachment": image}})
jsonResponse = json.loads(response.to_json().decode('utf-8'))
articleID = jsonResponse['article']['id']
src = jsonResponse['article']['image']['src']

data = requests.get(src)

print("Post ID: " + str(articleID))
print("Image address: " + src)

"""Do something with the src address, i.e. store in DB for future article post"""

article2Delete = shopify.Article.find(articleID)
article2Delete.destroy()

Alternatively you could create a dummy blog for your image articles that remain hidden and are never to be referenced to but not to be deleted, this would be a safer option. Now that I think about it I might go down this path. This way you'd always be able to edit the image and have complete access to it as well. So in that case just remove the last two lines and use the dummy blog id as your blogID.

Hope this helps someone and if there is a better solution please post.

Stu.

0 Likes
Richard_Monette
Shopify Staff
Shopify Staff
3 0 2

If you are using the GraphQL based Admin API there is now functionality for uploading images to a Shop. These such images are not directly associated to a particular Theme Asset or Product. See the docs at https://help.shopify.com/en/api/graphql-admin-api/reference/mutation/shopimageupload and https://help.shopify.com/en/api/graphql-admin-api/reference/mutation/stageduploadtargetgenerate Hope this helps!

Luke30
Shopify Partner
13 0 1

Is there any better documentation on this? For example, there is nothing on who to get write_images access to an API Key. Also, what is a "staged upload target"? There is no explanation for any of this or a working example.

0 Likes
moishe1927
Shopify Partner
1 0 1

the link is not working, is this API removed?

i also don't see shopimageupload in the GraphQL list https://help.shopify.com/en/api/graphql-admin-api/reference/mutation

Mateusz_Kaminsk
Shopify Partner
2 0 1

Hey 

 

0 Likes
gdarrigo
Tourist
4 0 2

Hi all,

I have the same need. I want to upload files in /admin/settings/files section using APIs.

I followed the instructions in this document: https://shopify.dev/tutorials/manage-product-media-with-admin-api#uploading-media-to-shopify.

Apparently all works correctly but I don't have any file in my /admin/settings/files section.

Someone can help me?

Thanks in advance

 
0 Likes