猿问

数据读取的长度与预期不同:dataLength=964481363;

我正在尝试从 S3 读取加密对象,然后将其上传到 S3 中具有不同加密密钥的新存储桶。当我尝试读取对象时,使用getObject()并尝试使用PutObject请求放置对象。


当我打印源的内容长度时,它是964481376。那么,数据长度来自哪里呢?这是964481363


我的源文件的元数据如下所示:


内容类型


二进制/八进制流


x-amz-meta-x-amz-unencrypted-content-length


964481363


x-amz-meta-x-amz-wrap-alg


公里


我无法共享 KMS 密钥。


public boolean copyS3Object(AmazonS3Encryption sourceS3Client, AmazonS3URI sourceS3Uri,

            AmazonS3Encryption destS3Client, AmazonS3URI destS3Uri) throws AmazonServiceException, IOException {

        //String eTag;

        S3Object sourceS3Object = null;


        try {

            //Get the Source object stream

            sourceS3Object = sourceS3Client.getObject(sourceS3Uri.getBucket(), sourceS3Uri.getKey());

            ObjectMetadata objectMetadata = sourceS3Object.getObjectMetadata();

            System.out.println("content length " + objectMetadata.getContentLength());

            PutObjectRequest putRequest = new PutObjectRequest(destS3Uri.getBucket(), destS3Uri.getKey(),

                    sourceS3Object.getObjectContent(), sourceS3Object.getObjectMetadata());


            destS3Client.putObject(putRequest);

//

//            if (eTag.isEmpty()) {

//                System.out.println("Copy failed, New object in " + destS3Uri.toString() + " is empty");

//                return false;

//            }


        } catch (AmazonServiceException e) {

            throw e;

        } finally {

            if (sourceS3Object != null) {

                sourceS3Object.close();

            }

        }

        //System.out.println("Copied successfully to " + destS3Uri.toString() + " Etag:" + eTag);

        return true;

    }


吃鸡游戏
浏览 102回答 1
1回答

蛊毒传说

我弄清楚了问题所在。S3 对象的内容长度是加密的数据长度。但是当我放置对象时,我试图放置未加密的数据。因此,我需要将对象的内容长度显式设置为未加密的长度。下面的代码解决了这个问题。&nbsp;try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Get the Source object stream&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceS3Object = sourceS3Client.getObject(sourceS3Uri.getBucket(), sourceS3Uri.getKey());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //For a decrypted object, the content length in metadata has the encrypted length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Set the content length to the unencrypted-data-length&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ObjectMetadata objectMetadata = sourceS3Object.getObjectMetadata();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Map<String, String> modelMetaData = objectMetadata.getUserMetadata();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Long unencryptedDataLength = Long.parseLong(modelMetaData.get(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "x-amz-unencrypted-content-length"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objectMetadata.setContentLength(unencryptedDataLength);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PutObjectRequest putRequest = new PutObjectRequest(destS3Uri.getBucket(), destS3Uri.getKey(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceS3Object.getObjectContent(), objectMetadata);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eTag = destS3Client.putObject(putRequest).getETag();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (eTag.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Copy failed, New object in " + destS3Uri.toString() + " is empty");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答