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 in the installation article and Steps 1& 2 in the quick start article as well to make sure you are setup.
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 -
Here's a breakdown of all the fields in this code -
- 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.
- 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
- 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.
- 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.
- destination site - This input is only applicable for sharepoint. This determined the site ID where you want to upload the file to.