GitCoder
GitCoder

Reputation: 121

Upload file to S3 using java

I want to upload a file to S3 bucket using maven java project.

void uploadFile(String filePath,String fileName) throws IOException{
    String bucketName="my-feeds/prod/myfiles/myData";
    String currentDir = System.getProperty("user.dir");
    input = new FileInputStream(currentDir+"/src/main/resources/keyDetails.properties");
    prop.load(input);
    String YourAccessKeyID = prop.getProperty("accessKey");
    String YourSecretAccessKey = prop.getProperty("secretKey");
    AWSCredentials credentials = new BasicAWSCredentials(YourAccessKeyID, YourSecretAccessKey);
    AmazonS3 s3client = new AmazonS3Client(credentials);
    s3client.putObject(bucketName, fileName, new File(filePath));
}

But am getting exception :

Exception in thread "main" java.lang.NoSuchMethodError: com.amazonaws.AmazonWebServiceRequest.copyPrivateRequestParameters()Ljava/util/Map;

What can be reason ? Please help.

My pom.xml has this as dependency :

<dependency>
                <groupId>com.amazonaws</groupId>
                <artifactId>aws-java-sdk-s3</artifactId>
                <version>1.9.0</version>
            </dependency>

Now getting this exception

Exception in thread "main" com.amazonaws.services.s3.model.AmazonS3Exception: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. (Service: Amazon S3; Status Code: 301; Error Code: PermanentRedirect; " 

After I update the version

Upvotes: 0

Views: 1183

Answers (2)

Kayaman
Kayaman

Reputation: 73528

The bucket name should be just my-feeds. The prod/myfiles/myData part is part of the key, not the bucket.

Upvotes: 1

Mark B
Mark B

Reputation: 200436

Your bucket name is invalid. The string my-feeds/prod/myfiles/myData looks like the directory structure you are trying to create, not the name of an S3 bucket.

Go into your S3 console and find the name of the S3 bucket you created, and use that as the bucket name. S3 doesn't have directories, just objects, so to emulate a directory structure you need to put those "directory names" in the object key. In other words the second parameter you are passing to putObject() needs to look something like: "/my-feeds/prod/myfiles/myData/" + filename

Upvotes: 0

Related Questions