Android AWS S3 TransferUtility以静默方式忽略ClientConfiguration中的代理设置

时间:2017-04-13 15:32:07

标签: android amazon-web-services amazon-s3 proxy

我使用适用于Android的AWS开发工具包将对象从S3下载到我的Android平板电脑。为了通过代理路由流量(现在我在我的本地网络上使用Charles Proxy作为测试台)来限制S3下载的下载速度,我使用ClientConfiguration.setProxyHost和setProxyPort但S3 TransferUtility似乎忽略了设置并直接转到AWS S3而不是代理。

没有代理集的代码工作正常。对象下载成功。代理集的代码行为完全相同,代理不会显示平板电脑的任何连接。

我已经证明代理的工作原理是在Android平板电脑Wifi设置中设置代理主机和端口(手动设置代理),代理显示成功通过代理的AWS S3连接。

起初,我认为AmazonS3Client完全忽略了我的ClientConfiguration,但我已经证明,当我将ClientConfiguration.setProtocol从HTTPS设置为HTTP时,AWS TransferUtility会从http更改为https作为AWS端点。

代码看起来像每个其他示例代码,用于设置我可以使用ClientConfiguration和TransferUtility找到的代理。

ClientConfiguration getClientConfiguration() {
    return new ClientConfiguration()
            .withMaxConnections(2)
            .withProxyHost("192.168.1.137")
            .withProxyPort(8888)
            .withConnectionTimeout(30 * 1000) // Wait 30 seconds to make a connection
            .withSocketTimeout(0); // Keep open connections open indefinitely (we have very large downloads)
}
public S3Controller(Context context) {
    java.util.logging.Logger.getLogger("com.amazonaws").setLevel(java.util.logging.Level.FINEST);
    applicationContext = context.getApplicationContext();

    clientConfig = getClientConfiguration();

    s3Client = new AmazonS3Client(getCredentials(), clientConfig);

    transferUtility = new TransferUtility(s3Client, applicationContext);

    pendingTransfers = new PendingTransferQueue();

}

// ...稍后

TransferObserver observer = transferUtility.download(BuildConfig.S3_BUCKET, key, file);

正如您所看到的,我还尝试将amazonaws的日志记录级别设置为FINEST,以便在代理失败的位置获得一些可见性,而不会显示其他日志记录。

有关为什么AWS SDK忽略我的代理或者如何设置日志记录以查找有关失败位置的更多详细信息的任何建议?

提前致谢。

1 个答案:

答案 0 :(得分:1)

所以我发现我可以实例化com.amazonaws.http.ApacheHttpClient类,它将使用受保护的HttpClientFactory来配置clientConfig中的代理集。

AmazonS3Client的创建使用不同的构造函数来传递具有如下配置代理的HttpClient:

HttpClient httpClient = new ApacheHttpClient(clientConfig);

s3Client = new AmazonS3Client(getCredentialsProvider(), clientConfig, httpClient);