One of the projects I'm working on right now requires being able to upload product images to Shopify. These images
are sourced via Dropbox links, which look something like this: https://www.dropbox.com/s/[some id]/[filename].jpg?dl=0
.
Shopify has a feature which allows for API image uploads via URL or via a Base64 encoded attachment. However, when you try and link a Dropbox URL, you will likely encounter an error since Dropbox file extensions are "faux", and aren't the actual location of the image you want.
Searching for a solution to this yeilds little help. The common solution from Shopify indicates that you'd have to download
the images via your URL (to do this you must pass ?dl=1
), and either encode it to base64 or host it somewhere on your own,
then attach the image to your API request.
What a pain in the ass.
Doing a little more poking around, I discovered that Dropbox has a feature to allow linking of a raw image. Yay! This is more on target for what we need.
Dropbox structures raw image links as such: https://www.dropbox.com/s/[some id]/[filename].jpg?raw=1
, where ?raw=1
is our
key parameter. But this fails again with Shopify! How?!
Again, this isn't the actual location of the image. Let's explore with some Python requests
:
import requests
r = requests.head('https://www.dropbox.com/s/[some id]/[filename].jpg?raw=1', allow_redirects=False)
print(r.status_code)
print(r.headers['location'])
Running this code through our interpreter, we notice that the ?raw=1
link is 301 redirecting to the same link, but with
/raw/
preprended before our file id.
Now lets run the same code, but instead this time we'll add the /raw/
into our link.
import requests
r = requests.head('https://www.dropbox.com/s/raw/[some id]/[filename].jpg', allow_redirects=False)
print(r.status_code)
print(r.headers['location'])
Boom 302! We've now discovered the true location of our file. The long location URL of this redirect is passable to Shopify, and we can now upload our products directly via URL, just by performing a small HEAD request to find the true location.
Thank you!