Skip to main content
Skip table of contents

Uploading Files to S-Drive (Amazon S3)

You can use SDriveTools API calls for uploading the S-Drive Attachments/Folders Files to Amazon. For this, you will need to use below methods:


Before upload, you must first initialize attachment upload. Initializing the attachment upload creates work in progress (WIP) file(s) objects (S3Object, Case File, My Example Object File, etc.) and calculates the required file information for the upload process. For initializing attachment upload, use initializeUpload() method.


The next step is uploading the file(s) to Amazon using html upload (see code snippet below.) When you upload file(s) to Amazon, You should use the information which has been returned by initializeUpload() as well as the values retrieved from getBucketName(), getAmazonRegion() and getS3Endpoint() methods. If the region used is us-east-1, ten the value for region used will be s3.


If upload is successful, you must complete attachment upload. Complete attachment upload means updating WIP file records created during initialize upload call and set the WIPc field value as false. For completing attachment upload, use completeUpload() method.

If you are using a versioned bucket you will be able to upload a file as a new version. First you need to find the existing files you want to upload a new version of. Get their Ids and put it into a list. Then add the list as another parameter in the initializeUpload(). Continue uploading using the information from UploadRequestInfos object. See our section on initializeUpload for more details(). Then you may get the version Id from the response headers of the request. See the code below to see how to get the version Id. The response header’s name is x-amz-version-id. After getting the version Id use the completeUpload() method with the versionId as a parameter.


If upload fails, you must cancel upload. Canceling upload means deleting the WIP file records. For canceling attachment upload, use cancelUpload() method.
You can also upload the big files using multipart uploads. For this, you will need below methods.

Sample Code:

CODE
<form>
File : <input type="file" name="file" id="file" />
<input type="button" value="Upload" onclick="htmlUpload();"/>
</form>

<script type="text/javascript">

var uploadFile ;
document.getElementById('file').addEventListener('change', handleFileSelect,false);

function handleFileSelect(evt)
{
      var fileName = evt.currentTarget.files[0].name ;
      var fileSize = evt.currentTarget.files[0].size;
      uploadFile = evt.currentTarget.files[0];
      //Call initializeUpload() method
}
  
function htmlUpload()
{
      var fd = new FormData();
      fd.append('key', '{!uploadRequestInfo.fileLocation}');
      fd.append('acl', 'private');
      fd.append('x-amz-credential', '{!uploadRequestInfo.awsCredential}');
      fd.append('x-amz-date', '{!uploadRequestInfo.timeStamp}');
      fd.append('policy', '{!uploadRequestInfo.policy}');
      fd.append('x-amz-algorithm', 'AWS4-HMAC-SHA256');
      fd.append('x-amz-server-side-encryption', '{!uploadRequestInfo.s3EncryptionType}');   
      fd.append('Content-Disposition', 'attachment; filename=\"'+ '{!uploadRequestInfo.fileName}' +'\"');    
      fd.append('x-amz-signature', '{!uploadRequestInfo.signature}');   
      fd.append("file",uploadFile);
         
      var xhr = new XMLHttpRequest();
      xhr.addEventListener("load", function() {
      if (xhr.status === 200) {
        // use complete upload with version id
        var versionId = xhr.getResponseHeader('x-amz-version-id');
        if(versionId != null)
          // Call completeUpload() method using the version Ids. See SDriveTools.completeUpload() method
        else
          // call completeUpload() method without using the versionIds. See SDriveTools.completeUpload() method
        console.log(xhr.responseText);
      } else {
        // handle the error response here
        console.error('Error:', xhr.statusText);
      }
      });
      xhr.addEventListener("error", function() {
        // handle the error here
        console.error('Error:', xhr.statusText);
      });
      xhr.open('POST', 'https://’ + '{!bucketName}' + '. ' + '{!region}' + '.s3.amazonaws.com/', true);     // if region is us-east-1. leave region blank,    
     
      xhr.send(fd);    
} 


function htmlUploadComplete(evt) 
{      
        //Call completeUpload()  method   
}

function htmlUploadFailed(evt) 
{
        //Call cancelUpload() method      
}
</script>

Html Policy

CODE
{ 
  "conditions": 
    [
      ["starts-with", "$Content-Disposition", "attachment; filename"],
    ]
}


Exceptions and Reasons for Upload Operations

[SDriveException]"Parent Folder IDs must be the same for all file records that are being uploaded". 
[Reason] If Parent__c values which are passed in with the SObjects are not same for all S3Object.
[Solution] Give the parent folder ids the same for all S3Objects.

[SDriveException]"A file with the same name exists in the target folder (fileName) ".
[Reason] This exception occurs if a file with the same name exists in the same folder for S3Objects.The same file name condition holds when the file is not a WIP (WIP_c =false) file or the file is a WIP file (WIP_c =true) with the same file namebutithasbeenuploadedbyanotheruserlessthan12 hours ago.
[Solution]Upload files with different file names or delete the previously existing files before the upload.

[SDriveException]"Error occurred while checking the files to upload! File Name, File Size cannot be blank (fileName)".
[Reason]If the file name or file size fields that have been passed in with the SObject are null or empty.
[Solution]File name and file size is required for upload. Thus, pass non-blank values in with the SObject.

[SDriveException]"Name cannot start with a space or a dot and cannot contain any of the following characters: \\ /: ? \" < > |  (fileName)". 
[Reason]If the file name is invalid. Invalid name means, file name starts with a spaceor a dot and contains any of the following characters:  \ / :  ? \" < > | 
[Solution]Do not upload a file with the name starting with space or a dot and contain any of the following characters:  \ / : * ? \" < > | ~

[SDriveException]"You cannot upload a zero-length file (fileName)".  
[Reason] If the file size is 0. 
[Solution] Upload files that are greater in size than zero.

[SDriveException]"Files greater than (maxFileSize) cannot be uploaded. Please contact your system administrator for the file size limits! (fileName)".
[Reason]File being uploaded has a size greater than the maximum file size limit  (MAX_FILE_SIZE configuration in S-Drive Configuration page).
[Solution]Do not upload files whose size greater than MAX_FILE_SIZE. Or Increase the MAX_FILE_SIZE configuration in S-Drive Configuration page.

[SDriveException]"Invalid parent id specified. Check your function parameters!". 
[Reason] If object (parent) id, which is passed as a parameter, is not a Salesforce.com 15 or 18 characters long ID.
[Solution]Pass 15-character or 18-characterSalesforce.com ID as objectId parameter.

Error Messagesof ResultObject and Reasons

[errorMessage]"Wip Id is null! ". 
[Reason] If wipId in the wipIds list parameter is null.
[Solution] Do not pass null Salesforce.com IDs of file objects.

[errorMessage]"'No such wip file with provided id: (wipId) ".
[Reason]If wipId in wipIds list is not available.
[Solution]Pass correct and available Salesforce.com ids of files objects.

[errorMessage]"There is no wip files".
[Reason]If all of wipId in wipIds list is null or wipIds' size is 0.
[Solution]Pass wipIds with length greater than0 and non-null string values.

[errorMessage]" Insufficient Privileges. You do not have not access to update operation".
[Reason]If Object-level security for update access is not allowed.
[Solution] Make sure that update access is allowed for your profile.
Html upload example:



You can see the appropriate values for the request parameters below (Figure 10).

Form Field

Value

acl
Content-Disposition
Content-Type
key
policy
x-amz-server-side-encryption
x-amz-credential
x-amz-date
x-amz-algorithm
x-amz-signature
success_action_status

'private'
'attachment; filename=\"'+ uploadRequestInfo.fileName+'\"'
uploadRequestInfo.fileType
uploadRequestInfo.fileLocation
uploadRequestInfo.policy
uploadRequestInfo.s3EncryptionType
uploadRequestInfo.awsCredential
uploadRequestInfo.timeStamp
'AWS4-HMAC-SHA256’
uploadRequestInfo.signature
201

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.