简体   繁体   English

我在Kotlin的android项目中的AWS S3中的Putobject

[英]Putobject in AWS S3 from my android project in kotlin

I am amateur in both android studio and kotlin. 我在android studio和kotlin都是业余爱好者。 However, I am designing an where I have post the new user data to AWS S3 for storage. 但是,我正在设计一个将新用户数据发布到AWS S3进行存储的位置。 I have done the same using Putobject but I am not able to know its been successful. 我已经使用Putobject进行了同样的操作,但是我不知道它是否成功。 Moreover, I want to know if I am doing it right. 此外,我想知道我是否做对了。 The code snippets are shown below. 代码片段如下所示。

val background= object :Thread() {
        override fun run() {
            try {
                try {
                    AWSMobileClient.getInstance().initialize(baseContext)
                    credentials = BasicAWSCredentials(AWSutils().KEY,AWSutils().SECRET)
                    s3Client = AmazonS3Client(credentials)
                    s3Client.putObject(AWSutils().bucket_name,AWSutils().KEY,new_parent.toString())
                } 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()

In the above scenario I'm trying to post my object new_parent. 在上述情况下,我试图发布我的对象new_parent。 Since the syntax takes a string in the third argument, I've casted it to string. 由于语法在第三个参数中包含字符串,因此我将其强制转换为字符串。

Another clarification is that I have to run this a different thread because I get networkingonMainthread exception. 另一个说明是,我必须在另一个线程上运行此线程,因为我收到了networkonMainthread异常。 Therefore, I have done the way you see the code above. 因此,我已经完成了您看到上面代码的方式。

How do I know that the posting has been successful? 我怎么知道发布成功?

Any corrections and help is appreciated. 任何纠正和帮助表示赞赏。

Checking if the posting the object has been successful can be done by printing the objectContents after getObject . 可以通过在getObject之后打印objectContents来检查发布对象是否成功。 Sample code snippet: 示例代码段:

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.getObject(AWSutils().bucket_name,"jsa3/NewParentData")
                    println("Checking the respose" + response.objectContent.toString())
                    Log.i(response.objectContent.toString(),"checking if response successful")
                    response.close()
                } 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()

This should help. 这应该有所帮助。

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

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