简体   繁体   English

我可以使用 OkHttp3 客户端从 Amazon S3 Presigned URL 获取 header 详细信息“Content-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.我们有一些 Java 服务将文件上传到 S3 兼容存储并生成预签名 URL。 Other Java services receive such URLs and work on the files.其他 Java 服务接收此类 URL 并处理文件。 We need the content length of this file, without loading the whole object from the body.我们需要这个文件的内容长度,而不是从正文加载整个 object。 Is this possible?这可能吗? And if yes, how?如果是,如何?

We upload via我们通过上传

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

and create URLs with并创建 URL

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.当我使用 S3 浏览器检查文件的 Http 标头时,我可以看到正确的 Content-Length 条目。

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.在那些响应对象中没有内容长度。

If the response is streamed (HTTP/1.1 chunked) or compressed then it won't have a content length to read from.如果响应是流式传输(HTTP/1.1 分块)或压缩的,那么它将没有内容长度可供读取。 You can probably use a HEAD request to get the headers without the body and check first.您可能可以使用 HEAD 请求来获取没有正文的标头并首先检查。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM