簡體   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