amazon-web-services – S3警告:“没有为流数据指定内容长度”

我突然开始看到记录此警告消息,即使底层aws libs的使用没有变化.我一直在使用aws-
java-sdk版本1.6.9.1

No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors.

这是文件的上传方式:

client.putObject(bucketName, key, new ByteArrayInputStream(data), new ObjectMetadata())

我怀疑我可能会看到这个,因为我没有在ObjectMetadata对象上设置内容长度,但这就是之前的情况,并且没有生成警告.

任何人都知道为什么这个警告信息会突然出现?

谢谢!

最佳答案 我知道这已经有一段时间了,但我今天必须处理这个问题并在文档中找到了这个解释.

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/ObjectMetadata.html#setContentLength-long-

When uploading files, the AWS S3 Java client will attempt to determine
the correct content type if one hasn’t been set yet. Users are
responsible for ensuring a suitable content type is set when uploading
streams. If no content type is provided and cannot be determined by
the filename, the default content type, “application/octet-stream”,
will be used.

For more information on the Content-Type header, see
07001

在我的情况下,我不得不缓冲流,所以我使用这样的东西:

https://books.google.com/books?id=cXggAQAAQBAJ&pg=PA377&lpg=PA377&dq=content+length+of+bytebuffer+java&source=bl&ots=AQSUOUwoI6&sig=tdok7YyipU7YUI6p1Bu_2X9HT6s&hl=en&sa=X&ved=0ahUKEwigi_DhiJPVAhVk8IMKHVWLBmAQ6AEIiQEwCA#v=onepage&q=content%20length%20of%20bytebuffer%20java&f=false

ByteBuffer buffer = new ByteBuffer(...)
...
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(buffer.limit());
点赞