#Region Project Attributes
#ApplicationLabel: AWS_S3_Use2
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
End Sub
Sub Globals
Private AWS_job As HttpJob
End Sub
Sub Activity_Create(FirstTime As Boolean)
End Sub
Sub Activity_Resume
'Set time zone to GMT - all date/times to AWS need to be wrt GMT
DateTime.SetTimeZone(0)
'http://docs.aws.amazon.com/AmazonS3/latest/API/s3-api.pdf
'page 368
'Example 1: Upload an Object
'
'PUT /test1.jpg HTTP/1.1
'Host: <bucket>.<s3-aws-region>.amazonaws.com
'x-amz-date: <date as yyyyMMdd'T'HHmmss'Z'>
'Content-Type: text/plain <<<<<<<<<<<<WRONG
'Content-Length: <length>
'x-amz-meta-author: <name>
'Expect: 100-continue
'Authorization: authorization string
'[<length> bytes of object data]
'
'NOTE: after considerable fustrating fiddling it appears that only
'Content-Type setting that works is:
'Content-Type: application/x-www-form-urlencoded
'
'AWS_S3_File_Name : "test1.jpg"
'AWS_S3_HttpMethod : "PUT"
'AWS_S3_Query_map : Null
'AWS_S3_Payload : bytes of File.DirAssets,"test1.jpg"
'AWS_S3_OtherHeader_map: "Content-Type", "application/x-www-form-urlencoded"
' : "Content-Length", "<length>"
' : "x-amz-meta-author", "<name>"
' : "Expect", "100-continue"
' AWS_URI_Is_Path_Style
' True = URI is "path style", e.g.:
' http://<endpoint>/<bucketname>/<filename>
' False = URI is "virtual hosted style", e.g.:
' http://<bucketname>.<endpoint>/<filename>
' AWS_Access_Key_ID
' AWS_Secret_Access_Key
' AWS_Region
' where S3 bucket resides, as per:
' http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
' e.g.:
' ap-southeast-2
' AWS_End_Point
' where S3 bucket can be accessed, as per:
' http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
' e.g.:
' s3-ap-southeast-2.amazonaws.com
' AWS_S3_Bucket_Name
' AWS_S3_File_Name
' full name of file in bucket, including any pseudo folders, e.g.:
' examplefile.txt
' pseudofolder1/pseudofolder2/examplefile.txt
' AWS_S3_HttpMethod
' "GET", "PUT" or "DELETE"
' AWS_S3_Query_map
' map of URI query parameter key, value pairs
' e.g:
' URI is:
' http://s3.amazonaws.com/examplebucket?prefix=somePrefix&marker=someMarker&Max-keys=20
' URI query parameter key, value pairs:
' "prefix", "somePrefix"
' "marker", "someMarker"
' "max-keys", "20"
' e.g:
' URI is:
' http://s3.amazonaws.com/examplebucket?acl
' URI query parameter key, value pairs:
' "acl", ""
' e.g:
' URI does not include a "?"
' AWS_S3_Query_map is Null
' AWS_S3_Payload
' AWS_S3_GMT_DateTime
' AWS_S3_OtherHeader_map
' map of other request header key, value pairs - exclude following
' headers which are added automatically:
' host
' x-amz-content-sha256
' x-amz-date
' following headers must be added to this map:
' content-type header - if present in request
' any other x-amz-* headers
' consider including all other headers in order to prevent data tampering
AWS_S3.AWS_Access_Key_ID = "yourAccess_Key_ID"
AWS_S3.AWS_Secret_Access_Key = "yourSecret_Access_Key"
AWS_S3.AWS_Region = "yourRegion"
AWS_S3.AWS_End_Point = "yourEnd_Point"
AWS_S3.AWS_S3_Bucket_Name = "yourBucket_Name"
AWS_S3.AWS_S3_File_Name = "test1.jpg"
AWS_S3.AWS_S3_HttpMethod = "PUT"
AWS_S3.AWS_S3_Query_map.Initialize
AWS_S3.AWS_S3_Query_map.Clear
Private wrk_in As InputStream
wrk_in = File.OpenInput(File.DirAssets,"test1.jpg")
Private wrk_out As OutputStream
wrk_out.InitializeToBytesArray(1000)
File.Copy2(wrk_in, wrk_out)
wrk_in.Close
wrk_out.Close
AWS_S3.AWS_S3_Payload = wrk_out.ToBytesArray
AWS_S3.AWS_S3_GMT_DateTime = DateTime.Now
AWS_S3.AWS_S3_OtherHeader_map.Initialize
AWS_S3.AWS_S3_OtherHeader_map.Clear
AWS_S3.AWS_S3_OtherHeader_map.Put("Content-Type", "application/x-www-form-urlencoded")
AWS_S3.AWS_S3_OtherHeader_map.Put("Content-Length", NumberFormat2(AWS_S3.AWS_S3_Payload.Length, 1, 0, 0, False))
AWS_S3.AWS_S3_OtherHeader_map.Put("x-amz-meta-author", "Jack")
AWS_S3.AWS_S3_OtherHeader_map.Put("Expect", "100-continue")
Log(AWS_S3.URI)
'Set up httpjob
AWS_job.Initialize("AWS_job", Me)
AWS_job.PutBytes(AWS_S3.URI, AWS_S3.AWS_S3_Payload)
'Retrieve full header map
Private wk_hdr_map As Map
wk_hdr_map.Initialize
wk_hdr_map = AWS_S3.FullHeaderMap
Private wrk_ptr As Int
'For each key, value pair of full header map...
For wrk_ptr = 0 To wk_hdr_map.Size - 1
'If not host header...
If wk_hdr_map.GetKeyAt(wrk_ptr) <> "host" Then
'Add it to httpjob
AWS_job.GetRequest.SetHeader(wk_hdr_map.GetKeyAt(wrk_ptr), wk_hdr_map.GetValueAt(wrk_ptr))
Log(wk_hdr_map.GetKeyAt(wrk_ptr) & ", " & wk_hdr_map.GetValueAt(wrk_ptr))
End If
Next
Log(AWS_S3.Authorization)
'Add Authorization header to httpjob
AWS_job.GetRequest.SetHeader("Authorization", AWS_S3.Authorization)
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub JobDone(Job As HttpJob)
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Select Job.JobName
Case "AWS_job"
Log("test1.jpg uploaded")
End Select
Else
Log("Error: " & Job.ErrorMessage)
End If
Job.Release
Activity.Finish
End Sub