dependencies {
def aws_version = "2.16.+"
implementation "com.amazonaws:aws-android-sdk-s3:$aws_version"
implementation ("com.amazonaws:aws-android-sdk-mobile-client:$aws_version") { transitive = true }
public void downloadFileByKey(String key) throws JSONException {
JSONObject jsonConfig = new JSONObject();
JSONObject s3TransferUtility = new JSONObject();
jsonConfig.putOpt("S3TransferUtility", s3TransferUtility);
s3TransferUtility.put("Region", "服务端请求来的region");
s3TransferUtility.put("Bucket", "服务端请求来bucket");
AWSSessionCredentials credentials = new AWSSessionCredentials() {
@Override
public String getSessionToken() {
return "服务端请求过来的SessionToken";
@Override
public String getAWSAccessKeyId() {
return "服务端请求来的AccessKey";
@Override
public String getAWSSecretKey() {
return "服务端请求来的SecretKey";
AWSConfiguration configuration = new AWSConfiguration(jsonConfig);
TransferUtility utility = TransferUtility.builder()
.context(this)
.s3Client(new AmazonS3Client(credentials))
.awsConfiguration(configuration)
.build();
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/test44.png");
TransferObserver observer = utility.download(key, file);
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int i, TransferState transferState) {
if (transferState == TransferState.COMPLETED) {
//下载完成了
@Override
public void onProgressChanged(int i, long l, long l1) {
@Override
public void onError(int i, Exception e) {
//下载出错
以上是下载文件,上传文件类似:
TransferObserver observer = utility.upload(key, file);