function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sunil kattasunil katta 

File upload into Salesforce Content Version using Rest API

I want to upload a file to content version using Rest API. As of now i achieved to do using REST API with java code. Code is uploading till 35 MB single file and no more than that. Code is throwing 400 status code.
Please suggest me its possible to upload a file more than 100+MB file. If its  YES, then where i got stuck. looking forward from team if any one faced same situation.
.
 
Best Answer chosen by sunil katta
AnudeepAnudeep (Salesforce Developers) 
Hi Sunil,

You must be hitting the following limit

"You can insert or update blob data using a non-multipart message, but if you do, you are limited to 50 MB of text data or 37.5 MB of base64–encoded data and that is the reason you are not able to load the content version via Rest api"

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm#inserting_a_contentversion

Did you try using any other way of loading other than using Java cide? This should be possible using any tools capable of generating multipart POST requests (e.g. Curl, Fiddler, C#/.NET client applications, etc.).

In multipart message the first part of the request message body contains non–binary field data such as the Description or Name. The second part of the message contains the binary data of the file that you're uploading.

Example for creating a new Document via curl:

curl https://na1.salesforce.com/services/data/v23.0/sobjects/Document/ -H "Authorization: Bearer token" -H "Content-Type: multipart/form-data; boundary=\"boundary_string\"" --data-binary @newdocument.json

Example request body for creating a new Document

This code is the contents of newdocument.json. Note that the binary data for the PDF content has been omitted for brevity and replaced with “Binary data goes here.” An actual request will contain the full binary content.

--boundary_string
Content-Disposition: form-data; name="entity_document";
Content-Type: application/json

{
"Description" : "Marketing brochure for Q1 2011",
"Keywords" : "marketing,sales,update",
"FolderId" : "005D0000001GiU7",
"Name" : "Marketing Brochure Q1",
"Type" : "pdf"
}

--boundary_string
Content-Type: application/pdf
Content-Disposition: form-data; name="Body"; filename="2011Q1MktgBrochure.pdf"

Binary data goes here.

--boundary_string--


Example response body for creating a new Document

On success, the ID of the new Document is returned.

{
"id" : "015D0000000N3ZZIA0",
"errors" : [ ],
"success" : true
}

If this is failing with other apps as well, we know you are hitting the limit. Otherwise there could be an issue with Java code

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. Thank you!

Anudeep
 

All Answers

VinayVinay (Salesforce Developers) 
Hi Sunil,

Using the SObject Basic Information or SObject Rows APIs, the maximum file size for uploads is 2 GB for ContentVersion objects and 500 MB for all other eligible standard objects as per below article.

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

Please refer below examples.

https://xomino.com/2016/09/05/adding-an-attachment-to-an-opportunity-using-salesforce-rest-apis/
https://crmcog.com/retrieve-sfdc-attachments-using-rest/

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
AnudeepAnudeep (Salesforce Developers) 
Hi Sunil,

You must be hitting the following limit

"You can insert or update blob data using a non-multipart message, but if you do, you are limited to 50 MB of text data or 37.5 MB of base64–encoded data and that is the reason you are not able to load the content version via Rest api"

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm#inserting_a_contentversion

Did you try using any other way of loading other than using Java cide? This should be possible using any tools capable of generating multipart POST requests (e.g. Curl, Fiddler, C#/.NET client applications, etc.).

In multipart message the first part of the request message body contains non–binary field data such as the Description or Name. The second part of the message contains the binary data of the file that you're uploading.

Example for creating a new Document via curl:

curl https://na1.salesforce.com/services/data/v23.0/sobjects/Document/ -H "Authorization: Bearer token" -H "Content-Type: multipart/form-data; boundary=\"boundary_string\"" --data-binary @newdocument.json

Example request body for creating a new Document

This code is the contents of newdocument.json. Note that the binary data for the PDF content has been omitted for brevity and replaced with “Binary data goes here.” An actual request will contain the full binary content.

--boundary_string
Content-Disposition: form-data; name="entity_document";
Content-Type: application/json

{
"Description" : "Marketing brochure for Q1 2011",
"Keywords" : "marketing,sales,update",
"FolderId" : "005D0000001GiU7",
"Name" : "Marketing Brochure Q1",
"Type" : "pdf"
}

--boundary_string
Content-Type: application/pdf
Content-Disposition: form-data; name="Body"; filename="2011Q1MktgBrochure.pdf"

Binary data goes here.

--boundary_string--


Example response body for creating a new Document

On success, the ID of the new Document is returned.

{
"id" : "015D0000000N3ZZIA0",
"errors" : [ ],
"success" : true
}

If this is failing with other apps as well, we know you are hitting the limit. Otherwise there could be an issue with Java code

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. Thank you!

Anudeep
 
This was selected as the best answer
sunil kattasunil katta
Hi Anudeep, 

Thank you for your Suggestion. I wonder if i can use the curl code in JAVA platform. Also do you have any idea on multipart option in Java platform to achive this. 

here is my JAVA code :

package LoginExample;

//import java.io.IOException;

import org.apache.http.client.methods.HttpPost;

//import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.util.EntityUtils;

import org.apache.http.client.ClientProtocolException;

import org.json.JSONObject;

import org.json.JSONTokener;

import org.json.JSONException;

import java.io.*;
import java.io.IOException;//used for Exception Handling
import java.util.Base64;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;



public class salesforceLogin_V {

    static final String USERNAME     = "";

    static final String PASSWORD     = "";

    static final String LOGINURL     = "https://login.salesforce.com";

    static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";

    static final String CLIENTID     = "";

    static final String CLIENTSECRET = "";

    public static void main(String[] args) throws IOException {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        
         //HttpParams httpParams = new BasicHttpParams();
//HttpConnectionParams.setConnectionTimeout(httpParams, 10000);

//HttpConnectionParams.setSoTimeout(httpParams, 10000);
HttpClient httpclient1=new HttpClient();

//HttpClient httpclient = new DefaultHttpClient(httpParams); 


       // HttpClient httpClient1 = HttpClients.createDefault();
        // Assemble the login request URL

        String loginURL = LOGINURL +

                          GRANTSERVICE +

                          "&client_id=" + CLIENTID +

                          "&client_secret=" + CLIENTSECRET +

                          "&username=" + USERNAME +

                          "&password=" + PASSWORD;

 
        System.out.println(loginURL);
        // Login requests must be POSTs
        
        HttpPost httpPost = new HttpPost(loginURL);

        HttpResponse response = null;

 

        try {

            // Execute the login POST request

            response = httpclient.execute(httpPost);

        } catch (ClientProtocolException cpException) {
            //cpException.printStackTrace();
        } catch (IOException ioException) {
            //ioException.printStackTrace();
        }

        // verify response is HTTP OK

        final int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {

            System.out.println("Error authenticating to Force.com: "+statusCode);

            // Error is in EntityUtils.toString(response.getEntity())

            return;

        }

 

        String getResult = null;

        try {

            getResult = EntityUtils.toString(response.getEntity());

        } catch (IOException ioException) {

            // Handle system IO exception

        }

        JSONObject jsonObject = null;

        String loginAccessToken = null;

        String loginInstanceUrl = null;

        try {

            jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();

            loginAccessToken = jsonObject.getString("access_token");

            loginInstanceUrl = jsonObject.getString("instance_url");
            
            System.out.println("loginInstanceUrl : "+ loginInstanceUrl)   ;        


        } catch (JSONException jsonException) {

            // Handle JSON exception

        }

        System.out.println(response.getStatusLine());

        System.out.println("Successful login");

        System.out.println("  instance URL: "+loginInstanceUrl);

        System.out.println("  access token/session ID: "+loginAccessToken);

        SalesForceLogin_FileUpload FileLoad=new SalesForceLogin_FileUpload();
        
        String PATH="D:\\test";
        
        String rootDir = PATH; // Set the rootDir variable to the input port ROOT_PATH                         
                                    
          // FilePathCollection holds a list of file paths                        
                                    
                          
                             
if( PATH!=null ) { // If ROOT_PATH is not null then proceed                                   
                                      
// Call the traverst function and pass it the rootDir variable to collect all direcory file entries                                 
                                    
            FileLoad.traverse( rootDir );                  
                                    
// For Every fileObject in the FilePathCollection (loop through all fileObjects)                               
                                
            for ( String fileObject : FileLoad.FilePathCollection ) {                       
                    
                           
// Get the File Directory and assign it to FILE_DIR                         
                                    
                       String  FILE_DIR = FileLoad.getFileDirectory( fileObject );  
                       System.out.println("FILE_DIR : "+FILE_DIR);             
// Get the File Name and assign it to FILE_NAME                           
                                    
                       String FILE_NAME = FileLoad.getFileName( fileObject );   
                        System.out.println("FILE_NAME : "+FILE_NAME);            
// Get the File Extension and assign it to FILE_EXT                        
                                    
                       String FILE_EXT = FileLoad.getFileExtension( fileObject ); 
                         System.out.println("FILE_EXT : "+FILE_EXT);              
// Get the File Size and assign it to FILE_SIZE_KB                         
                                    
                       String FILE_SIZE_KB =FileLoad.getFileSize( fileObject );  
                                    
// Get the File into BaseEncode64 Conversion                                   
                                    
                   
// Generate and output the data row collected for the current fileObject                               
                                    
                        //FileLoad.generateRow();  
                             
//Loading a converted file
                             
                    // HttpClient httpclient = new HttpClient() {}; 
                     JSONObject content = new JSONObject();
                     content.put("PathOnClient",FileLoad.getFileDirectory( fileObject ));
                     content.put("Title",FileLoad.getFileName( fileObject ));
                     content.put("FirstPublishLocationId","0052w000001WdAAO");
                     content.put("VersionData",FileLoad.getencodeFileToBase64Binary (fileObject) );

    PostMethod post1 = new PostMethod(loginInstanceUrl+"/services/data/v47.0/sobjects/ContentVersion/");
    post1.setRequestHeader("Authorization", "OAuth " +  loginAccessToken);
    
   

        System.out.println("post1"+post1); 
                try {
                    post1.setRequestEntity(new StringRequestEntity(content.toString(),
                            "application/json",null));
                    
                }
            catch(Exception ex){   
                System.out.println(ex.toString());
            }

    String contentId = null;
           try {
        httpclient1.executeMethod(post1);
       
          if (post1.getStatusCode() == HttpStatus.SC_CREATED) {
            JSONObject response_back = new JSONObject(new JSONTokener(
                    new InputStreamReader(
                           post1.getResponseBodyAsStream())));
            if (response_back.getBoolean("success")) {
                contentId = response_back.getString("id");
                
            }

       }
    }

    catch(Exception ex){
    System.out.println(ex.toString());
    }

finally {
       //post1.releaseConnection();
    }
    
                         
    }                      
                                    
}                                  
   // release connection

       httpPost.releaseConnection();

    }

}
 
sunil kattasunil katta
Hi Team,

Please let me know if anyone has idea on Implementing curl command within java Platform