1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Upload a file to Salesforce using multipart-form

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to upload a text file (also tried PDF, etc) to Salesforce. Text file contains 'hello world'.

    Here is the code I am using

    def putFile(sf, libname, filen):
    file_name=os.path.basename(filen)
    libId=libraryExists(sf, libname)
    contentDocumentId = getContentDocumentId(sf, libname, file_name)
    if not libId:
    print(f"Provided library '{libname}' does not exists")
    return

    with open(filen, "rb") as f:
    bodyEncoded = base64.b64encode(f.read())

    boundary = '----------------------------741e90d31eff'
    headers = {
    'Content-Type' : 'multipart/form-data; boundary=' + boundary
    }

    nonBinaryPart = '--'+boundary+'\nContent-Disposition: form-data; name="entity_content";\n'
    nonBinaryPart += 'Content-Type: application/json;\r\n\r\n'
    nonBinaryPart += json.dumps({
    "ContentDocumentId" : contentDocumentId,
    "ReasonForChange" : "Large file upload",
    "PathOnClient" : file_name
    })
    nonBinaryPart += '\r\n\r\n'

    header = '--'+boundary+'\nContent-Disposition: form-data; name="VersionData"; filename="'+file_name+'";\nContent-Type: application/octet-stream\r\n\r\n'
    footer = '--'+boundary+'--'
    headerEncoded = header
    last4Bytes = bodyEncoded[len(bodyEncoded)-4:len(bodyEncoded)]
    print(type(last4Bytes))
    print(last4Bytes)
    if last4Bytes.endswith(b'=='):
    last4Bytes = last4Bytes[0:2] + b'0K'
    bodyEncoded = bodyEncoded[0:len(bodyEncoded)-4] + last4Bytes
    footerEncoded = footer
    reqBody = headerEncoded+str(bodyEncoded)+footerEncoded
    elif last4Bytes.endswith(b'='):
    print('Ends with =')
    last4Bytes = last4Bytes[0:3] + b'N'
    bodyEncoded = bodyEncoded[0:len(bodyEncoded)-4] + last4Bytes
    footer = '\n' + footer;
    footerEncoded = footer
    reqBody = headerEncoded+str(bodyEncoded)+footerEncoded
    else:
    footer = '\r\n' + footer
    footerEncoded = footer
    reqBody = headerEncoded+str(bodyEncoded)+footerEncoded

    reqBody = nonBinaryPart + reqBody

    print('==================================================')
    print(reqBody)
    print('==================================================')

    res = sf.contentVersion.create(reqBody, headers)

    print(res)

    print('Now downloading it...')
    os.system('rm -f ' + filen + '_downloaded')
    getFile(sf, contentDocumentId, filen + '_downloaded', './' )
    print('Downloaded.')


    os.system('md5sum ' + filen)
    os.system('md5sum ' + filen + '_downloaded')


    This results in following request body which seems is according to Salesforce guidelines: https://developer.salesforce.com/do.../api_rest/dome_sobject_insert_update_blob.htm

    Headers:

    Content-Type: multipart/form-data; boundary="----------------------------741e90d31eff" Accept: application/json

    Request body:

    ------------------------------741e90d31eff Content-Disposition: form-data; name="entity_content"; Content-Type: application/json;

    {"ContentDocumentId": "0699E000000lKbLQAU", "ReasonForChange": "Large file upload", "PathOnClient": "hello_world.txt"}

    ------------------------------741e90d31eff Content-Disposition: form-data; name="VersionData"; filename="hello_world.txt"; Content-Type: application/octet-stream

    b'aGVsbG8gd29ybGQK' ------------------------------741e90d31eff--

    Continue reading...

Compartilhe esta Página