initializeUpload
This method is used to initialize attachment uploads.
Refer to Uploading Files to S-Drive (Amazon S3) for comprehensive instructions on how to upload files using SDriveTools API.
List<UploadRequestInfo> initializeUpload
(String objectId, List<SObject> attachments, Map<String,String> policyMap)
Parameters:
objectId<String>: Id of the parent object. You can use 15-character or 18-character Salesforce.com id. For example, this ID is the ID of the case record if the attachments are being uploaded for a case.
attachments<List<Object>>:List of Salesforce SObject for uploading. The SObject will be representing the "File" object. For example, for a Case attachment, the SObject will be representing the cg_CaseFile_c record.
policyMap<Map<String,String>>: Map of policy conditions and theirs values that represent the additional policy parameters used during upload (e.g. ('$Content-Disposition', 'attachment; filename)). For more information, visit the Amazon documentation:
http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
IdToUploadNewVersionList<List<String>>(Optional): If you are using a versioned bucket and want to upload a file as a new version add this parameter to the initalizeUpload method. The value is a list of Ids you want to upload a new version of. The List should be of the same size as the attachments parameter. Each Id corresponds to an attachment in the attachments list.
Return Value: The method will return the list of UploadRequestInfo (see table below), which holds information about the files that are about to be uploaded.
The following steps are executed within this method:
Validate the input with the following rules:If the uploaded attachments are of type S3Object (i.e. files in S-Drive tab), the Parent__c field should be the same for all files.
If the uploaded attachments are of type S3Object (i.e. files in S-Drive tab), there should not be an existing file with the same name in the same folder.
File name and file size can't be blank
File name can't include the following characters: \ / : * ? \" <> | ~
File size can't be zero.
File size can't be more than the Max file size defined in S-Drive Configuration.
Create the file record in Salesforce with work in progress state (WIP__c = true) ( i.e. create
S3Object, Case File, Custom Object File, etc).Calculate policy, signature, file name etc. in order to be used as the POST parameters. While
calculating the policy, policyMap parameter is used to generate different parameters in the
policy. For html upload please reference Figure 9 and Returns a list of UploadRequestInfo
objects.
Example Code
List<SObject>attachments = new List<SObject>();
My_Example_Object_File__c attachment = new My_Example_Object_File__c();
attachment.File_Name__c = 'Example.txt';
attachment.File_Size_in_Bytes__c ='100';
attachment.Parent__c = 'a0I80…';
String objectId = String.valueOf(attachment.Parent__c);
attachments.add(attachment);
Map<String, String> policyMap= new Map<String,String>();
policyMap.put('$Content-Disposition','attachment; filename');
List<cg.UploadRequestInfo> uploadRequestInfos=
cg.SDriveTools.initializeUpload(objectId ,attachments, policyMap );
// with versioning
List<String> IdToUploadNewVersionList = new List<String>();
IdToUploadNewVersionList.add(oldVersionId);
List<cg.UploadRequestInfo> uploadRequestInfos=
cg.SDriveTools.initializeUpload(objectId ,attachments, policyMap, IdToUploadNewVersionList);
Notes:
It is required to set SObject’s File_Name__c, File_Size_in_Bytes__c and Parent__c
fields. You can optionally set other custom fields.For S-Drive Attachments files, Parent__c refers to its parent object id. In order to set
the parent folder id, use Parent_Folder_Id__c field. For S-Drive Folders files,
Parent__c refers to its parent folder id and if you want to upload the file to home
folder set this to null. Also for S-Drive Folders files, objectId must to be set to null.If you put expiration, acl, bucket, key and x-amz-server-side-encryption conditions in
the policyMap, these parameters will be ignored. Because, these are being set in the
initializeUpload () method. You do not need to put these parameters in the
policyMap. But, you must use these parameters while uploading.
Form Field | Value |
acl | private' |