简体   繁体   English

通过HTTP获取文件创建日期

[英]Get File Creation Date Over HTTP

Given a file on a webserver (eg, http://foo.com/bar.zip -> only accessible through HTTP), is there any way to get the date attributes (eg, date [created, modified]) without downloading the entire archive in the first place? 给定web服务器上的文件(例如, http//foo.com/bar.zip - >只能通过HTTP访问),有没有办法获取日期属性(例如,日期[创建,修改])而不下载整个档案首先?

Right now, I download the archive and read the attributes programmatically. 现在,我下载存档并以编程方式读取属性。 Trouble is that the archive is dozens of MiB so it seems like a waste of resources to download the entire thing and end up reading off just a couple of bytes of information. 麻烦的是存档是几十个MiB,因此下载整个内容并最终只读取几个字节的信息似乎浪费资源。

I realize that bandwidth is practically free, but I don't like to be wasteful in any case. 我意识到带宽实际上是免费的,但我不喜欢浪费任何情况。

尝试从标题中读取Last-Modified

Be sure to use a HTTP HEAD request instead of a HTTP GET request to read the HTTP headers only. 请务必使用HTTP HEAD请求而不是HTTP GET请求来仅读取HTTP标头。 If you do a HTTP GET, you will download the whole file nevertheless, even if you decide just to inspect the HTTP headers. 如果您执行HTTP GET,即使您决定只检查HTTP标头,也会下载整个文件。

Just for the sake of simplicity, here's a compilation of the existing (perfect) answers from @ihorko and @JanThomä, that uses curl. 仅仅为了简单起见,这里是@ihorko和@JanThomä的现有(完美)答案的汇编,它使用curl。 Other option are available too, of course, but here's a fully functional answer. 当然,其他选项也是可用的,但这是一个功能齐全的答案。

Use curl with the -I option: 使用curl和-I选项:

-I, --head
(HTTP/FTP/FILE) Fetch the HTTP-header only! (HTTP / FTP / FILE)仅获取HTTP标头! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. HTTP服务器具有HEAD命令,它只用于获取文档的标题。 When used on an FTP or FILE file, curl displays the file size and last modification time only. 在FTP或FILE文件上使用时,curl仅显示文件大小和上次修改时间。

Also, the -s option is nice here: 此外, -s选项在这里很好:

-s, --silent
Silent or quiet mode. 无声或安静模式。 Don't show progress meter or error messages. 不显示进度表或错误消息。 Makes Curl mute. 使卷曲静音。 It will still output the data you ask for, potentially even to the terminal/stdout unless you redirect it. 它仍会输出您要求的数据,甚至可能输出到终端/标准输出,除非您重定向它。

Hence, something like this would do the trick: 因此,像这样的东西可以做到这一点:

curl -sI http://foo.com/bar.zip | grep 'Last-Modified' | cut -d' ' -f 2-

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

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