Длина считанных данных отличается от ожидаемой: dataLength = 964481363; expectedLength = 964481376; - PullRequest
0 голосов
/ 29 марта 2019

Я пытаюсь прочитать зашифрованный объект с S3, а затем загрузить его в новое хранилище на S3 с другим ключом шифрования. Пока я пытаюсь прочитать объект, использую getObject () и пытаюсь поместить объект с помощью запроса PutObject.

Когда я печатаю длину содержимого источника, это 964481376. Итак, откуда берется длина данных? что 964481363

Метаданные моего исходного файла выглядят так:

Content-Type

двоичный / октет-поток

х-AMZ-мета-х-AMZ-незашифрованном-Content-Length

964481363

х-АМЗ-мета-х-АМЗ-обертывание-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;
    }

Но я получаю следующую ошибку:

{ «errorMessage»: «Чтение данных имеет длину, отличную от ожидаемой: dataLength = 964481363; Ожидаемый лэнд = 964481376; includeSkipped = false; in.getClass () = класс com.amazonaws.internal.ReleasableInputStream; отмеченныйSupported = false; отмеченный = 0; resetSinceLastMarked = false; markCount = 0; resetCount = 0 ", "errorType": "com.amazonaws.SdkClientException", "трассировки стека": [ "Com.amazonaws.util.LengthCheckInputStream.checkLength (LengthCheckInputStream.java:151)", "Com.amazonaws.util.LengthCheckInputStream.read (LengthCheckInputStream.java:109)", "Java.io.FilterInputStream.read (FilterInputStream.java:107)", "Com.amazonaws.services.s3.internal.crypto.CipherLiteInputStream.nextChunk (CipherLiteInputStream.java:225)", "Com.amazonaws.services.s3.internal.crypto.CipherLiteInputStream.read (CipherLiteInputStream.java:118)", "Com.amazonaws.services.s3.internal.crypto.RenewableCipherLiteInputStream.read (RenewableCipherLiteInputStream.java:112)", "Com.amazonaws.internal.SdkFilterInputStream.read (SdkFilterInputStream.java:90)", "Com.amazonaws.internal.SdkFilterInputStream.read (SdkFilterInputStream.java:90)", "Com.amazonaws.util.LengthCheckInputStream.read (LengthCheckInputStream.java:107)", "Com.amazonaws.internal.SdkFilterInputStream.read (SdkFilterInputStream.java:90)", "Com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStream.read (MD5DigestCalculatingInputStream.java:128)", "Java.io.BufferedInputStream.fill (BufferedInputStream.java:246)", "Java.io.BufferedInputStream.read1 (BufferedInputStream.java:286)", "Java.io.BufferedInputStream.read (BufferedInputStream.java:345)", "Com.amazonaws.internal.SdkBufferedInputStream.read (SdkBufferedInputStream.java:76)", "Com.amazonaws.internal.SdkFilterInputStream.read (SdkFilterInputStream.java:90)", "Com.amazonaws.event.ProgressInputStream.read (ProgressInputStream.java:180)", "Com.amazonaws.internal.SdkFilterInputStream.read (SdkFilterInputStream.java:90)", "Org.apache.http.entity.InputStreamEntity.writeTo (InputStreamEntity.java:140)", "Com.amazonaws.http.RepeatableInputStreamRequestEntity.writeTo (RepeatableInputStreamRequestEntity.java:160)", "Org.apache.http.impl.DefaultBHttpClientConnection.sendRequestEntity (DefaultBHttpClientConnection.java:156)", "Org.apache.http.impl.conn.CPoolProxy.sendRequestEntity (CPoolProxy.java:162)", "Org.apache.http.protocol.HttpRequestExecutor.doSendRequest (HttpRequestExecutor.java:238)", "Com.amazonaws.http.protocol.SdkHttpRequestExecutor.doSendRequest (SdkHttpRequestExecutor.java:63)", "Org.apache.http.protocol.HttpRequestExecutor.execute (HttpRequestExecutor.java:123)", "Org.apache.http.impl.execchain.MainClientExec.execute (MainClientExec.java:271)", "Org.apache.http.impl.execchain.ProtocolExec.execute (ProtocolExec.java:184)", "Org.apache.http.impl.client.InternalHttpClient.doExecute (InternalHttpClient.java:184)", "Org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:82)", "Org.apache.http.impl.client.CloseableHttpClient.execute (CloseableHttpClient.java:55)", "Com.amazonaws.http.apache.client.impl.SdkHttpClient.execute (SdkHttpClient.java:72)","Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeOneRequest (AmazonHttpClient.java:1297)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeHelper (AmazonHttpClient.java:1113)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.doExecute (AmazonHttpClient.java:770)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.executeWithTimer (AmazonHttpClient.java:744)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.execute (AmazonHttpClient.java:726)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutor.access $ 500 (AmazonHttpClient.java:686)", "Com.amazonaws.http.AmazonHttpClient $ RequestExecutionBuilderImpl.execute (AmazonHttpClient.java:668)", "Com.amazonaws.http.AmazonHttpClient.execute (AmazonHttpClient.java:532)", "Com.amazonaws.http.AmazonHttpClient.execute (AmazonHttpClient.java:512)", "Com.amazonaws.services.s3.AmazonS3Client.invoke (AmazonS3Client.java:4914)", "Com.amazonaws.services.s3.AmazonS3Client.invoke (AmazonS3Client.java:4860)", "Com.amazonaws.services.s3.AmazonS3Client.access $ 300 (AmazonS3Client.java:389)", "Com.amazonaws.services.s3.AmazonS3Client $ PutObjectStrategy.invokeServiceCall (AmazonS3Client.java:5793)", "Com.amazonaws.services.s3.AmazonS3Client.uploadObject (AmazonS3Client.java:1786)", "Com.amazonaws.services.s3.AmazonS3Client.putObject (AmazonS3Client.java:1746)", "Com.amazonaws.services.s3.AmazonS3EncryptionClient.access $ 101 (AmazonS3EncryptionClient.java:81)", "Com.amazonaws.services.s3.AmazonS3EncryptionClient $ S3DirectImpl.putObject (AmazonS3EncryptionClient.java:690)", "Com.amazonaws.services.s3.internal.crypto.S3CryptoModuleBase.putObjectUsingMetadata (S3CryptoModuleBase.java:175)", "Com.amazonaws.services.s3.internal.crypto.S3CryptoModuleBase.putObjectSecurely (S3CryptoModuleBase.java:161)", "Com.amazonaws.services.s3.internal.crypto.CryptoModuleDispatcher.putObjectSecurely (CryptoModuleDispatcher.java:108)", "Com.amazonaws.services.s3.AmazonS3EncryptionClient.putObject (AmazonS3EncryptionClient.java:570)", "Com.amazon.testCase.lambda.object.UploadObject.coyS3Object (UploadObject.java:93)", "Com.amazon.testCase.lambda.object.UploadObject.handleRequest (UploadObject.java:54)", "sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)", "Sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)", "Sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)", "Java.lang.reflect.Method.invoke (Method.java:498)" ] }

1 Ответ

0 голосов
/ 29 марта 2019

Я разобрался в проблеме. Длина содержимого объекта S3 - это длина зашифрованных данных. Но когда я помещаю объект, я пытаюсь поместить незашифрованные данные. Поэтому мне нужно явно установить длину содержимого объекта в незашифрованную длину. Ниже код решает проблему.

 try {
            //Get the Source object stream
            sourceS3Object = sourceS3Client.getObject(sourceS3Uri.getBucket(), sourceS3Uri.getKey());
            //For a decrypted object, the content length in metadata has the encrypted length
            //Set the content length to the unencrypted-data-length
            ObjectMetadata objectMetadata = sourceS3Object.getObjectMetadata();
            Map<String, String> modelMetaData = objectMetadata.getUserMetadata();
            Long unencryptedDataLength = Long.parseLong(modelMetaData.get(
                    "x-amz-unencrypted-content-length"));
            objectMetadata.setContentLength(unencryptedDataLength);
            PutObjectRequest putRequest = new PutObjectRequest(destS3Uri.getBucket(), destS3Uri.getKey(),
                    sourceS3Object.getObjectContent(), objectMetadata);

            eTag = destS3Client.putObject(putRequest).getETag();

            if (eTag.isEmpty()) {
                System.out.println("Copy failed, New object in " + destS3Uri.toString() + " is empty");
                return false;
            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...