Description: A class i wrote that simplifies the ability to perform PUT, GET, LIST and DELETE functions on amazon s3 cloud services.
Requirements: ByteConverter, DateUtils, Encryption, HTTP Library, Reflection, StringUtils
Optional: XOM
I made some modifications to the HttpJob class. I'll include a modified version in the example, but the following should be added to the class:
To use the module you'll want to create a map (used for passing your own custom headers. If you don't want to, just create a map and pass it uninitialized) and call one of the four functions:
awstest.AwsPUT
awstest.AwsGET
awstest.AwsDELETE
awstest.AwsLIST
There are 2 other functions to set info for the aws service:
awstest.OverrideBucketInfo - takes bucket name, apppath and remoteuserdir
awstest.OverrideAccessKeys - takes access key and secret key
I created two different types of methods to read in the response (really just needed for the LIST since it comes back in xml). One that parses it like a string and the other that treats it as XML and uses the XOM parser to get the correct elements.
They're in the main activity.
Some notes:
You can enter in your keys/bucket name/optional paths in the Class_Globals of awsAuthorization or if you're not comfortable doing that, you can always just leave them blank and use the Override functions to set them. In the initialize method i had a dim P phone, remoteUserDir=P.GetSettings("android_id"). The way i'm using this is to create a folder in the S3 bucket that's unique per device so I have have multiple devices using my app and uploading the results to a S3 bucket. That way i can create custom responses for the files they store.
There are some unused methods in the class because i was sort of confused with the different encryption methods amazon has for their REST api documentation.
If you require a different function for the REST api, send me a message and i can update the class. If you want to add something to the class, feel free to write me with the new code segment so i can add it. I only have 4 functions out of like 20 or so written here, so it can be expanded later.
Thank you to Erel, nicholas.jj.taylor, the rest of the contributors that allows me to do this.
Requirements: ByteConverter, DateUtils, Encryption, HTTP Library, Reflection, StringUtils
Optional: XOM
I made some modifications to the HttpJob class. I'll include a modified version in the example, but the following should be added to the class:
B4X:
Sub Class_Globals
...
Private DownloadFolder As String
Private DownloadFileName As String
End Sub
Public Sub FinishDownload
File.Copy(HttpUtils2Service.TempFolder,taskId,DownloadFolder,DownloadFileName)
File.DELETE(HttpUtils2Service.TempFolder,taskId)
End Sub
Public Sub SetDownloadsettings(InDownloadFolder As String, InDownloadFileName As String)
DownloadFolder=InDownloadFolder
DownloadFileName=InDownloadFileName
End Sub
Public Sub GetXMLResponse As InputStream
Dim tr As TextReader,res As String,tempPosition As Int, tempstream As InputStream
tr.Initialize2(File.OpenInput(HttpUtils2Service.TempFolder, taskId), "UTF8")
res = tr.ReadAll
tr.Close
tempPosition=res.IndexOf("<ListBucketResult ")
tempstream.InitializeFromBytesArray(res.GetBytes("UTF8"),tempPosition,res.Length-tempPosition)
Return tempstream
End Sub
Public Sub DeleteRequest(Link As String)
mLink = Link
req.InitializeDelete(Link)
CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
To use the module you'll want to create a map (used for passing your own custom headers. If you don't want to, just create a map and pass it uninitialized) and call one of the four functions:
awstest.AwsPUT
awstest.AwsGET
awstest.AwsDELETE
awstest.AwsLIST
There are 2 other functions to set info for the aws service:
awstest.OverrideBucketInfo - takes bucket name, apppath and remoteuserdir
awstest.OverrideAccessKeys - takes access key and secret key
I created two different types of methods to read in the response (really just needed for the LIST since it comes back in xml). One that parses it like a string and the other that treats it as XML and uses the XOM parser to get the correct elements.
They're in the main activity.
Some notes:
You can enter in your keys/bucket name/optional paths in the Class_Globals of awsAuthorization or if you're not comfortable doing that, you can always just leave them blank and use the Override functions to set them. In the initialize method i had a dim P phone, remoteUserDir=P.GetSettings("android_id"). The way i'm using this is to create a folder in the S3 bucket that's unique per device so I have have multiple devices using my app and uploading the results to a S3 bucket. That way i can create custom responses for the files they store.
There are some unused methods in the class because i was sort of confused with the different encryption methods amazon has for their REST api documentation.
If you require a different function for the REST api, send me a message and i can update the class. If you want to add something to the class, feel free to write me with the new code segment so i can add it. I only have 4 functions out of like 20 or so written here, so it can be expanded later.
Thank you to Erel, nicholas.jj.taylor, the rest of the contributors that allows me to do this.