使用多部分表单将文件上传到Salesforce

时间:2019-09-13 13:03:40

标签: python upload salesforce multipartform-data

我正在尝试将文本文件(也尝试过PDF等)上载到Salesforce。 文本文件包含“ hello world”。

这是我正在使用的代码

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')

这将导致以下请求正文似乎符合Salesforce准则: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

标题:

Content-Type:multipart / form-data; boundary =“ ---------------------------- 741e90d31eff” 接受:application / json

请求正文:

-------------------------------- 741e90d31eff 内容处置:表单数据; name =“ entity_content”; 内容类型:application / json;

{“ ContentDocumentId”:“ 0699E000000lKbLQAU”,“ ReasonForChange”:“上传大文件”,“ PathOnClient”:“ hello_world.txt”}

-------------------------------- 741e90d31eff 内容处置:表单数据; name =“ VersionData”; filename =“ hello_world.txt”; 内容类型:application / octet-stream

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

2 个答案:

答案 0 :(得分:0)

就像您的代码显示bodyEncoded = base64.b64encode(f.read())一样,该文件以base64编码形式发送。下载文件后,需要对其进行解码,以恢复其原始的“可读”值。

注意:就像您的评论所说,文件的内容为b'aGVsbG8gd29ybGQK',其中b表示base64编码的字符串,另一部分是编码的值,您也可以使用base64decode之类的在线工具进行编码,该工具将显示字符串恰好是hello world

答案 1 :(得分:0)

最后我明白了。因此,要以多部分表单数据的形式上传到Salesforce: 1.没有base64编码!!! 它需要保持为二进制 2.我的错误是我试图将字符串连接到字节。 因此,构建多部分消息的非二进制部分并将其编码为二进制:

nonbinaryPart.encode()
  1. 将字节追加到文件的二进制内容字节之后。
  2. 调用api时,以字节为单位发布数据。使用API​​时要小心,比如说默认情况下simple-salsforce可能希望将其编码为json。不需要后续编码。以二进制形式发布。