Error during upload request for amazon S3

0

When trying to upload an image is giving problems and would like to know what the problem is and how to solve it.

Amazon request:

public void amazon() {
        String fileName = photoFile.getName();
        long mTotalFileBytes;

        ObjectMetadata metadata = new ObjectMetadata();

        metadata.setContentLength(mTotalFileBytes = photoFile.length());
        metadata.setContentType("image/".concat(fileName));

        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "us-east-1:my-cognito-code",
                Regions.US_EAST_1
        );

        amazonS3Client = new AmazonS3Client(credentialsProvider);
        amazonS3Client.setRegion(Region.getRegion(Regions.SA_EAST_1));
        try {
            por = new PutObjectRequest(bucket, fileName, new FileInputStream(photoFile), metadata);
            if (postImageAsyncTask == null) {
                postImageAsyncTask = new PostImageAsyncTask();
            }
            postImageAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        amazonUrl = amazonS3Client.getResourceUrl(bucket, fileName);
        Log.e(">>>>>URL", ">>>>>" + amazonUrl);


    }

AsyncTask:

public class PostImageAsyncTask extends AsyncTask<Object, Object, PutObjectResult> {

    public PostImageAsyncTask() {
        super();

    }

    @Override
    protected PutObjectResult doInBackground(Object... params) {
        try {
            return amazonS3Client.putObject(por);
        } catch (AmazonServiceException e) {
            return null;
        } catch (AmazonClientException f) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(PutObjectResult result) {
        if (!isCancelled()) {
            if (result != null) {
                docsIDController.pictureFinishedSendingToS3(amazonUrl);
            } else {
                postImageAsyncTask.cancel(true);

                if (RECONECT_TIME_DELAY < MAX_TIME_DELAY) {
                    if ((snackbar == null || !snackbar.isShown())) {
                        snackbar = Snackbar.make(coordinatorLayoutView, "Ocorreu um problema de conexão, aguardando dispositivo se reconectar", Snackbar.LENGTH_INDEFINITE);
                        snackbar.setActionTextColor(Color.BLUE);
                        snackbar.setAction("Cancelar", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                finishedTakingDocIDPicture(null);
                            }
                        });
                        snackbar.show();
                    }
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            RECONECT_TIME_DELAY = RECONECT_TIME_DELAY * 2;
                            postImageAsyncTask.cancel(true);
                            postImageAsyncTask = new PostImageAsyncTask();
                            postImageAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                        }
                    }, RECONECT_TIME_DELAY);
                }
            }
        } else {
            postImageAsyncTask.cancel(true);
        }
    }
}

Stack trace:

I/AmazonHttpClient: Unable to execute HTTP request: Write error: ssl=0x5c15d378: I/O error during system call, Broken pipe
                                                                 javax.net.ssl.SSLException: Write error: ssl=0x5c15d378: I/O error during system call, Broken pipe
                                                                     at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_write(Native Method)
                                                                     at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:717)
                                                                     at libcore.net.http.FixedLengthOutputStream.write(FixedLengthOutputStream.java:41)
                                                                     at com.amazonaws.http.UrlHttpClient.write(UrlHttpClient.java:172)
                                                                     at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:129)
                                                                     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.s3.AmazonS3Client.invoke(AmazonS3Client.java:4204)
                                                                     at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1618)
                                                                     at com.foregon.forid.pictureManagement.boundary.DocsIDUI$PostImageAsyncTask.doInBackground(DocsIDUI.java:488)
                                                                     at com.foregon.forid.pictureManagement.boundary.DocsIDUI$PostImageAsyncTask.doInBackground(DocsIDUI.java:478)
                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                     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)
    
asked by anonymous 20.01.2016 / 19:29

1 answer

1

Aleff, I'm going to share with you a utilitarian class that I myself made to send images to S3. I still keep using it and it works super well:

public class AmazonS3Utils {
    private static final String MY_ACCESS_KEY_ID = "SUA_ACCESS_KEY_ID";
    private static final String MY_SECRET_KEY = "SUA_SECRET_KEY";
    private static final String TAG = AmazonS3Utils.class.getSimpleName();
    private static AmazonS3Client amazonS3Client;

    // FIXME: pode retornar null
    public static PutObjectRequest createPutObjectRequest(@NonNull String bucketName, @NonNull String objectName,
                                                          @NonNull File file) {
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);
        return putObjectRequest;
    }

    // Exceção UnknownHostException é disparada caso não haja conexão com a internet
    public static void putObject(@NonNull PutObjectRequest putObjectRequest) throws UnknownHostException {
        getS3Client().putObject(putObjectRequest);
    }

    public static String generateImageUrl(@NonNull String bucketName, @NonNull String objectName) {
        return getS3Client().getResourceUrl(bucketName, objectName);
    }

    private static AmazonS3Client getS3Client() {
        if (amazonS3Client == null) {
            ClientConfiguration clientConfiguration = new ClientConfiguration();
            // 60 segundos
            clientConfiguration.setSocketTimeout(60 * 1000); // milliseconds
            amazonS3Client = new AmazonS3Client(new BasicAWSCredentials(MY_ACCESS_KEY_ID, MY_SECRET_KEY),
                    clientConfiguration);
            return amazonS3Client;
        }
        return amazonS3Client;
    }
}

One important thing to note is that perhaps because you do not set up any metadata like you do here metadata.setContentType("image/"); , at the time of retrieving there is no image metadata, such as guidance, for example. This has no problem for my scope, it depends on whether you want to adapt to your case.

    
25.01.2016 / 21:15