首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AWS认知与S3

AWS认知与S3
EN

Stack Overflow用户
提问于 2016-05-03 18:15:38
回答 1查看 401关注 0票数 1

我正在开发安卓应用程序,用户可以从Amazon S3上传和下载文件。我已经为我的应用程序开发了身份验证。目前,每当用户想上传几个文件时,每个文件用户都可以通过点击我的后端获得IdentityIDToken

我有几个问题

  1. 如何知道令牌是否过期?
  2. AWS API是否保存令牌和标识本身?如果是的话,如何找回它们?
  3. 在我的项目中使用IdentityId和令牌的最好方法是什么?为每个文件调用后端以获得令牌?或者当用户想上传一堆(选定的)文件时调用一次?或者保存令牌,并在令牌未过期时重用它?

代码:

Auth.java:

代码语言:javascript
复制
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

代码语言:javascript
复制
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()));

        }

    }
}
EN

回答 1

Stack Overflow用户

发布于 2016-05-03 18:44:12

  1. 我可以马上想到两种方法: a.简单的尝试/捕捉使用令牌的代码。如果它抛回一个适当的异常,获取一个新的异常并重试。 通过开发人员身份验证,您可以配置令牌的有效长度。你可以很容易地在应用程序中跟踪它,并在它过期时采取适当的行动。这是我推荐的路线。
  2. 我不知道你这么说是什么意思。SDK会在检索到它们之后存储它们。您可以使用getIdentityId()或getCachedIdentityId()获得id,为什么需要令牌?
  3. 理想的/推荐的流程是您不必手动对它们做任何操作。一旦SDK拥有了它们,您就应该能够使用凭据提供程序来获取您可以访问S3的凭据。这意味着保存令牌并重用它,但这不应该是您的额外工作。

编辑:

不应该每次都在新线程中重新创建凭据提供程序。它将失去状态,您将不得不不断地联系您的后端。它应该是一个单例,如博客帖子和示例中的例子(在博客文章中链接)。

您还应该为提供程序提供您在控制台上配置的提供程序的密钥,而不是cognito Idty.amazonaws.com。

你设置令牌的方式也看不见了。示例设置的登录方式如下:

代码语言:javascript
复制
CognitoSyncClientManager
            .addLogins(
                    ((DeveloperAuthenticationProvider) CognitoSyncClientManager.provider
                            .getIdentityProvider()).getProviderName(),
                    userName);

我建议再看一看这个示例,在实现方面似乎有一些不同。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37011498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档