【发布时间】:2021-09-21 16:15:39
【问题描述】:
下面的代码在 10 MB 上运行完美,但是当我尝试上传超过 20 MB 的文件时,它会引发错误。
private string TOKEN = 'access_token_of_dropbox_app';
string folderPathWithFileName;
for(ContentVersion contentVersionRec : [SELECT PathOnClient, VersionData
FROM contentversion
WHERE contentdocumentId IN : contentDocumentIdSet]){
folderPathWithFileName = '/Root/'+PathOnClient;//PathOnClient contains file name with extension
//like imagefile.png
system.debug('###folderPathWithFileName=>'+folderPathWithFileName);
Http http = new Http();
HttpRequest httpReq = new HttpRequest();
httpReq.setEndpoint('https://content.dropboxapi.com/2/files/upload');
httpReq.setMethod('POST');
httpReq.setHeader('Authorization','Bearer ' + TOKEN);
httpReq.setHeader('Content-Type','application/octet-stream');
httpReq.setHeader('Dropbox-API-Arg','{"path":"'+folderPath+'","mode":{".tag":"add"}}');
httpReq.setBodyAsBlob(contentVersionRec.VersionData);
HttpResponse httpResponse = http.send(httpReq);
System.debug('res###' + httpResponse);
System.debug('res.getBody()###'+httpResponse.getBody());
if (httpResponse.getStatusCode() == 200) {
System.debug('##Congratulations File Uploaded successfully :) =>'+httpResponse.getBody());
}else{
//Handle code here when file not uploaded.
}
}
当我尝试上传 20 MB 文件时,它会引发错误:
System.CalloutException:请求大小为 12001280 时超出最大大小限制 12000000
上面的代码在 queueable(Async) apex 中运行,并且该 queueable apex 从触发器中触发。所以我能够成功上传 10 MB 的文件。我也知道 ASYNC apex 中堆大小为 12 MB 的限制。 但想知道有没有办法将文件从 salesforce 上传到超过 10 MB 的 Dropbox。我想从 apex HTTP 请求上传最大 100 MB 的文件到 Dropbox。
注意:dropbox API 还允许我们以块的形式上传数据,这意味着我们可以使用 apex HTTP 请求以块的形式向 dropbox 发送数据。但挑战在于,首先制作块时,我们需要将 blob 数据转换为字符串或 base64 以制作多个块。它可以在 7-8 MB 上正常工作,但是当我们有 20 MB 以上时,它也会在将 blob 转换为字符串或 base64 时引发堆大小错误。
【问题讨论】:
标签: salesforce dropbox-api apex-code