Android-Amplify:使用Amplify将文件上传到AWS S3/从AWS S3下载文件 [英] Android-Amplify: Uploading/downloading file to/from AWS S3 using Amplify

查看:843
本文介绍了Android-Amplify:使用Amplify将文件上传到AWS S3/从AWS S3下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个简单的Android应用程序,以将图像文件上传到AWS中已经创建的S3存储桶.如果我用Google搜索,则所有最新的AWS文档都将我重定向为使用放大框架.我不理解此处.我不知道在哪里可以提供存储桶名称,IAM凭据等.我也找不到任何视频教程.如果要在没有提供适当文档的情况下强制使用Amplify,为什么要使用AWS?

I want to develop a simple android app to upload an image file to an already created S3 bucket in AWS. If I google, All the latest AWS documentations are redirecting me to use Amplify framework. I don't understand the documentation of uploading a file described here. I don't understand where I can provide bucket name, IAM credentials, etc. I don't find any video tutorials as well. Why AWS if forcing to use Amplify without providing proper documentation?

此处他们提到配置所有详细信息在Amplify CLI中使用

Here they mention to configure all details in Amplify CLI using

amplify add storage

并要求使用

amplify push

但是,如果我们要以编程方式添加存储桶名称,用户详细信息等详细信息,那么我们该怎么做呢? 通过使用Amplify框架或旧的适用于Android的AWS开发工具包(不包含Cognito)上传文件,为我提供逐步详细信息

But if we want to add details such as bucket name, user details, etc. programmatically, then how do we do it? Give me step by step details either using Amplify framework or old AWS SDK for android for uploading file without Cognito

推荐答案

有很多方法可以从Android设备将文件上传到S3.这里有一些.

There are a number of ways to upload files to S3, from an Android device. Here are a few.

有关扩展Android存储类别的主要文档是在假设您将使用Amplify CLI创建 new AWS资源的情况下编写的.还有一些有关使用现有S3存储桶 .

The main documentation for Amplify Android's Storage category is written with the assumption that you'll create new AWS resources, using the Amplify CLI. There are also some note about using an existing S3 bucket.

如果都不满足您的需求,则可以使用适用于Android的AWS开发工具包中的旧版TransferUtility.这是 .

If neither meet your needs, you can use the old TransferUtility from the AWS SDK for Android. Here's an example use of the TransferUtility.

您注意到,上面的文档使用AWSMobileClient,它是Amazon Cognito的接口.但是,您可以使用CredentialsProvider的任何实现进行身份验证; AWSMobileClient只是凭据提供程序的一个示例.

As you note, the documentation above uses the AWSMobileClient, which is an interface to Amazon Cognito. However, you can use any implementation of the CredentialsProvider, for authentication; AWSMobileClient is just one example of a credentials provider.

最简单(也是最不安全)的方法可能是使用StaticCredentialsProvider为IAM用户提供访问权限和密钥,如下所示.

The simplest (and a least secure) approach might be to provide an IAM user's access and secret key using a StaticCredentialsProvider, as below.

val region = Region.getRegion(Regions.US_EAST_1)
val credentials = BasicAWSCredentials(accessKey, secretKey)
val provider = StaticCredentialsProvider(credentials)

val transferUtility = TransferUtility.builder()
    .context(applicationContext)
    .s3Client(AmazonS3Client(provider, region))
    .awsConfiguration(AWSConfiguration(applicationContext))
    .build()

val listener = object: TransferListener {
    override fun onProgressChanged(id: Int, curr: Long, tot: Long) {}
    override fun onStateChanged(id: Int, state: TransferState?) {
        when (state) {
            COMPLETED -> { Log.i("Demo", "Upload succeeded.") }
            FAILED -> { /* handle err */ }
            else -> { /* handle cases... */ }
        }
    }
    override fun onError(id: Int, ex: Exception?) { /* handle err */ }
}

transferUtility.upload(remoteBucket, remoteKey, localFile)
    .setTransferListener(listener)

这篇关于Android-Amplify:使用Amplify将文件上传到AWS S3/从AWS S3下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆