简体   繁体   中英

Downloading images from Salesforce URL using Python

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))

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.

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

从开发人员控制台(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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM