我开始学习 SalesForce 并使用 django 开发应用程序。
我需要帮助将文件上传到salesforce,为此我阅读了simple-salesforce,这有助于使用rest和SOAP api上传文件。
我的问题是如何使用 simple-salesforce 上传一个或多个文件?
我开始学习 SalesForce 并使用 django 开发应用程序。
我需要帮助将文件上传到salesforce,为此我阅读了simple-salesforce,这有助于使用rest和SOAP api上传文件。
我的问题是如何使用 simple-salesforce 上传一个或多个文件?
这是我用于上传文件的代码块。
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,Name 是短名称,body 是 base-64 编码的数据字符串。
然后将文件提交到带有标题和数据的 URL。然后我打印响应,以便我可以看到它发生。
要上传文件,您只需要simple-salesforce
完整示例,包括创建客户、联系人和案例。然后将文件附加到案例。
#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)
(你知道如何解决剩下的问题)