简体   繁体   English

使用tranferutility将我的对象从Kotlin的android项目上载到AWS S3

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

I am amateur to both android studio and kotlin. 我对android studio和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. 但是,我正在设计一个应用程序,用于收集用户数据,并且需要以匿名方式将其发布到AWS s3,而无需进行认知验证。 I have done a sample upload of an image onto aws S3 but how do I post my json object onto the AWS S3. 我已经完成了将图像上传到AWS S3的示例,但是如何将json对象发布到AWS S3上。 I have read some of the documentation but I am stuck at transferutility.upload . 我已经阅读了一些文档,但仍停留在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. 任何帮助表示赞赏。

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. 如果要将对象发布到AWS S3,则有一个名为putObject的方法,您可以在其中将对象上传到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 . 如果您问我如何知道对象是否已成功发布:一种方法是使用aws s3帐户检入存储桶,另一种方法是执行getobject并打印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. 将对象上传到AWS s3的另一件事是您无法追加数据,仅会覆盖。 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. 因此,如果您想拥有一个对象集合,那么使用putObject方法是不明智的,这是在用例场景下进行的,可以对此进行进一步讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM