简体   繁体   English

使用Python从Salesforce URL下载图像

[英]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 我有来自Salesforce的一系列联系人的csv,其名称,位置和URL打开了我需要下载的图像

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. 到目前为止,我只能使用Python访问我的Salesforce实例,但我很难找到一种方法,使用python包不仅可以打开URL,还可以下载.jpg格式的图像并使用名称和位置重命名客户。

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. 我尝试过使用simple-salesforce但我认为它不具备此功能。

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 下面是我到目前为止的代码,显示了一个查询,它返回了我需要下载URL链接的所有联系人( URL_Fotograf_a_Cliente__c ),但我没有比这更进一步

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. simple_salesforce是访问Salesforce的正确包,但您使用它的方式并不适合典型的途径。 You don't need to touch SalesforceLogin or SFType ; 您无需触摸SalesforceLoginSFType ; 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. 您下载映像的具体方式取决于它是否托管在Salesforce上,并且需要身份验证才能访问。 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. 我假设它只是一个外部URL,在这种情况下,您可以使用requests或您选择的库来请求数据并将其保存到磁盘。

I've never written a line of Python code in my life but hopefully between this and David's answer you'll get somewhere. 我从来没有在我的生活中写过一行Python代码,但希望在这个和大卫的答案之间,你会得到某个地方。

Are the images uploaded to Salesforce? 图像是否已上传到Salesforce? They're most likely in Attachment or File ( ContentVersion ) table. 它们最有可能出现在Attachment或File( ContentVersion )表中。 You could try to inspect the URL. 您可以尝试检查URL。 All attachment record ids start with 00P . 所有附件记录ID以00P开头。

I'mnot sure what your package uses, SOAP or REST API. 我不确定你的包使用什么,SOAP或REST API。 In SOAP API you can get the body directly, as base64-encoded string. 在SOAP API中,您可以直接获取主体,作为base64编码的字符串。 In REST API you can get a direct download URL. 在REST API中,您可以获得直接下载URL。

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

从开发人员控制台(REST API)查询结果

So you'd base64-decode it, rename & save... 所以你需要对其进行base64解码,重命名并保存...

If something was uploaded to Files then it's similar process, just different table. 如果将某些内容上传到Files,则它是类似的过程,只是不同的表。 This blog post looks like good start: https://crmcog.com/retrieve-sfdc-attachments-using-rest/ 这篇博客文章看起来很不错: https//crmcog.com/retrieve-sfdc-attachments-using-rest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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