Downloading images from Salesforce URL using Python
Asked Answered
S

2

0

I have a csv of a series of contacts from Salesforce with the name, location and a URL that opens an image which I need to download

Thus far I have only been able to access my Salesforce instance using Python but am struggling to find a means of using a python package to not only open the URL, but also download the image in .jpg format and rename it with the name and location of the client.

Is there any package available that would allow my to do this. I've tried using simple-salesforce but I don't think it has this capability.

Below is the code i have so far showing a query that returns all the contacts that I need to download the URL link for (URL_Fotograf_a_Cliente__c), but I haven't got any further than that

from simple_salesforce import Salesforce, SFType, SalesforceLogin
import pandas as pd
import json
from pprint import pprint as pp

session_id, instance = SalesforceLogin(username= username, password=password, security_token=security_token, sandbox=False)

print(session_id,instance)

sf = Salesforce(instance= instance, session_id=session_id)

SOQL= "SELECT Id, Name,C_digo_del_Usuario__c,Tratamiento_IPA__c,Estado__c,Municipio__c,Comunidad__c, URL_Fotograf_a_Cliente__c FROM Contact WHERE Tratamiento_IPA__c IN ('x', 'y', 'z')"

pp(sf.query(query = SOQL))
Statued answered 22/5, 2019 at 23:50 Comment(2)
I'm using Windows as my OSStatued
Hi Jack, to keep your code readable - make sure you put it into code blocks format which will help with someone finding a solution faster for you. Also, when editing, update the question with an edit label.Aldo
P
0

simple_salesforce is the right package to access Salesforce, but the way you're using it doesn't really fit with the typical pathway. You don't need to touch SalesforceLogin or SFType; those are mostly internal. Just do

sf = Salesforce(username=username, password=password, security_token=security_token, sandbox=False)

Then, query for your records

records = sf.query("SELECT Id, Name,C_digo_del_Usuario__c,Tratamiento_IPA__c,Estado__c,Municipio__c,Comunidad__c, URL_Fotograf_a_Cliente__c FROM Contact WHERE Tratamiento_IPA__c IN ('x', 'y', 'z')")
for r in records.get("records"):
    url = record["URL_Fotograf_a_Cliente__c"]
    # Now, download the image and save it.
    # other record details are available as, e.g., `record["Name"]`

How exactly you download the image depends on whether it's hosted on Salesforce and requires authentication to access or not. I'm assuming it's simply an external URL, in which case you can use requests or your library of choice to make a request for the data and save it to disk.

Profant answered 23/5, 2019 at 0:29 Comment(0)
F
0

I've never written a line of Python code in my life but hopefully between this and David's answer you'll get somewhere.

Are the images uploaded to Salesforce? They're most likely in Attachment or File (ContentVersion) table. You could try to inspect the URL. All attachment record ids start with 00P.

I'mnot sure what your package uses, SOAP or REST API. In SOAP API you can get the body directly, as base64-encoded string. In REST API you can get a direct download URL.

SELECT Id, Name, Body, BodyLength, ContentType
FROM Attachment

Query results from developer console (REST API)

So you'd base64-decode it, rename & save...

If something was uploaded to Files then it's similar process, just different table. This blog post looks like good start: https://crmcog.com/retrieve-sfdc-attachments-using-rest/

Footrest answered 23/5, 2019 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.