使用 simple-salesforce python 上传多个文件

Upload multiple files using simple-salesforce python

我开始学习 SalesForce 并使用 django 开发应用程序。

我需要帮助将文件上传到 salesforce,为此我阅读了 simple-salesforce and this 帮助使用 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)

基本上,要发送文件,您需要使用请求模块并通过 post 事务提交文件。 post 事务需要请求发送到的 URL、header 信息和数据。

这里sf是simple-salesforce初始化返回的实例。由于我的实例使用自定义域,因此我必须在 simple-salesforce 中创建自己的函数来处理它;我称之为 get_forced_url()。注意:URL 可能因您使用的版本而异 [v29.0 部分可能会更改]。

然后我设置我的承载和header。

接下来是一个循环,它为映射中的每个附件提交一个新附件,从 Parent ID 到我要上传的文件。请务必注意,附件必须具有 Parent Object,因此您需要知道 ParentId。对于每个附件,我将 body 空白,为附件创建一个长名称和短名称。然后是重要的部分。在附件中,文件的实际数据存储为 base-64 二进制数组。因此文件必须以二进制形式打开,因此 "rb" 然后编码为 base-64.

一旦文件被解析为 base-64 二进制文件,我将构建我的 json 字符串,其中 ParentId 是 parent [=] 的 object ID 35=],Name为简称,body为base-64编码的数据串。

然后将文件与 header 和数据一起提交给 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'})

以及如何将 'one-file' 示例更改为多个文件

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)

(你知道如何修复其余部分)