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

Querying tags on an object in an S3 bucket

Description
Demonstrates querying for an expected tag on an object in an S3 bucket.

s3_gettagging.zip[download all files]

Source Files





gettagging.mos

(!*******************************************************
  * Mosel S3 Integration Examples                       *
  * =============================                       *
  *                                                     *
  * file gettagging.mos                                 *
  * ```````````````````                                 *
  * Example of reading the tags of an object stored     *
  * in an S3 bucket.                                    *
  *                                                     *
  * 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.  *
  *                                                     *
  * You can use the example 'settagging.mos' to create  *
  * the remote object that is used by this example.     *
  *                                                     *
  * 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 S3GetTaggingExample
 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

 declarations
  OBJECT_KEY="MyTaggedFile.txt"
  mybucket: s3bucket
  tags: list of s3tag
  type: string
 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
 
 
 ! Now get the tags applied to this object
 writeln( "Querying tags of the object ",OBJECT_KEY )
 tags := s3getobjecttagging( mybucket, OBJECT_KEY )
 
 ! Check for errors
 if s3status(mybucket)<>S3_OK then
  writeln("Error returned by S3 service: ", s3getlasterror(mybucket))
  exit(1)
 end-if
 
 writeln( "Tagging query successful" )
 
 ! Look for a 'type' tag
 forall (t in tags) do
  writeln(" Found tag ",t.key,"=",t.value)
  if t.key="type" then
   type := string(t.value)
  end-if
 end-do
 
 ! Now do something different based on type
 if type='example' then
  writeln("This was an example file")
 elif type='' then
  writeln("This file did not have a 'type' tag")
 else
  writeln("This file had an unrecognized type '",type,"'")
 end-if
end-model
Back to examples browserPrevious example