Upload multiple files using simple-salesforce python
<p>I started learning SalesForce and developing apps using django.</p>

<p>I need assistance with uploading a file to salesforce, For that I read <a href="https://github.com/simple-salesforce/simple-salesforce" rel="nofollow">simple-salesforce</a> and <a href="https://gist.github.com/wadewegner/df609a495df2e4bd7a07" rel="nofollow">this</a> that help to upload file using rest and SOAP api.</p>

<p>My question is how do I upload one or more files using simple-salesforce?</p>

<p>Here is the code block I use for uploading files.</p>

<pre><code>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)
</code></pre>

<p>Basically, to send the file, you need to use the requests module and submit the file via a post transaction.  The post transaction requires the URL to which the request is sent, the header information, and the data.</p>

<p>Here, sf is the instance of returned by the simple-salesforce initialization.  Since my instance uses custom domains, I had to create my own function in simple-salesforce to handle that; I call it get_forced_url().  Note: The URL is may be different for you depending on which version you are using [the v29.0 portion may change].</p>

<p>Then I set up my bearer and header.</p>

<p>The next thing is a loop that submits a new attachment for each attachment in a map from Parent ID to the File I wish to upload.  This is important to note, attachments must have a Parent Object so you need to know the ParentId.  For each attachment, I blank out the body, create a long and short name for the attachment.  Then the important part.  On attachments, the actual data of the file is stored as a base-64 binary array.  So the file must be opened as binary, hence the "rb" and then encoded to base-64.</p>

<p>Once the file has been parsed to base-64 binary, I build my json string where ParentId is the object ID of the parent object, the Name is the short name, and the body is the base-64 encoded string of data.</p>

<p>Then the file is submitted to the URL with the headers and data.  Then I print the response so I could watch it happening.</p>

python django salesforce