Downloading images from Salesforce URL using Python

℡╲_俬逩灬. 提交于 2021-02-17 05:14:42

问题


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

回答1:


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.




回答2:


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

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/



来源:https://stackoverflow.com/questions/56266375/downloading-images-from-salesforce-url-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!