将STS与AWSS3TransferManagerUploadRequest和AWSS3TransferManagerDownloadRequest集成

时间:2014-09-30 10:59:15

标签: ios7 amazon-web-services amazon-s3 amazon-cognito

我们正在尝试在Android和iOS应用中实施AWS Security Token Service。在后端,我们使用下面的代码生成令牌:

public class CloudManagementImpl implements CloudManagement{

    private static final Logger Log = LoggerFactory.getLogger(CloudManagementImpl.class);

    @Override
    public CloudConfiguration getCloudProperties() {

        CloudConfiguration CloudConfiguration = new CloudConfiguration();

        AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest();
        assumeRoleRequest.setRoleArn(JiveGlobals.getProperty(XYZConstant.AWS_ARN_EC2_ROLE_MAP));
        assumeRoleRequest.setRoleSessionName(XYZConstant.AWS_ROLE_SESSIONNAME);
        assumeRoleRequest.setDurationSeconds(JiveGlobals.getIntProperty(XYZConstant.AWS_CREDENTIALS_LIFETIME, 1800));

        AWSSecurityTokenServiceClient stsClient = new AWSSecurityTokenServiceClient();
        AssumeRoleResult assumeRoleResult = stsClient.assumeRole(assumeRoleRequest);
        if (assumeRoleResult != null) {
            Credentials sessionCredentials = assumeRoleResult.getCredentials();
            CloudConfiguration.setAwsAccessId(sessionCredentials.getAccessKeyId());
            CloudConfiguration.setAwsAccessKey(sessionCredentials.getSecretAccessKey());
            CloudConfiguration.setToken(sessionCredentials.getSessionToken());
            CloudConfiguration.setAwsMainBucket(JiveGlobals.getProperty(XYZConstant.AWS_MAIN_BUCKET));
        } else {
            Log.error("Cloud Management :: Propery values not configured ");
        }

        return CloudConfiguration;
    }

}

然后通过单独的Web服务调用在iOS和Android应用程序中获取生成的令牌。

在android中我们使用下面的代码来使用检索到的令牌:

public S3Client(String accessKey, String secretKey, String token, String bucketName) {
        super();
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucketName = bucketName;
        BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(accessKey, secretKey, token);
        amazonS3Client = new AmazonS3Client(basicSessionCredentials);

    }

问题是 -

  

我们在适用于iOS的AWS mobile SDK版本2中没有类似Android的API,   使用我们可以使用检索到的令牌,也许是最好的方法   在iOS中实现这一点是通过AWSCognitoCredentialsProvider,   但我们不确定。

请建议 - 在iOS中集成AWS Security Token Service的最佳方式是什么。

1 个答案:

答案 0 :(得分:2)

您需要通过符合AWSCredentialsProvider来实施自己的凭据提供程序。听起来你已经有了一个代码片段,可以从你的服务器中检索临时凭证。该逻辑应该进入您的自定义凭据提供程序。您可以查看AWSWebIdentityCredentialsProviderAWSCognitoCredentialsProvider的实施,了解如何实施自己的凭据提供程序。