Apex
Examples

Upload files from Salesforce to any cloud storage with Apex

In this article, we discuss how CloudFiles offers a simple way to upload any file type of any size from Salesforce to cloud storages such as Sharepoint, Google Drive, OneDrive, Box, Dropbox, AWS S3 etc...

Using CloudFiles for uploading files also bypasses any governor limits that Salesforce imposes since the CloudFiles app 'pulls' the file from Salesforce using it's own server and transfers it your storage, without maintaining a copy in between.

This article shows the pre-requisites for using CloudFiles Apex with Salesforce and then shows the piece of code that makes it possible.

Pre-requisites for using CloudFiles Apex

In order to use CloudFiles Apex, make sure you have CloudFiles installed in the correct sandbox / production environment. Also make sure you have authenticated your Cloud Drive using CloudFiles atleast once.

Simply follow Steps 1 & 2 mentioned in the Installation guide and Steps 1 & 2 mentioned in the Connect external folder to a record as well to make sure you are setup.

Document image


CloudFiles Upload File Apex

We at CloudFiles have built an AppExchange app to work with files easily in Salesforce. Using CloudFiles, you can avoid setting up an API server, setting up authentications or building integrations with different cloud storage platforms.

CloudFiles can upload any type of files using this 1 line apex code -

1.cldfs.Client.upload( files, // a list of sobjects with blob field 2. 'rTmK2YQnxaEIFkU_C18a62b2xWyDB', // folder id in external storage 'google', // destination: google|onedrive|sharepoint|box|dropbox|cloudfiles(s3) 3. 'mydrive', // drive id in external storage(optional) 4. null // site id in case of sharepoint(optional for other storages) );

JavaScript

Here's a breakdown of all the fields in this code -

  1. files - This is a list of sObjects with blob fields, i.e. your actual file data that you want to transfer. For more info on Salesforce blob objects, check this official documentation.
  2. folder id - This is the target folder where you want to place your files. This can be retrieved by going into the folder in the CloudFiles library and copying the given folder ID 
  3. destination library - This is the final library where you want to send the file to. This can be set to google, onedrive, sharepoint, box, dropbox, cloudfiles( AWS s3) depending on your requirement.
  4. destination drive - This depends on the destination library. For google and onedrive, this can be mydrive or the share drive's ID. For Sharepoint this is the drive ID retrieved from URL when going into a specific drive folder of a specific site.
  5. destination site - This input is only applicable for sharepoint. This determined the site ID where you want to upload the file to.