2016-05-03 23 views
1

私はAmazon S3からファイルをアップロードしてダウンロードするAndroidアプリを開発中です。私は自分のアプリケーションの認証を開発しました。現在、少数のファイルをアップロードしたいときはいつでも、すべてのファイルユーザに対して、私のバックエンドにpingを実行することによって、IdentityIDTokenが得られます。AWS CognitoとS3

私はいくつかの質問

  1. トークンの有効期限が切れている場合どのように私は知っていますがありますか?

  2. AWS APIセーブトークンとアイデンティティ自体?はいの場合、それらを取得する方法は?

  3. 私のプロジェクトにIdentityIdとトークンを使用する最も良い方法はありますか?すべてのファイルのバックエンドを呼び出してトークンを取得しますか?または、ユーザーがファイルの束(選択)をアップロードしたいときに一度電話しますか?トークンが有効期限切れでない場合、トークンを保存して再利用しますか?

CODE:

Auth.java:

public class Auth extends AWSAbstractCognitoDeveloperIdentityProvider { 



private Context ctx; 
public Auth(String accountId, String identityPoolId, Regions region,Context ctx) { 
    super(accountId, identityPoolId, region); 
     this.ctx=ctx; 

} 

@Override 
public String getProviderName() { 
    return "cognito-identity.amazonaws.com"; 

} 

@Override 
public String refresh() { 
    setToken(null); 

    if (getProviderName() != null && 
      !this.loginsMap.isEmpty() && 
      this.loginsMap.containsKey(getProviderName())&& internetchek.connectGoogle()) { 

     Log.d("Refreshing..","Loading.."); 
     Idtoken(); 

     update(identityId, token); 

     return token; 



    } else { 

     this.getIdentityId(); 
     return null; 
    } 

} 
@Override 
public String getToken() { 
    return token; 
} 

public void Idtoken(){ 



    String serverurl = constants.IP_ADDRESS_CREDENTIALS; 
    try { 
     save s = new save(this.ctx, constants.USER_DETAILS); 
     String phonenumber = s.read(constants.PHONE_NUMBER); 

     if (phonenumber != null) { 


      URL url = new URL(serverurl); 
      HttpURLConnection http = (HttpURLConnection) url.openConnection(); 
      http.setRequestMethod("POST"); 
      http.setDoInput(true); 
      http.setDoOutput(true); 

      OutputStream OS = http.getOutputStream(); 
      BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8")); 
      String data = URLEncoder.encode("number", "UTF-8") + "=" + URLEncoder.encode(phonenumber, "UTF-8"); 
      bufferedWriter.write(data); 
      bufferedWriter.flush(); 
      bufferedWriter.close(); 
      OS.close(); 
      InputStream IS = http.getInputStream(); 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1")); 
      String line = ""; 
      String response=""; 

      while ((line= bufferedReader.readLine()) != null) { 
       response = response+line; 

      } 
      bufferedReader.close(); 
      IS.close(); 
      http.disconnect(); 

      response = response.replaceAll("\\s+", ""); 

      Log.d("RESPONCE", response); 

      String[] splitter = response.split("=="); 
      if (splitter[0] != null) { 

       if (splitter[1] != null) { 

        identityId = splitter[0]; 
        token = splitter[1]; 

       } 

      } 
     } 
      Log.d("IDENTITYID",identityId); 
      Log.d("TOKEN",token); 

     }catch(MalformedURLException e){ 
      e.printStackTrace(); 
     }catch(UnknownHostException e) 
     { 
      e.printStackTrace(); 
     }catch(IOException e){ 
      e.printStackTrace(); 
     } 

}} 

Uploadfile私はぶっきらぼうな二つの方法を考えることができ

private class Uploadfile extends AsyncTask<Void,Void,Void>{ 

    Context ctx; 
    String remotepath; 
    File file; 
    ProgressBar progressBar; 

    private Uploadfile(Context ctx,File file,String remotepath,ProgressBar progressBar){ 
     this.ctx =ctx; 
     this.file=file; 
     this.remotepath=remotepath; 
     this.progressBar =progressBar; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     if(!internetchek.isNetworkAvailable(this.ctx)||!internetchek.connectGoogle()){ 
      Log.d("NETWORK","TRUE"); 
     }else { 
      Auth developerProvider = new Auth(
        null, 
        "ap-northeast-1:a871fa5fxxxxxxxxxxxxx1437244", 
        Regions.AP_NORTHEAST_1, this.ctx); 
      CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
        this.ctx.getApplicationContext(), 
        developerProvider, 
        Regions.AP_NORTHEAST_1); 


      HashMap<String, String> loginsMap = new HashMap<String, String>(); 
      loginsMap.put("cognito-identity.amazonaws.com", credentialsProvider.getToken()); 
      credentialsProvider.setLogins(loginsMap); 
      credentialsProvider.refresh(); 

      ClientConfiguration configuration = new ClientConfiguration(); 
      configuration.setProtocol(Protocol.HTTP); 
      configuration.setSocketTimeout(5 * 10000); 
      configuration.setConnectionTimeout(5 * 10000); 
      configuration.setMaxErrorRetry(3); 
      configuration.setMaxConnections(100); 

      if (sS3Client == null) { 

       sS3Client = new AmazonS3Client(credentialsProvider, configuration); 

      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 

     if(sS3Client!=null){ 

      sTransferUtility = new TransferUtility(sS3Client, this.ctx); 

      observer = sTransferUtility.upload(remotepath, file.getName(), file); 
      transferObservers.add(observer); 

      observer.setTransferListener(new UploadListener(this.progressBar,observer,file.getPath())); 

     } 

    } 
} 

答えて

0
  1. a。トークンを使用する単純なtry/catch aroundコード。適切な例外をスローした場合は、新しい例外を取得して再試行してください。

    b。開発者が認証したIDを使用して、トークンの有効な長さを構成します。アプリ内で簡単にそれを追跡し、有効期限が切れたときに適切に行動することができます。これは私が推薦するルートです。

  2. あなたはこれが何を意味するか分かりません。 SDKは、取得した後にSDKに格納します。 getIdentityId()またはgetCachedIdentityId()を使用してIDを取得できます。なぜトークンが必要ですか?

  3. 理想的な/推奨されるフローは、手動で何もする必要はありません。 SDKにそれらがあると、資格情報プロバイダを使用してS3にアクセスできる資格情報を取得できるだけです。これはトークンを保存して再利用することを意味しますが、これはあなたの追加作業ではありません。

EDIT:

あなたは新しいスレッドで資格情報プロバイダを毎回再作成するべきではありません。それは状態を失い、あなたは常にあなたのバックエンドに連絡する必要があります。これは、blog postとそのサンプル(そのブログ記事にリンクされている)に例示されているように、シングルトンでなければなりません。

また、cognito-identity.amazonaws.comではなく、コンソールに設定したプロバイダーのキーをプロバイダーに与える必要があります。

トークンを設定する方法も見えます。サンプルは次のようにログインを設定しています

CognitoSyncClientManager 
      .addLogins(
        ((DeveloperAuthenticationProvider) CognitoSyncClientManager.provider 
          .getIdentityProvider()).getProviderName(), 
        userName); 

私はサンプルに別の外観を与えるお勧めしますが、実装にいくつかの違いがあるように見えます。

+0

"これはトークンを保存して再利用することですが、もう一度あなたの部品に追加作業を加える必要はありません"というのは、APIがトークンを格納して自動的に再利用するということですか? – Naroju

+0

SDKは必ずしもAPIである必要はありません。 –

+0

のようになります。私は自分のコードに何かをねじ込んだ。私はここにそれを掲示します。コードを見てください – Naroju