【发布时间】:2020-01-15 08:42:16
【问题描述】:
我正在尝试将文本文件(也尝试过 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
标题:
内容类型:multipart/form-data;边界="----------------------------741e90d31eff" 接受:application/json
请求正文:
------------------741e90d31eff 内容处置:表单数据;名称=“实体内容”; 内容类型:application/json;
{"ContentDocumentId": "0699E000000lKbLQAU", "ReasonForChange": "大文件上传", "PathOnClient": "hello_world.txt"}
------------------741e90d31eff 内容处置:表单数据;名称="版本数据";文件名="hello_world.txt"; 内容类型:application/octet-stream
b'aGVsbG8gd29ybGQK' ------------------------------741e90d31eff--
【问题讨论】:
-
在上传的文件中,我看到的不是“hello world”,而是 b'aGVsbG8gd29ybGQK
-
您好,我也在处理将文件传输到 salesforce。但是我正在处理大型文件。在我的情况下,我想读取流中的文件并将多部分标头动态附加到请求正文中。我看到 bodyEncoded = base64.b64encode(f.read()) 可以帮助您读取内存中的所有文件内容,您有什么建议让这也适用于使用文件流吗?
标签: python upload salesforce multipartform-data