如何使用Python从Salesforce获取文件 [英] How to get files from Salesforce using Python
问题描述
我正在使用Python/Beatbox访问Salesforce案例.
I am using Python/Beatbox to access Salesforce cases.
service = beatbox.PythonClient() # instantiate the object
service.login(...) # login using your sf credentials
query_result = service.query("SELECT Id, AccountId, CaseNumber FROM Case WHERE Id='xyz' ")
我对特定情况感兴趣:
print query_result[0].Id
获取附件...
att_result = service.query("SELECT Id, ContentType, Name FROM Attachment WHERE ParentId= '" + str(query_result[0].Id) + "'")
到目前为止,效果很好.现在我要下载上传到案子的文件. 我应该查询什么?我尝试了以下操作,但该操作始终为空..但是我确定此案例中包含文件和附件.
So far the results are good. Now I want to download the files uploaded to the case. What should be my query? I tried following and its always empty..But Im sure the case has files as well as attachments..
doc_result = service.query("SELECT Id, ContentDocumentId, Title FROM AttachedContentDocument WHERE Id= '" + str(query_result[0].Id) + "'")
我也尝试了文档对象,但仍然没有成功.感谢您的帮助.
I also tried the document object and still no success. I appreciate your help.
推荐答案
在任何Salesforce API中,通过一个请求最多只能获得一个附件.为确保您没有附件,请先获取Attachment.Id,然后循环获取正文
You can get not more than one Attachment by one request in any Salesforce API. To be sure that you don't an attachment, get the Attachment.Id first, then get the body by a loop
SELECT Id FROM Attachment WHERE ParentId = '...'
for ...
SELECT Body FROM Attachment WHERE Id = '...'"
A) SOAP API(Beatbox):将附件作为普通的长base64编码字段获取.
A) SOAP API (Beatbox): Get the Attachment as a normal long base64encoded field.
import base64
ret = service.query("SELECT Id, Body FROM Attachment WHERE Id = '...'")
blob = base64.b64decode(ret)[0]['Body'])
该查询应该期望一行,因为如果正文"被输出,则输出被限制为一行.字段存在.
The query should expect one row because the output is restricted to one row if the "Body" field is present.
B)如果使用 REST API( simple-salesforce 包)使用相同的查询,则Body
或VersionData
字段的值是'/services/data/v40.0/sobjects/Attachment/<object_id>/Body'
形式的URL,可以通过GET请求下载.
B) If REST API is used (simple-salesforce package) with the same queries, the value of Body
or VersionData
fields is a URL of the form '/services/data/v40.0/sobjects/Attachment/<object_id>/Body'
which can be downloaded by GET request.
对二进制大对象有用的对象是附件,文档和ContentVersion.
这些有用的查询允许通过 SOAP API(Beatbox)将二进制大对象(附件或文档)作为普通的长字段获取. ContentVersion
对象允许存储相同数据的更多版本. Attachment
有一个父对象. Document
没有任何父对象.有用的查询:(阅读上述API的限制)
Objects useful for Binary Large OBjects are Attachment, Document and ContentVersion.
These useful queries allow to get a binary large object (Attachment or Document) as a normal long field by SOAP API (Beatbox). The ContentVersion
object allows to store more versions of the same data. Attachment
has a parent object. Document
is without any parent object. Useful queries: (read restrictions by API above)
SELECT Id, Body FROM Attachment WHERE ParentId = '...'
SELECT Id, Body FROM Document WHERE ParentId = '...'
SELECT Id, VersionData FROM ContentVersion WHERE ...
这篇关于如何使用Python从Salesforce获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!