javaamazon-s3okhttppre-signed-urlcontent-length

Can I get the header detail "Content-Length" from an Amazon S3 Presigned URL using OkHttp3 client?


We have some Java services that upload files to an S3 compatible storage and generate Presigned URLs. Other Java services receive such URLs and work on the files. We need the content length of this file, without loading the whole object from the body. Is this possible? And if yes, how?

We upload via

com.amazonaws.services.s3.AmazonS3.putObject()

and create URLs with

com.amazonaws.services.s3.AmazonS3.generatePresignedUrl()

When I check the Http Headers of a file using my S3 Browser, I can see the correct Content-Length entry.

But in our services we use

OkHttpClient.newCall(
    new Builder().get()
        .url(url).build()
).execute();

And in those Response objects there is no Content-Length.


Solution

  • If the response is streamed (HTTP/1.1 chunked) or compressed then it won't have a content length to read from. You can probably use a HEAD request to get the headers without the body and check first.

    https://stackoverflow.com/a/38673237/1542667