为什么会出现AmazonS3Exception?

huangapple go评论78阅读模式
英文:

Why am I getting a AmazonS3Exception?

问题

我的任务是将上传到S3的文件进行加密。在加密之前,上传工作正常,但是在我对文件进行加密后,出现了以下异常。

所提供的 XML 不符合格式要求,或未根据我们发布的架构进行验证

我将以下代码添加到现有代码中。

		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);
		}

我的想法是,为什么AmazonS3希望是XML文件?而不是普通的文本文档?
是否有选项可以通过存储桶策略进行更改?

编辑
这是上传代码,可能存在问题。我不明白为什么没有加密时它能正常工作。

             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);
英文:

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.<br/><br/>
The XML you provided was not well-formed or did not validate against our published schema
<br/><br/>
I added this to the existing Code <br/><br/>

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

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

EDIT <br/>
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&lt;PartETag&gt; partETags = new ArrayList&lt;PartETag&gt;();
			 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 &lt; 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);

答案1

得分: 0

也许下次我应该停止复制互联网上的随机代码。<br/>
你应该使用FileOutputStream来写,而不是反过来。所以文件是空的,这导致了异常。

CryptoInputStream&lt;KmsMasterKey&gt; encryptingStream = crypto.createEncryptingStream(crypt, in);
FileOutputStream out = null;

try {
    out = new FileOutputStream(encryptfile);
    IOUtils.copy(encryptingStream, out);
    encryptingStream.close();
    out.close();
} catch (IOException e)
英文:

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

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

</details>



huangapple
  • 本文由 发表于 2020年5月4日 23:18:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61595637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定