繁体   English   中英

无法使用 cognito 从 android 上传 s3 存储桶

[英]Can't upload s3 bucket from android using cognito

大家好,所以我尝试使用 android 和 cognito 从我的模拟器上传一张图片到 s3 存储桶上:

package com.example.s3_aws;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    AmazonS3 s3;
    TransferUtility transferUtility;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        credentialsProvider();
        setTransferUtility();
        upload();

    }

    public void credentialsProvider(){
        // Initialize the Amazon Cognito credentials provider
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "idafa", // I actually have here the correct identity pool id
                Regions.US_EAST_1// Region
        );

        setAmazonS3Client(credentialsProvider);
    }

    public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider){

        s3 = new AmazonS3Client(credentialsProvider);
        s3.setRegion(Region.getRegion(Regions.US_EAST_1));
    }

    public void setTransferUtility(){
        transferUtility = new TransferUtility(s3, getApplicationContext());
    }

    public void upload(){
        @SuppressLint("SdCardPath") final TransferObserver observer = transferUtility.upload(
                "s3.android.test",
                "test.png",
                new File("/sdcard/DCIM/Camera/IMG_20200415_001815.jpg"),
                CannedAccessControlList.PublicRead
        );

        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                if (state.equals(TransferState.COMPLETED)) {
                    Log.i("state changed", state+"");
                } else if (state.equals(TransferState.FAILED)) {
                    Log.e("state changed", state+"");
                }

            }
            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

            }
            @Override
            public void onError(int id, Exception ex) {

            }
        });
    }


}

问题是它在我运行程序时给了我一些错误:

访问 AndroidKeyStore 以检索 keyAlias 的密钥时出错:com.amazonaws.android.auth.aesKeyStoreAlias

检索用于从持久存储中解密数据的解密密钥时出错。 为请求的 dataKey 返回 null

我还要离开这里给 unauth 角色的内联策略,可能是这里的问题:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement0",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": "arn:aws:s3:::s3.android.test/*"
        }
    ]
}

这也发生在我身上,这是有效的

添加

-keep class com.amazonaws.mobileconnectors.s3.transferutility.TransferNetworkConnectionType {*;}

到解决我的问题的项目中的proguard-rules.pro文件。

这是我的 s3 依赖项

//amazon s3 bucket
implementation 'com.amazonaws:aws-android-sdk-core:2.15.1'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.6.23'
implementation 'com.amazonaws:aws-android-sdk-s3:2.15.1'
//implementation 'com.amazonaws:aws-java-sdk:1.11.404'
implementation 'com.amazonaws:aws-android-sdk-ddb:2.2.0'
implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.16.8') { transitive = true; }

这里看到

暂无
暂无

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

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