androidpostamazon-s3kotlinawss3transferutility

Upload my object to AWS S3 from my android project in kotlin using tranferutility


I am amateur to both android studio and kotlin. However, I am designing an app where I am collecting user data and need to post it to AWS s3 in an anonymous way without the cognito authentication. I have done a sample upload of an image onto aws S3 but how do I post my json object onto the AWS S3. I have read some of the documentation but I am stuck at transferutility.upload. Because most of the code snippets for upload talk about a file and I freeze there because, I just want post my object.

What I have attempted till now. The interface code:

interface IMyAPI {

@GET("child/{dynamic}/learningpods_new")
fun getDynamic(@Path("dynamic")dynamic:String):Observable<ArrayList<Data>>

@POST("parent/")
fun createParent(@Body parentDetails: ParentDetails):Call<ParentDetails>
}

I am able to get from the specified end point but I don't want to post to the same endpoint.

The retrofit code:

object RetrofitClient {
private var OurInstance : Retrofit?=null
val instance:Retrofit
get() {
    if (OurInstance==null)
    {
        OurInstance =Retrofit.Builder()
                .baseUrl("http://coreapi.imagin8ors.org:8080/v1/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
    }
    return OurInstance!!
  }
}

The posting code snippet:

val retrofit = RetrofitClient.instance
jsonAPI = retrofit.create(IMyAPI::class.java)
val call=jsonAPI.createParent(new_parent)
call.enqueue(object :Callback<ParentDetails>{
        override fun onFailure(call: Call<ParentDetails>?, t: Throwable?) {
            Toast.makeText(this@ParentmainActivity,"something went wrong",Toast.LENGTH_LONG).show()
        }

        override fun onResponse(call: Call<ParentDetails>?, response: Response<ParentDetails>?) {
            Toast.makeText(this@ParentmainActivity,"successful :"+response?.code(),Toast.LENGTH_LONG).show()
        }

    })

Any help is appreciated.


Solution

  • If you want to post object to AWS S3, there's a method by name putObject where you can upload objects to the AWS S3. The below code snippet is an sample way to upload an object containg "name". You can find the same in the specified bucket.

    val background= object :Thread() {
            override fun run() {
                try {
                    try {
                        AWSMobileClient.getInstance().initialize(baseContext)
                        credentials = BasicAWSCredentials(AWSutils().KEY,AWSutils().SECRET)
                        s3Client = AmazonS3Client(credentials)
                        val response=s3Client.putObject(AWSutils().bucket_name,"jsa3/NewParentData","name")
                    } catch(e:AmazonServiceException) {
                        // The call was transmitted successfully, but Amazon S3 couldn't process
                        // it, so it returned an error response.
                        e.printStackTrace();
                    }
                    catch(e:AmazonClientException) {
                        // Amazon S3 couldn't be contacted for a response, or the client
                        // couldn't parse the response from Amazon S3.
                        e.printStackTrace();
                    }
                }catch (e:Exception){
                    e.printStackTrace()
                }
            }
        }
        background.start()
    

    Moreover, the upload cannot happen on the mainthread so you'll have to create another thread to do this upload and that's what is shown in the above code. If you ask how do I know if the object has been successfully posted: One way is to check in your bucket using your aws s3 account, the other way is to do a getobject and print the objectContent. This should give you an indication that you were able to post the data.

    However, there might be some concerns: we must ensure that you are uploading the object and not a new instance of the object. The other thing to uploading objects to aws s3 is that you cannot append data, only overwriting happens. So, if you want to have a collection of objects, then its not wise to use the putObject method-----this comes under the use case scenario and this can be discussed further.