私はAmazon S3
からファイルをアップロードしてダウンロードするAndroidアプリを開発中です。私は自分のアプリケーションの認証を開発しました。現在、少数のファイルをアップロードしたいときはいつでも、すべてのファイルユーザに対して、私のバックエンドにpingを実行することによって、IdentityID
とToken
が得られます。AWS CognitoとS3
私はいくつかの質問
トークンの有効期限が切れている場合どのように私は知っていますがありますか?
AWS API
セーブトークンとアイデンティティ自体?はいの場合、それらを取得する方法は?私のプロジェクトに
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()));
}
}
}
"これはトークンを保存して再利用することですが、もう一度あなたの部品に追加作業を加える必要はありません"というのは、APIがトークンを格納して自動的に再利用するということですか? – Naroju
SDKは必ずしもAPIである必要はありません。 –
のようになります。私は自分のコードに何かをねじ込んだ。私はここにそれを掲示します。コードを見てください – Naroju