HUGO

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
gohugoio Star
  • 关于
    • 本节内容
    • 简介
    • 特性
    • 隐私
    • 安全
    • 许可证
  • 安装
    • 本节内容
    • macOS
    • Linux
    • Windows
    • BSD
  • 入门
    • 本节内容
    • 快速开始
    • 基本用法
    • 目录结构
    • 配置
    • 配置标记
    • 配置构建
    • 术语表
    • 外部学习资源
  • 快速参考
    • 本节内容
    • 表情符号
    • 函数
    • 方法
    • 页面集合
  • 内容管理
    • 本节内容
    • 组织
    • 页面包
    • 内容格式
    • 前言
    • 构建选项
    • 页面资源
    • 图像处理
    • 短代码
    • 相关内容
    • 章节
    • 内容类型
    • 原型
    • 分类法
    • 摘要
    • 链接和交叉引用
    • URL 管理
    • 菜单
    • 评论
    • 多语言
    • Markdown 属性
    • 语法高亮
    • 图表
    • 数学
    • 数据源
    • 内容适配器
  • 模板
    • 本节内容
    • 简介
    • 模板类型
    • 查找顺序
    • 基础模板
    • 首页模板
    • 单页模板
    • 章节模板
    • 分类模板
    • 术语模板
    • 局部模板
    • 内容视图模板
    • 短代码模板
    • 站点地图模板
    • RSS 模板
    • 404 模板
    • robots.txt 模板
    • 菜单
    • 分页
    • 嵌入式模板
    • 自定义输出格式
  • 函数
    • 本节内容
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hash
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • 方法
    • 本节内容
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • 渲染钩子
    • 本节内容
    • 简介
    • 块引用
    • 代码块
    • 标题
    • 图像
    • 链接
    • 透传
    • 表格
  • 短代码
    • 本节内容
    • 评论
    • 详情
    • 图
    • Gist
    • 高亮
    • Instagram
    • Param
    • QR
    • Ref
    • Relref
    • Vimeo
    • X
    • YouTube
  • Hugo 模块
    • 本节内容
    • 配置 Hugo 模块
    • 使用 Hugo 模块
    • 主题组件
  • Hugo Pipes
    • 本节内容
    • 简介
    • 将 Sass 转译为 CSS
    • PostCSS
    • 后处理
    • JavaScript 构建
    • 资源压缩
    • 串联资源
    • 指纹识别和 SRI 哈希
    • 从字符串创建资源
    • 从模板创建资源
  • CLI
  • 故障排除
    • 本节内容
    • 审计
    • 日志记录
    • 检查
    • 弃用
    • 性能
    • 常见问题
  • 开发者工具
    • 本节内容
    • 编辑器插件
    • 前端
    • 搜索
    • 迁移
    • 其他项目
  • 托管和部署
    • 本节内容
    • Hugo Deploy
    • 使用 Rclone 部署
    • 使用 Rsync 部署
    • 在 21YunBox 上托管
    • 在 AWS Amplify 上托管
    • 在 Azure 静态 Web 应用上托管
    • 在 Cloudflare Pages 上托管
    • 在 Firebase 上托管
    • 在 GitHub Pages 上托管
    • 在 GitLab Pages 上托管
    • 在 KeyCDN 上托管
    • 在 Netlify 上托管
    • 在 Render 上托管
  • 贡献
    • 本节内容
    • 开发
    • 文档
    • 主题
  • 维护
函数 资源函数

resources.GetRemote

从给定的 URL 返回远程资源,如果没有找到则返回 nil。

语法

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 }}

在检索远程数据时,配置错误的服务器可能会发送带有不正确的 Content-Type 的响应头。例如,服务器可能会将 Content-Type 头设置为 application/octet-stream 而不是 application/json。

在这些情况下,将资源 Content 传递给 transform.Unmarshal 函数,而不是传递资源本身。例如,在上面的示例中,请改为执行此操作

{{ $data = .Content | transform.Unmarshal }}

错误处理

使用 try 语句来捕获 HTTP 请求错误。如果你不自己处理错误,Hugo 将会构建失败。

Hugo 不会将状态码为 404 的 HTTP 响应分类为错误。在这种情况下,resources.GetRemote 返回 nil。

{{ $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$"
         ]
      }
   }
}

请注意,上面的条目是

  • 对允许列表的添加;它不会替换允许列表
  • 正则表达式的数组

另请参阅

  • data.GetCSV
  • data.GetJSON
  • resources.ByType
  • resources.Get
  • resources.GetMatch
  • resources.Match
  • 资源

本页内容

  • 选项
  • 远程数据
  • 错误处理
  • HTTP 响应
  • 缓存
  • 安全

本节内容

  • resources.Babel
  • resources.ByType
  • resources.Concat
  • resources.Copy
  • resources.ExecuteAsTemplate
  • resources.Fingerprint
  • resources.FromString
  • resources.Get
  • resources.GetMatch
  • resources.GetRemote
  • resources.Match
  • resources.Minify
  • resources.PostCSS
  • resources.PostProcess
  • resources.ToCSS
最后更新时间:2025 年 1 月 16 日:将目录名、文件名和文件路径格式化为代码 (8051ff818)
改进此页面
由 Hugo 作者
Hugo Logo
  • 提交问题
  • 获取帮助
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo 赞助商

您的公司?
 

Hugo 标志版权归 © Steve Francia 2013–2025 所有。

Hugo 地鼠基于 Renée French 的原创作品。

  • 新闻
  • 文档
  • 主题
  • 社区
  • GitHub
  • 关于
  • 安装
  • 入门
  • 快速参考
  • 内容管理
  • 模板
  • 函数
  • 方法
  • 渲染钩子
  • 短代码
  • Hugo 模块
  • Hugo Pipes
  • CLI
  • 故障排除
  • 开发者工具
  • 托管和部署
  • 贡献
  • 维护