FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserNext example

Uploading a file to an S3 bucket

Description
Demonstrates uploading data to an object in an S3 bucket.


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
upload.mos[download]





upload.mos

(!*******************************************************
  * Mosel S3 Integration Examples                       *
  * =============================                       *
  *                                                     *
  * file upload.mos                                     *
  * ```````````````                                     *
  * Example of uploading a file to an S3 bucket.        *
  *                                                     *
  * Note: this must either be run on a cloud component  *
  * (e.g. Xpress Workbench on DMP), or the user must    *
  * enter their own S3 bucket URL and access            *
  * credentials where indicated below.                  *
  *                                                     *
  * (c) 2017 Fair Isaac Corporation                     *
  *     author: James Farmer, 2017                      *
  *******************************************************!)
model S3UploadExample
 uses "s3"

 parameters
  ! Set to 'true' when running this model within DMP,
  ! 'false' when on-premise
  ON_CLOUD=true
  
  ! On-premise, user must specify their own S3 credentials
  ! Fill in yours in the lines below
  S3_BUCKET_URL = ''
  S3_REGION = ''
  S3_ACCESS_KEY_ID = ''
  S3_SECRET_KEY = ''
  S3_SESSION_TOKEN = ''  ! Optional
 end-parameters

 public declarations
  DATA_TO_UPLOAD = "Hello World"
 end-declarations
 
 declarations
  LOCAL_FILE="mmsystem.text:DATA_TO_UPLOAD"   ! Upload directly from a string object, to save us the trouble of creating a local file
  OBJECT_KEY="MyFile.txt"
  mybucket: s3bucket
 end-declarations


 if ON_CLOUD then
  ! On the cloud, use the DMP 'solutiondata' bucket
  s3init( mybucket, S3_DMP_SOLUTIONDATA )
  if s3status(mybucket)<>S3_OK then
   writeln("Error initializing S3 folder: ", s3getlasterror(mybucket))
   exit(1)
  end-if
 
 else
  ! On-premise, user must specify their own S3 credentials
  ! Fill in yours in the lines below
  mybucket.url := S3_BUCKET_URL
  mybucket.region := S3_REGION
  mybucket.accesskeyid := S3_ACCESS_KEY_ID
  mybucket.secretkey := S3_SECRET_KEY
  mybucket.sessiontoken := S3_SESSION_TOKEN
 end-if
 
 ! Upload local file to remote object
 writeln( "Uploading to ",OBJECT_KEY," from local file ",LOCAL_FILE )
 s3putobject( mybucket, OBJECT_KEY, LOCAL_FILE )

 ! Check for errors
 if s3status(mybucket)<>S3_OK then
  writeln("Error returned by S3 service: ", s3getlasterror(mybucket))
  exit(1)
 end-if
 
 writeln( "Upload successful" )
 
end-model
Back to examples browserNext example