0

I tried to find a solution on google since 2 days but didn't have any solution.

DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
TransferManager tx = new TransferManager(credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(existingBucketName, keyName, new File("C:/Users/Desktop/Management_System.jpg"));
    // While the transfer is processing, you can work with the transfer object
while (myUpload.isDone() == false) {
    System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
}
myUpload.waitForCompletion();
tx.shutdownNow();

This code is working fine and uploading an image Management_System.jpg, which is placed into my desktop to aws s3 bucket successfully. I have an html form with a file field. I want to browse any image in mycomputer and upload it into my s3 bucket. I also tried this below code and it works fine on my localhost when i test it on amazon ec2 then it throw error something like this "ImageIO write Invalid native argument". I've file field into my form name [photo]. So my code is as below:

BufferedImage src = ImageIO.read(photo.getInputStream());
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(src, "JPG", os);
byte[] buffer = os.toByteArray();
InputStream is = new ByteArrayInputStream(buffer);

AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());

if(isValidFile(s3client, existingBucketName, info.getPhoto())){
    s3client.deleteObject(new DeleteObjectRequest(existingBucketName, info.getPhoto()));
}

ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(buffer.length);
meta.setContentType(photo.getContentType());
s3client.putObject(new PutObjectRequest(existingBucketName, keyName, is, meta));
s3client.setObjectAcl(existingBucketName, keyName, CannedAccessControlList.PublicRead);

Please guide me where i'm doing wrong? I'm using tomcat 7 on ec2 instance and my localhost version is also tomcat 7. I don't understand that is it version compatibility issue or something else...

Updated Answer:

Finally i make it work. Here is mine full file upload code through html form field. I'm posting it here to help others please vote me up if you find it helpfull. thanks

AmazonS3 s3 = new AmazonS3Client(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
            TransferManager tx = new TransferManager(new PropertiesCredentials(DashboardController.class.getResourceAsStream("/AWSCredentials.properties")));
            //AmazonS3 s3 = new AmazonS3Client(new DefaultAWSCredentialsProviderChain());
            if(isValidFile(s3, BucketName, info.getPhoto())){
                s3.deleteObject(new DeleteObjectRequest(BucketName, info.getPhoto()));
            }
            S3Object s3Object = new S3Object();
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(photo.getContentType());
            meta.setContentLength(photo.getSize());
            meta.setHeader("filename", NewkeyName);
            ByteArrayInputStream bis = new ByteArrayInputStream(photo.getBytes());
            s3Object.setObjectContent(bis);

            //s3.putObject(new PutObjectRequest(BucketName, NewkeyName, bis, omd));
            Upload myUpload = tx.upload(BucketName, NewkeyName, bis, meta);
            while (myUpload.isDone() == false) {
                System.out.println(myUpload.getProgress().getPercentTransferred() + "%");
            }
            if(myUpload.isDone() == true){
                System.out.println("100%");
            }
            s3.setObjectAcl(BucketName, NewkeyName, CannedAccessControlList.PublicRead);
            myUpload.waitForCompletion();
            tx.shutdownNow();

            s3Object.close();
user3370108
  • 83
  • 2
  • 11
  • you have some code. What doesn't work? – tedder42 Mar 23 '15 at 16:33
  • Hi tedder42, thanks for your response. Please have a look over my question again. I uploaded my code. – user3370108 Mar 23 '15 at 16:58
  • Hi mate, I have already spent longer than a week trying to figure out exactly the same issue. Thanks a lot for the code-help. I am still receiving a lot of error. May i know which packages i will need to import in my code? – VD007 Sep 07 '15 at 05:35
  • Getting this error : com.amazonaws.services.s3.model.AmazonS3Exception: AWS authentication requires a valid Date or x-amz-date header (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: D27A13B41C01AF88), S3 Extended Request ID: SnDRF7tppd0ADkOfi/npzgsk37eX7S2wrRAB4WWFM+i5n03dt2juimTyhBTw0B+mvmsX0tdAZR8= Please assist – Shubhi224 Nov 14 '16 at 18:05
  • Check your server timestamp if getting invalid date. – saurabheights Jun 23 '17 at 10:24

1 Answers1

1

The issue seems to be before S3 code.

The error message relate to the ImageIO.write.

A litle search show that OpenJDK doesn't include JPEG support.

You can test this part of the code localy without S3 (Make sure to use same JRE).

Any way why are you using ImageIO? Do you transcode before putting it in S3

Community
  • 1
  • 1
Kazaag
  • 2,115
  • 14
  • 14
  • Hi Kazaag, thanks for your response. I'm new to java programming coming from a php background. So i'm finding the easiest way to upload a file from form file field to s3. Can you assist me regarding this matter with the help of example code? – user3370108 Mar 23 '15 at 20:25
  • If you are only using the ImageIO to get a byte array, http://stackoverflow.com/questions/1264709/convert-inputstream-to-byte-array-in-java will help you to find another way. To get the Oracle/Sun JDK you can go here: http://www.oracle.com/technetwork/java/javase/downloads/index.html. The open JDK is an open source version of the JDK with any element with potential external patent removed. – Kazaag Mar 23 '15 at 20:43
  • Hi Kazaag, Thanks for your response. I've look over your provided examples. There's a lot of confusion in provided solution. Kindly can you assist me regarding my codes. I'would appreciate it if you assist me with my codes so that i can easily implement it. thanks – user3370108 Mar 23 '15 at 21:54