我正在开发安卓应用程序,用户可以从Amazon S3上传和下载文件。我已经为我的应用程序开发了身份验证。目前,每当用户想上传几个文件时,每个文件用户都可以通过点击我的后端获得IdentityID和Token。
我有几个问题
AWS API是否保存令牌和标识本身?如果是的话,如何找回它们?IdentityId和令牌的最好方法是什么?为每个文件调用后端以获得令牌?或者当用户想上传一堆(选定的)文件时调用一次?或者保存令牌,并在令牌未过期时重用它?代码:
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()));
}
}
}发布于 2016-05-03 18:44:12
编辑:
不应该每次都在新线程中重新创建凭据提供程序。它将失去状态,您将不得不不断地联系您的后端。它应该是一个单例,如博客帖子和示例中的例子(在博客文章中链接)。
您还应该为提供程序提供您在控制台上配置的提供程序的密钥,而不是cognito Idty.amazonaws.com。
你设置令牌的方式也看不见了。示例设置的登录方式如下:
CognitoSyncClientManager
.addLogins(
((DeveloperAuthenticationProvider) CognitoSyncClientManager.provider
.getIdentityProvider()).getProviderName(),
userName);我建议再看一看这个示例,在实现方面似乎有一些不同。
https://stackoverflow.com/questions/37011498
复制相似问题