Skip to content

why S3 headObject() API fails with content-related error when it should not be looking at content at all?

0

Java java.io.EOFException during AWS S3 headObject with GZIP decompression

I'm encountering a perplexing java.io.EOFException wrapped in a java.io.UncheckedIOException when my Java application attempts to perform an S3Client.headObject operation. The error seems to originate within the AWS SDK's Crc32Validation and GZIPInputStream classes, suggesting an issue with GZIP decompression of the S3 object's metadata or content.

The application is a microservice that interacts with AWS S3. Specifically, the error occurs when calling s3Client.headObject() within a utility class (S3JsonStore) which is then called by a Storage class, ultimately triggered by a SyncController.

The full stack trace is as follows:

java.io.EOFException: java.io.UncheckedIOException
java.io.UncheckedIOException: java.io.EOFException
	at software.amazon.awssdk.utils.FunctionalUtils.asRuntimeException(FunctionalUtils.java:180)
	at software.amazon.awssdk.utils.FunctionalUtils.lambda$safeSupplier$4(FunctionalUtils.java:110)
	at software.amazon.awssdk.utils.FunctionalUtils.invokeSafely(FunctionalUtils.java:136)
	at software.amazon.awssdk.core.http.Crc32Validation.decompressing(Crc32Validation.java:85)
	at software.amazon.awssdk.core.http.Crc32Validation.process(Crc32Validation.java:62)
	at software.amazon.awssdk.core.http.Crc32Validation.validate(Crc32Validation.java:44)
	at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler$Crc32ValidationResponseHandler.handle(AwsSyncClientHandler.java:95)
	at software.amazon.awssdk.core.internal.handler.BaseClientHandler.lambda$successTransformationResponseHandler$6(BaseClientHandler.java:234)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:40)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.HandleResponseStage.execute(HandleResponseStage.java:30)
	at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:73)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptTimeoutTrackingStage.execute(ApiCallAttemptTimeoutTrackingStage.java:42)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:78)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.TimeoutExceptionHandlingStage.execute(TimeoutExceptionHandlingStage.java:40)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(ApiCallAttemptMetricCollectionStage.java:50)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallAttemptMetricCollectionStage.execute(ApiCallAttemptMetricCollectionStage.java:36)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:81)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.RetryableStage.execute(RetryableStage.java:36)
	at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
	at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:56)
	at software.amazon.awssdk.core.internal.http.StreamManagingStage.execute(StreamManagingStage.java:36)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.executeWithTimer(ApiCallTimeoutTrackingStage.java:80)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:60)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallTimeoutTrackingStage.execute(ApiCallTimeoutTrackingStage.java:42)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(ApiCallMetricCollectionStage.java:48)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ApiCallMetricCollectionStage.execute(ApiCallMetricCollectionStage.java:31)
	at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
	at software.amazon.awssdk.core.internal.http.pipeline.RequestPipelineBuilder$ComposingRequestPipelineStage.execute(RequestPipelineBuilder.java:206)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionStage.java:37)
	at software.amazon.awssdk.core.internal.http.pipeline.stages.ExecutionFailureExceptionReportingStage.execute(ExecutionFailureExceptionStage.java:26)
	at software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient$RequestExecutionBuilderImpl.execute(AmazonSyncHttpClient.java:193)
	at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.invoke(BaseSyncClientHandler.java:103)
	at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.doExecute(BaseSyncClientHandler.java:167)
	at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.lambda$execute$1(BaseSyncClientHandler.java:82)
	at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.measureApiCallSuccess(BaseSyncClientHandler.java:175)
	at software.amazon.awssdk.core.internal.handler.BaseSyncClientHandler.execute(BaseSyncClientHandler.java:76)
	at software.amazon.awssdk.core.client.handler.SdkSyncClientHandler.execute(SdkSyncClientHandler.java:45)
	at software.amazon.awssdk.awscore.client.handler.AwsSyncClientHandler.execute(AwsSyncClientHandler.java:56)
	at software.amazon.awssdk.services.s3.DefaultS3Client.headObject(DefaultS3Client.java:5089)
	at com.mycompany.myapp.util.S3JsonStore.getObjectMetadata(S3JsonStore.java:253)
	at com.mycompany.myapp.microservices.sync.Storage.writeSyncTrigger(Storage.java:383)
	at com.mycompany.myapp.microservices.sync.SyncController.sync(SyncController.java:64)
	at com.mycompany.myapp.microservices.sync.SyncHandler.handleRequest(SyncHandler.java:34)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
Caused by: java.io.EOFException
	at java.base/java.util.zip.GZIPInputStream.readUByte(Unknown Source)
	at java.base/java.util.zip.GZIPInputStream.readUShort(Unknown Source)
	at java.base/java.util.zip.GZIPInputStream.readHeader(Unknown Source)
	at java.base/java.util.zip.GZIPInputStream.<init>(Unknown Source)
	at java.base/java.util.zip.GZIPInputStream.<init>(Unknown Source)
	at software.amazon.awssdk.core.http.Crc32Validation.lambda$decompressing$2(Crc32Validation.java:85)
	at software.amazon.awssdk.utils.FunctionalUtils.lambda$safeSupplier$4(FunctionalUtils.java:108)
	... 44 more

Context:

  • Application: A Java microservice running on AWS Lambda (implied by handleRequest and SyncHandler).
  • AWS SDK: Using AWS SDK for Java 2.x (indicated by software.amazon.awssdk packages).
  • Operation: The error occurs during S3Client.headObject(). This operation is typically used to retrieve object metadata without downloading the object content.
  • GZIP: The Caused by: java.io.EOFException at java.base/java.util.zip.GZIPInputStream.readHeader(Unknown Source) strongly suggests that the S3 object (or its metadata response) is being treated as a GZIP compressed stream, and the GZIP header is either truncated or invalid.

Relevant Code Snippets (simplified and generalized):

S3JsonStore.java (line 253):

public ObjectMetadata getObjectMetadata(String bucketName, String key) {
    try {
        HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
                .bucket(bucketName)
                .key(key)
                .build();
        return s3Client.headObject(headObjectRequest); // This is line 253
    } catch (S3Exception e) {
        if (e.statusCode() == 404) {
            return null; // Object not found
        }
        throw e;
    }
}

Storage.java (line 383):

public void writeSyncTrigger(String triggerKey, SyncTrigger trigger) {
    // ... other logic ...
    ObjectMetadata metadata = s3JsonStore.getObjectMetadata(myBucket, triggerKey); // This is line 383
    if (metadata == null) {
        // ... create object ...
    } else {
        // ... update object ...
    }
}

SyncController.java (line 64):

public void sync(SyncRequest request) {
    // ...
    storage.writeSyncTrigger(request.getTriggerKey(), request.getTrigger()); // This is line 64
    // ...
}

Problem Description:

The EOFException indicates that the GZIPInputStream unexpectedly reached the end of the stream while trying to read the GZIP header. This happens when s3Client.headObject() is called.

What I've checked/considered:

  1. Object Existence: The getObjectMetadata method handles 404s, so it's not simply a missing object. The error occurs even for objects that are known to exist.
  2. Object Content-Encoding: I've verified that the S3 objects themselves are not GZIP compressed. They are plain JSON files.
  3. AWS SDK Configuration: I haven't explicitly configured any GZIP decompression settings in the S3Client builder. It seems the SDK is attempting to decompress the response automatically.
  4. Network Issues: While an EOFException can sometimes indicate a truncated network stream, the fact that it's specifically within GZIPInputStream.readHeader and Crc32Validation points more towards an issue with how the SDK is interpreting the S3 response, rather than a general network problem.
  5. S3 Object Integrity: The objects in S3 are valid JSON and can be downloaded and read without issues.

My Hypothesis:

It appears the AWS SDK for Java 2.x's Crc32Validation component is attempting to decompress the headObject response as a GZIP stream. Why?

Question:

How can I diagnose and resolve this java.io.EOFException during S3Client.headObject?

  • Is there a way to disable GZIP decompression for headObject responses in the AWS SDK for Java 2.x?
  • What could cause the Crc32Validation to attempt GZIP decompression on a non-GZIP S3 object's metadata response?
  • Are there any known issues or specific configurations related to Crc32Validation and headObject that could lead to this?

Any insights or debugging steps would be greatly appreciated!

asked a year ago484 views

1 Answer
0

The issue you're experiencing with the headObject API call failing with a GZIP-related EOFException is unusual since the headObject operation should only retrieve metadata without returning the object's content.

Based on the stack trace, the error is occurring in the AWS SDK's Crc32Validation class during an attempt to decompress what it believes is GZIP-encoded content. This suggests that either:

  1. The object in S3 has a Content-Encoding: gzip header set, causing the SDK to attempt decompression even during a HEAD request
  2. There might be a response compression happening at the HTTP level

The HEAD action in S3 retrieves metadata from an object without returning the object itself. It has the same options as a GET action but returns only headers, not the body. When the headObject method is called, it should only return metadata information about the object.

To resolve this issue, you could try:

  1. Check if the object in S3 has Content-Encoding: gzip set in its metadata. If it does, you might need to update the object metadata to remove this encoding if the content isn't actually compressed.

  2. Examine how the S3 client is configured. You might need to disable automatic content decompression in your HTTP client configuration when building the S3 client.

  3. If you're uploading objects to S3, ensure you're properly setting the content metadata. When using input streams to upload objects, make sure to set the correct content length in the ObjectMetadata to avoid issues. The AWS SDK might be buffering the entire stream in memory if content length isn't specified, which could lead to unexpected behavior.

For example, when uploading objects using input streams, you should set the content length explicitly:

ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(inputFile.length()); // Set content length explicitly
s3Client.putObject(s3Bucket, inputFile.getName(), inputStream, metadata);

This issue highlights the importance of properly configuring object metadata when working with S3, as incorrect metadata can cause unexpected behavior even in operations that shouldn't be accessing the object's content.
Sources
Unchecked S3 object metadata content length | Amazon Q, Detector Library
S3Client (AWS SDK for Java - 2.21.29)
S3AsyncClient (AWS SDK for Java - 2.21.29)

answered a year ago

  • this seems to be a bug in the AWS SDK, the only workaround I found was to use GetObject instead of HeadObject, and discard the input stream

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.