Initializing help system before first use

Setting tags on an object in an S3 bucket


Description: Demonstrates uploading data to an object in an S3 bucket and then setting tags on that object.
File(s): settagging.mos


settagging.mos
(!*******************************************************
  * Mosel S3 Integration Examples                       *
  * =============================                       *
  *                                                     *
  * file settagging.mos                                 *
  * ```````````````````                                 *
  * Example of uploading a file to an S3 bucket and     *
  * then setting some tags on it.                       *
  *                                                     *
  * You could use tags to indicate a file format,       *
  * to store processing instructions, or any meta-data  *
  * that you want to record against the remote object.  *
  *                                                     *
  * 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 S3SetTaggingExample
 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 I should have tags"
 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="MyTaggedFile.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" )
 
 ! Now set the tags applied to this object
 writeln( "Applying tags type=example and author=JFarmer to the object ",OBJECT_KEY )
 s3setobjecttagging( mybucket, OBJECT_KEY, [ s3newtag("type","example"), s3newtag("author","JFarmer") ] )
 
 ! Check for errors
 if s3status(mybucket)<>S3_OK then
  writeln("Error returned by S3 service: ", s3getlasterror(mybucket))
  exit(1)
 end-if
 
 writeln( "Tags applied successfully" )
 
end-model