[英]Downloading images from Salesforce URL using Python
我有來自Salesforce的一系列聯系人的csv,其名稱,位置和URL打開了我需要下載的圖像
到目前為止,我只能使用Python訪問我的Salesforce實例,但我很難找到一種方法,使用python包不僅可以打開URL,還可以下載.jpg格式的圖像並使用名稱和位置重命名客戶。
是否有任何可用的包可以讓我這樣做。 我嘗試過使用simple-salesforce
但我認為它不具備此功能。
下面是我到目前為止的代碼,顯示了一個查詢,它返回了我需要下載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
是訪問Salesforce的正確包,但您使用它的方式並不適合典型的途徑。 您無需觸摸SalesforceLogin
或SFType
; 那些大多是內部的。 做就是了
sf = Salesforce(username=username, password=password, security_token=security_token, sandbox=False)
然后,查詢您的記錄
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"]`
您下載映像的具體方式取決於它是否托管在Salesforce上,並且需要身份驗證才能訪問。 我假設它只是一個外部URL,在這種情況下,您可以使用requests
或您選擇的庫來請求數據並將其保存到磁盤。
我從來沒有在我的生活中寫過一行Python代碼,但希望在這個和大衛的答案之間,你會得到某個地方。
圖像是否已上傳到Salesforce? 它們最有可能出現在Attachment或File( ContentVersion )表中。 您可以嘗試檢查URL。 所有附件記錄ID以00P
開頭。
我不確定你的包使用什么,SOAP或REST API。 在SOAP API中,您可以直接獲取主體,作為base64編碼的字符串。 在REST API中,您可以獲得直接下載URL。
SELECT Id, Name, Body, BodyLength, ContentType
FROM Attachment
所以你需要對其進行base64解碼,重命名並保存...
如果將某些內容上傳到Files,則它是類似的過程,只是不同的表。 這篇博客文章看起來很不錯: https : //crmcog.com/retrieve-sfdc-attachments-using-rest/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.