resources.GetRemote
语法
resources.GetRemote URL [OPTIONS]
返回值
resource.Resource
{{ $url := "https://example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
选项
resources.GetRemote
函数接受一个可选的选项映射。
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"headers" (dict "Authorization" "Bearer abcd")
}}
{{ $resource := resources.GetRemote $url $opts }}
如果同一个头部键需要多个值,请使用切片
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"headers" (dict "X-List" (slice "a" "b" "c"))
}}
{{ $resource := resources.GetRemote $url $opts }}
你也可以更改请求方法并设置请求体
{{ $url := "https://example.org/api" }}
{{ $opts := dict
"method" "post"
"body" `{"complete": true}`
"headers" (dict "Content-Type" "application/json")
}}
{{ $resource := resources.GetRemote $url $opts }}
远程数据
在检索远程数据时,请使用 transform.Unmarshal
函数来 解组 响应。
{{ $data := dict }}
{{ $url := "https://example.org/books.json" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $data = . | transform.Unmarshal }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
错误处理
使用 try
语句来捕获 HTTP 请求错误。如果你不自己处理错误,Hugo 将会构建失败。
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
要将错误记录为警告而不是错误
{{ $url := "https://broken-example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ warnf "%s" . }}
{{ else with .Value }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ warnf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
HTTP 响应
resources.GetRemote
函数返回的资源上的 Data
方法会返回 HTTP 响应中的信息。
{{ $url := "https://example.org/images/a.jpg" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ with .Data }}
{{ .ContentLength }} → 42764
{{ .ContentType }} → image/jpeg
{{ .Status }} → 200 OK
{{ .StatusCode }} → 200
{{ .TransferEncoding }} → []
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
- ContentLength
- (
int
)内容的字节长度。 - ContentType
- (
string
)内容类型。 - Status
- (
string
)HTTP 状态文本。 - StatusCode
- (
int
)HTTP 状态码。 - TransferEncoding
- (
string
)传输编码。
缓存
从 resources.GetRemote
返回的资源会缓存到磁盘。有关详细信息,请参阅配置文件缓存。
默认情况下,Hugo 会从传递给该函数的参数、URL 以及选项映射(如果有)中派生缓存键。
通过在选项映射中设置 key
来覆盖缓存键。使用此方法可以更好地控制 Hugo 获取远程资源的频率。
{{ $url := "https://example.org/images/a.jpg" }}
{{ $cacheKey := print $url (now.Format "2006-01-02") }}
{{ $resource := resources.GetRemote $url (dict "key" $cacheKey) }}
安全
为了防止恶意意图,resources.GetRemote
函数会检查服务器响应,包括
- 响应头中的 Content-Type
- 文件扩展名(如果有)
- 内容本身
如果 Hugo 无法将媒体类型解析为其 允许列表中的条目,则该函数会引发错误
ERROR error calling resources.GetRemote: failed to resolve media type...
例如,如果你尝试下载可执行文件,你将会看到上面的错误。
尽管允许列表包含常见媒体类型的条目,但你可能会遇到 Hugo 无法解析你已知安全的文件媒体类型的情况。在这种情况下,请编辑你的站点配置,将媒体类型添加到允许列表中。例如
hugo.
security:
http:
mediaTypes:
- ^image/avif$
- ^application/vnd\.api\+json$
[security]
[security.http]
mediaTypes = ['^image/avif$', '^application/vnd\.api\+json$']
{
"security": {
"http": {
"mediaTypes": [
"^image/avif$",
"^application/vnd\\.api\\+json$"
]
}
}
}
请注意,上面的条目是
- 对允许列表的添加;它不会替换允许列表
- 正则表达式的数组