Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

java - 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.

question from:https://stackoverflow.com/questions/66050941/can-i-get-the-header-detail-content-length-from-an-amazon-s3-presigned-url-usi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...