发布whille上传到AWS S3存储桶

时间:2021-11-14 01:32:42

I am trying to upload the image files to aws bucket but I am facing this error some times on the devices,

我正在尝试将图像文件上传到aws存储桶但我在设备上有时会遇到此错误,

I/AmazonHttpClient: Unable to execute HTTP request: Unable to resolve host "cognito-identity.us-east-1.amazonaws.com": No address associated with hostname
                                                                  java.net.UnknownHostException: Unable to resolve host "cognito-identity.us-east-1.amazonaws.com": No address associated with hostname
                                                                      at java.net.InetAddress.lookupHostByName(InetAddress.java:400)
                                                                      at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
                                                                      at java.net.InetAddress.getAllByName(InetAddress.java:220)
                                                                      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
                                                                      at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
                                                                      at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
                                                                      at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
                                                                      at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
                                                                      at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
                                                                      at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
                                                                      at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:441)
                                                                      at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
                                                                      at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
                                                                      at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
                                                                      at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
                                                                      at libcore.net.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:280)
                                                                      at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:128)
                                                                      at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
                                                                      at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
                                                                      at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
                                                                      at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:533)
                                                                      at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:344)
                                                                      at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:170)
                                                                      at com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider.refresh(AWSEnhancedCognitoIdentityProvider.java:76)
                                                                      at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:537)
                                                                      at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:370)
                                                                      at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:440)
                                                                      at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:76)
                                                                      at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4198)
                                                                      at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1618)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.uploadSinglePartAndWaitForCompletion(UploadTask.java:174)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:74)
                                                                      at com.amazonaws.mobileconnectors.s3.transferutility.UploadTask.call(UploadTask.java:40)
                                                                      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
                                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:137)
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
                                                                      at java.lang.Thread.run(Thread.java:856)

But after some time, it shows like,

但过了一段时间,它表明,

D/CognitoCachingCredentialsProvider: Identity id is changed
12-12 11:27:25.771 11239-14564/D/CognitoCachingCredentialsProvider: Saving identity id to SharedPreferences
12-12 11:27:25.771 11239-11246/com.amlooking4 I/dalvikvm: Jit: resizing JitTable from 4096 to 8192
12-12 11:27:26.161 11239-14564/D/CognitoCachingCredentialsProvider: Saving credentials to SharedPreferences

EDITED :

编辑:

Now I am getting this too ,

现在我也得到了这个,

    I/AmazonHttpClient: Unable to execute HTTP request: SSL handshake timed out
                                                                      java.net.SocketTimeoutException: SSL handshake timed out

 Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x1e5dff8: Failure in SSL library, usually a protocol error
                                                                  error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:658 0x401d6cf5:0x00000000)
                                                                      at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method)
                                                                      at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
                                                                        ... 23 more

Really getting frustrated because of this misbehaviour, the code is same but some time it executes perfectly, some times it does not and show the above written error.

由于这种不当行为真的感到沮丧,代码是相同的,但有时它完美地执行,有时它没有并显示上述书面错误。

I am new to aws, I understand that it saves the cognito id into preferneces.

我是aws的新手,我明白它可以将认知内容保存到首选项中。

1 个解决方案

#1


1  

add this code to the Utils.Java

将此代码添加到Utils.Java中

    private static CognitoCachingCredentialsProvider getCredProvider(Context context) {
    if (sCredProvider == null) {
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeout(30000);
        clientConfiguration.setSocketTimeout(30000);
        sCredProvider = new CognitoCachingCredentialsProvider(
                context.getApplicationContext(),
                Constants.COGNITO_POOL_ID,
                Regions.fromName(Constants.COGNITO_POOL_REGION),clientConfiguration);
    }
    return sCredProvider;
};    

and the amazonS3Client suppose to look like this :

而amazonS3Client假设看起来像这样:

     public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.fromName(Constants.BUCKET_REGION)));

    }
    return sS3Client;
};

#1


1  

add this code to the Utils.Java

将此代码添加到Utils.Java中

    private static CognitoCachingCredentialsProvider getCredProvider(Context context) {
    if (sCredProvider == null) {
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setConnectionTimeout(30000);
        clientConfiguration.setSocketTimeout(30000);
        sCredProvider = new CognitoCachingCredentialsProvider(
                context.getApplicationContext(),
                Constants.COGNITO_POOL_ID,
                Regions.fromName(Constants.COGNITO_POOL_REGION),clientConfiguration);
    }
    return sCredProvider;
};    

and the amazonS3Client suppose to look like this :

而amazonS3Client假设看起来像这样:

     public static AmazonS3Client getS3Client(Context context) {
    if (sS3Client == null) {
        sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
        sS3Client.setRegion(Region.getRegion(Regions.fromName(Constants.BUCKET_REGION)));

    }
    return sS3Client;
};