我开始学习SalesForce并使用django开发应用程序。
我需要将文件上传到salesforce的帮助,因为我阅读了simple-salesforce和this,它们有助于使用rest和SOAP api上传文件。
我的问题是如何使用simple-salesforce上传一个或多个文件?
发布于 2016-09-20 01:42:24
下面是我用来上传文件的代码块。
def load_attachments(sf, new_attachments):
'''
Method to attach the Template from the Parent Case to each of the children.
@param: new_attachments the dictionary of child cases to the file name of the template
'''
url = "https://" + sf.get_forced_url() + ".my.salesforce.com/services/data/v29.0/sobjects/Attachment/"
bearer = "Bearer " + sf.get_session_id()
header = {'Content-Type': 'application/json', 'Authorization': bearer}
for each in new_attachments:
body = ""
long_name = str(new_attachments[each]).split(sep="\\")
short_name = long_name[len(long_name) - 1]
with open(new_attachments[each], "rb") as upload:
body = base64.b64encode(upload.read())
data = json.dumps({
'ParentId': each,
'Name': short_name,
'body': body
})
response = requests.post(url, headers=header, data=data)
print(response.text)基本上,要发送文件,您需要使用requests模块并通过post事务提交文件。post事务需要将请求发送到的URL、标头信息和数据。
这里,sf是simple-salesforce初始化返回的实例。因为我的实例使用自定义域,所以我必须在simple-salesforce中创建自己的函数来处理它;我称之为get_forced_url()。注意:根据您使用的版本不同,URL可能会有所不同,v29.0部分可能会发生变化。
然后我设置我的持有者和标题。
下一件事是一个循环,它为从父ID到我希望上传的文件的映射中的每个附件提交一个新附件。注意这一点很重要,附件必须有一个父对象,所以你需要知道ParentId。对于每个附件,我清空正文,为附件创建一个长名称和短名称。然后是最重要的部分。在附件中,文件的实际数据以base-64二进制数组的形式存储。因此,该文件必须以二进制格式打开,因此是"rb“,然后编码为base-64。
一旦文件被解析为base-64二进制文件,我将构建我的json字符串,其中ParentId是父对象的对象ID,名称是短名称,主体是base-64编码的数据字符串。
然后,将文件连同头部和数据一起提交到URL。然后我打印响应,这样我就可以看到它的发生。
发布于 2021-11-11 10:41:29
要上传文件,您只需使用simple-salesforce
完整示例,包括创建帐户、联系人和案例。然后将文件附加到Case。
#Create Account, Contact and Case
AccountID = sf.Account.create({'Name':'Test12','Phone':'987654321'})["id"]
ContactID = sf.Contact.create({'LastName':'Smith2','Email':'example3@example.com'})["id"]
CaseID = sf.Case.create({'AccountId':AccountID,'ContactId':ContactID,'Description':'Test4321','Subject':'Test4321'})
#Convert image to Base64
import json, base64
with open('test1.png', mode='rb') as file:
img = file.read()
image = base64.encodebytes(img).decode('utf-8')
#The simple example
sf.Attachment.create({'ParentId': CaseID["id"],'Name':'TestFile1','body': image,'ContentType':'image/png'})以及如何将“一个文件”示例更改为多个文件
sf.bulk.Attachment.insert([
{'ParentId': CaseID["id"],'Name':'TestFile2','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile3','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile4','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile5','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile6','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile7','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile8','body': image,'ContentType':'image/png'},
{'ParentId': CaseID["id"],'Name':'TestFile9','body': image,'ContentType':'image/png'}],batch_size=1000,use_serial=True)(您知道如何修复其余部分)
https://stackoverflow.com/questions/39529028
复制相似问题