Tyron Pretorius
Tyron Pretorius

Reputation: 21

Uploading a file to Marketo using a Dropbox link to the file

I am using the python code below to upload a file to our marketing automation tool Marketo. It works when I have the file downloaded locally (/home/tyron/Downloads/yoda.jpeg) but I would like to be able to upload a file straight from Dropbox to Marketo without having to download it inbetween because I will be hosting this code in Zapier.

What is the best way to do this upload when given a link to the dropbox file?

import requests

url = "xxxxxx/rest/asset/v1/files.json"

payload = {'name': 'test',
'file': '/home/tyron/Downloads/yoda.jpe',
'folder': '{"id":367,"type":"Folder"}'}
files = [
  ('file', open('/home/tyron/Downloads/yoda.jpeg','rb'))
]
headers = {
  'Authorization': 'Bearer yyyyyyyy'
}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))

Upvotes: 0

Views: 183

Answers (1)

Tyron Pretorius
Tyron Pretorius

Reputation: 21

Thanks to Greg's suggestion in the comments I was able to upload the file to Marketo using the temporary link endpoint from dropbox.

Python Code to Get Dropbox Endpoint

import re
from urllib.parse import urlparse
from urllib.parse import unquote
import requests
import json

link = "https://www.dropbox.com/preview/Telnyx/...../file"

path  = urlparse(link).path
path = re.search("(/Telnyx.*$)",path).group(0).lower()
path = unquote(path)

print(path)

url = "https://api.dropboxapi.com/2/files/get_temporary_link"
payload = {"path": path}

headers = {
  'Authorization': 'Bearer xxxxxxxxxxxxxx',
  'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data = json.dumps(payload))

print(response)

link = re.search('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', response.text).group(0)

return {'id': 1234, 'rawHTML': link}

Python Code to Upload to Marketo

import requests
import urllib
import json
import re


path = "https://dl.dropboxusercontent.com/apitl/1/......." #returned from previous function

f = urllib.request.urlopen(path)
mime_type = f.info().get_content_type()

f_ext = f.info().get_content_type().split("/")[1]

url = "https://xxx-xxx-xxx.mktorest.com/rest/asset/v1/files.json"

f_name = 'Name.' + f_ext

payload = {
'folder': '{"id":xxxx","type":"Folder"}'
}

headers = {
  'Authorization': 'Bearer xxxxxxxx'
}

files = {'file': (f_name, f, mime_type)}

response = requests.request("POST", url, headers=headers, data = payload, files = files)

print(response.text.encode('utf8'))
f_url = re.search('"url":"(.*)","folder',response.text).group(1)

return {'id': 1234, 'rawHTML': f_url}

Upvotes: 0

Related Questions