aws / aws-sdk-java

The official AWS SDK for Java 1.x (In Maintenance Mode, End-of-Life on 12/31/2025). The AWS SDK for Java 2.x is available here: https://github.com/aws/aws-sdk-java-v2/
https://aws.amazon.com/sdkforjava
Apache License 2.0
4.13k stars 2.83k forks source link

S3 Java SDK not working with Mule #1135

Closed raghukaramel closed 7 years ago

raghukaramel commented 7 years ago

I am building an application in Mule that uploads file to S3 using Java SDK and initialization of AmazonS3Client is failing at runtime. I am using Mule Enterprise Edition 3.4.0, Java 1.7 and S3 SDK 1.11.106.

Here is the code that I am using... any suggestions?

package com.intuit.s3.test;

import org.mule.api.MuleMessage; import org.mule.api.transformer.TransformerException; import org.mule.transformer.AbstractMessageTransformer;

import java.io.File;

import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.DefaultAWSCredentialsProviderChain; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.PutObjectRequest;

public class S3FileUpload extends AbstractMessageTransformer { private static String bucketName = "my-bucket-test123"; private static String keyName = "java-sdk-test-upload.txt"; private static String uploadFileName = "/tmp/s3-upload-test.txt";

@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
    DefaultAWSCredentialsProviderChain awsCredentialsProviderChain = new DefaultAWSCredentialsProviderChain();
    AmazonS3 s3Client = new AmazonS3Client(awsCredentialsProviderChain.getCredentials());
    try {
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        s3Client.putObject(new PutObjectRequest(bucketName, keyName, file));

     } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " +
                "means your request made it " +
                "to Amazon S3, but was rejected with an error response" +
                " for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " +
                "means the client encountered " +
                "an internal error while trying to " +
                "communicate with S3, " +
                "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
    return null;
}

}

kiiadi commented 7 years ago

What's the problem you're seeing?

raghukaramel commented 7 years ago

It hangs at line - AmazonS3 s3Client = new AmazonS3Client(awsCredentialsProviderChain.getCredentials());

I don't see any error or any messages

kiiadi commented 7 years ago

Where are you expecting credentials to be loaded from? ie: which credential provider in the provider chain.

Are you running this on EC2?

kiiadi commented 7 years ago

Sorry, I'm not that familiar with mule.

raghukaramel commented 7 years ago

~/.aws/credentials

raghukaramel commented 7 years ago

No I am running it on my local. Mule is a spring based application container - more info can be found here - https://www.mulesoft.com/platform/mule

kiiadi commented 7 years ago

Can you try this:

AmazonS3 s3 = AmazonS3Client.builder().withCredentials(new ProfileCredentialsProvider()).build();
raghukaramel commented 7 years ago

I tried that and couple other ways to create S3Client, but all of them fail the same way. Here are the different things I tried

    //BasicAWSCredentials awsCreds = new BasicAWSCredentials("abc", "xyz");
    //AmazonS3 s3Client = new AmazonS3Client(awsCreds);

    //AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentialsProviderChain.getCredentials())).build();

    //AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider());
kiiadi commented 7 years ago

have you been able to step through to see where it's hanging?

raghukaramel commented 7 years ago

Yes, when I am stepping through the code, the cursor disappears for few seconds on the above line of code and then the control get transferred to line 76 of the below code - /*

import org.mule.api.MessagingException; import org.mule.api.MuleEvent; import org.mule.api.MuleException; import org.mule.api.construct.FlowConstruct; import org.mule.api.construct.Pipeline; import org.mule.api.processor.MessageProcessor; import org.mule.context.notification.MessageProcessorNotification; import org.mule.context.notification.ServerNotificationManager;

/**

kiiadi commented 7 years ago

I'm sorry, I don't see line numbers. Can you link me to a line in the repo? What version of the SDK are you using?

raghukaramel commented 7 years ago

Here is the repo link - https://github.com/mulesoft/mule/blob/mule-3.4.1-HF1/core/src/main/java/org/mule/execution/MessageProcessorNotificationExecutionInterceptor.java

I am currently using AWS S3 SDK 1.11.106, I also tried the latest version 1.11.124 and few other older versions as well 1.10.x, 1.9.x etc. I am seeing the same behavior with all different versions.

kiiadi commented 7 years ago

This looks like mule code - I'm looking for what part of the SDK you feel this is a problem with...

raghukaramel commented 7 years ago

not sure about... that's where I am looking for help

kiiadi commented 7 years ago

This repo is for the Aws Java SDK, the code you've posted isn't from our library. Have you tried asking in the mule repository? I'm unsure how else I'm able to help as you indicate the problem is not with the AWS Java SDK.

raghukaramel commented 7 years ago

I agree, it looks like a Mule issue. I have opened a case with Mule and working with them currently.

kiiadi commented 7 years ago

Closing this for now, feel free to re-open if you feel this is an issue with the SDK.