我们很高兴宣布 Cloudinary 成为 Astro 内容层 API 的首批合作伙伴,这是我们将外部内容引入 内容集合 的新方法。Cloudinary 是一个媒体资产托管平台,允许您转换和优化媒体类型。
内容集合是 Astro 为组织化、管理和类型安全内容提供的解决方案。现在,借助 Astro 的内容层,您可以在构建过程中从外部源引入所有不同类型的内容和数据。内容层 API 有效地同步和缓存内容,为您提供使用任何数据源时熟悉且统一的体验。
使用 Cloudinary 加载器
要使用新的加载器,首先从 npm 安装 astro-cloudinary
包。
npm install astro-cloudinary
接下来,您需要使用您的 API 密钥配置环境。有关详细信息,请参阅 Cloudinary 加载器配置指南。
然后在你的 src/content/config.ts
配置文件中导入并使用该加载器
import { defineCollection } from 'astro:content'import { cldAssetsLoader } from 'astro-cloudinary/loaders'
const samples = defineCollection({ loader: cldAssetsLoader({ limit: 4, folder: 'samples/food' })})
export const collections = { assets }
就这么简单!从这里开始,您可以像使用本地集合一样查询和使用集合。有关更多信息,请参阅 Astro 文档中的 内容集合指南。
有关使用 Cloudinary 加载器的更多信息,请阅读《使用 Astro 内容层在 Astro 中获取 Cloudinary 资产》,其中包含更多详细信息。此外,还可以查看昨天关于新内容层 API 的深入探讨,了解加载器的工作原理以及如何构建您自己的加载器。