Search code examples
javaamazon-web-servicesamazon-s3aws-java-sdk

Why am I getting a AmazonS3Exception?


my task is to encrypt a file that is uploaded to S3. The upload worked fine before the encryption but now after I encrypted the file I get this exception.

The XML you provided was not well-formed or did not validate against our published schema

I added this to the existing Code

        final AwsCrypto crypto = new AwsCrypto();

        try (
            final FileInputStream in = new FileInputStream(encryptfile);
            final FileOutputStream out = new FileOutputStream(file);
            final CryptoOutputStream<?> encryptingStream = crypto.createEncryptingStream(crypt, out)) 
        {
            IOUtils.copy(in, encryptingStream);
        }


My thoughts, Why does AmazonS3 expect a XML-File ? Why not a normal text document ?
Is there a Option to change this maybe with the Bucket Policy ?

EDIT
That is the upload code, maybe there is a Issue. I dont understand why it´s working without the encryption.

             File uploaffile = encryptFile(file);
             List<PartETag> partETags = new ArrayList<PartETag>();
             String filename = String.valueOf(System.currentTimeMillis());

             InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(awss3bucket, filename);
             InitiateMultipartUploadResult initResponse = amazons3.initiateMultipartUpload(initRequest);
             long partSize = 5 * 1024 * 1024; 
             long contentLength = uploaffile.length();
             long filePosition = 0;
             for (int i = 1; filePosition < contentLength; i++) {
                 partSize = Math.min(partSize, (contentLength - filePosition));

                 UploadPartRequest uploadRequest = new UploadPartRequest()
                         .withBucketName(awss3bucket)
                         .withKey(filename)
                         .withUploadId(initResponse.getUploadId())
                         .withPartNumber(i)
                         .withFileOffset(filePosition)
                         .withFile(uploaffile)
                         .withPartSize(partSize);

                 PartETag petag = new PartETag(amazons3.uploadPart(uploadRequest).getPartNumber(), amazons3.uploadPart(uploadRequest).getETag());
                 partETags.add(petag);

                 filePosition += partSize;
             }

             CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(awss3bucket, filename,
                                                                                            initResponse.getUploadId(), partETags);

             amazons3.completeMultipartUpload(compRequest);

Solution

  • Maybe next time I should stop to copy some random Code from the Internet.
    You use The FileoutputStream to write not the other way. So the File so was Empty which created the Exception.

            CryptoInputStream<KmsMasterKey> encryptingStream = crypto.createEncryptingStream(crypt, in);
            FileOutputStream out = null;
    
            try {
                out = new FileOutputStream(encryptfile);
                IOUtils.copy(encryptingStream, out);
                encryptingStream.close();
                out.close();
            } catch (IOException e)