“Content-Length” delimited消息体过早结束。

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

Premature end of Content-Length delimited message body

问题

我调试了代码,我遇到的错误是在 obj.close() 语句之后。我只需要 S3 对象的最后修改日期。

AmazonS3 s3 = AmazonS3ClientBuilder
              .standard()
              .withCredentials(new DefaultAWSCredentialsProviderChain())
              .withRegion(Regions.US_EAST_1)
              .build();
S3Object obj = null;
try {
    obj = s3.getObject("bucketName", "abc/1.txt");

    obj.getObjectContent();
    Date date = obj.getObjectMetadata().getLastModified();

    System.out.println(date);
} finally {
    if (obj != null) {
        obj.close();
    }
}

错误信息:

org.apache.http.ConnectionClosedException: Content-Length 期望值为 12,但接收到 0,消息体长度异常结束
    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
    at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:200)
    at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:103)
    at org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:140)
    at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228)
    at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:174)
    at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
    at com.amazonaws.event.ProgressInputStream.close(ProgressInputStream.java:211)
    at com.amazonaws.util.IOUtils.closeQuietly(IOUtils.java:70)
    at com.amazonaws.services.s3.internal.S3AbortableInputStream.close(S3AbortableInputStream.java:185)
    at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
    at com.amazonaws.services.s3.model.S3ObjectInputStream.close(S3ObjectInputStream.java:136)
    at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
    at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
    at com.amazonaws.event.ProgressInputStream.close(ProgressInputStream.java:211)
    at java.base/java.io.FilterInputStream.close(FilterInputStream.java:180)
    at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
    at com.amazonaws.services.s3.model.S3ObjectInputStream.close(S3ObjectInputStream.java:136)
    at com.amazonaws.services.s3.model.S3Object.close(S3Object.java:225)
英文:

I debugged the code the error that I am getting is after the obj.close() statement. I just need the last modified date of the S3 object.

AmazonS3 s3 = AmazonS3ClientBuilder
			  .standard()
			  .withCredentials(new DefaultAWSCredentialsProviderChain())
			  .withRegion(Regions.US_EAST_1)
			  .build();
	S3Object obj = null;
	try {
		obj = s3.getObject("bucketName","abc/1.txt");
	
	    obj.getObjectContent();
	    Date date = obj.getObjectMetadata().getLastModified();
	
	   System.out.println(date);
	}finally {
		if(obj!=null) {
			obj.close();
		}	
	}

Error:

org.apache.http.ConnectionClosedException: Premature end of Content-Length delimited message body (expected: 12; received: 0
	at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:180)
	at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:200)
	at org.apache.http.impl.io.ContentLengthInputStream.close(ContentLengthInputStream.java:103)
	at org.apache.http.impl.execchain.ResponseEntityProxy.streamClosed(ResponseEntityProxy.java:140)
	at org.apache.http.conn.EofSensorInputStream.checkClose(EofSensorInputStream.java:228)
	at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:174)
	at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
	at com.amazonaws.event.ProgressInputStream.close(ProgressInputStream.java:211)
	at com.amazonaws.util.IOUtils.closeQuietly(IOUtils.java:70)
	at com.amazonaws.services.s3.internal.S3AbortableInputStream.close(S3AbortableInputStream.java:185)
	at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
	at com.amazonaws.services.s3.model.S3ObjectInputStream.close(S3ObjectInputStream.java:136)
	at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
	at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
	at com.amazonaws.event.ProgressInputStream.close(ProgressInputStream.java:211)
	at java.base/java.io.FilterInputStream.close(FilterInputStream.java:180)
	at com.amazonaws.internal.SdkFilterInputStream.close(SdkFilterInputStream.java:99)
	at com.amazonaws.services.s3.model.S3ObjectInputStream.close(S3ObjectInputStream.java:136)
	at com.amazonaws.services.s3.model.S3Object.close(S3Object.java:225)

答案1

得分: 0

这是因为您调用了 s3.getObject(),但没有读取检索到的内容。SDK 假设如果您检索了对象数据,您会使用它。

如果您只关心对象的元数据,您应该直接在 AmazonS3 客户端对象上调用 getObjectMetadata()

英文:

This happens because you call s3.getObject() but do not read the content that it retrieves. The SDK assumes that if you retrieved the object data you would use it.

If all you care about is the object metadata, you should call getObjectMetadata() directly on the AmazonS3 client object.

huangapple
  • 本文由 发表于 2020年9月15日 00:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63888442.html
匿名

发表评论

匿名网友

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

确定