gpt4 book ai didi

sanity - 如何通过 HTTP 从 Sanity 下载文件?

转载 作者:行者123 更新时间:2023-12-05 08:37:47 26 4
gpt4 key购买 nike

我想知道是否可以通过 HTTP 请求从 Sanity 下载文件?

我只有引用编号:

{
file: {
asset: {
_ref: "file-fxxxxxxxxxxxxxxxxxxxx-xlsx"
_type: "reference"
}
}
}

我想在这种情况下这样做:

<a href="https://cdn.sanity.io/assets/clientID/dataset/file-xxxxxxxxxxx-xlsx"> 
Download File
</a>

最佳答案

确实可以 🎉 通过一些自定义代码,您可以仅从 _ref 执行此操作,它是文件文档的 _id

从文件的 _ref/_id 创建 URL

_ref/_id 结构是这样的:file-{ID}-{EXTENSION}(例如:file -207fd9951e759130053d37cf0a558ffe84ddd1c9-mp3).

这样,您就可以生成可下载的 URL,其结构如下:https://cdn.sanity.io/files/{PROJECT_ID}/{DATASET}/{ID_OF_FILE}。{EXTENSION}。下面是该操作的一些伪 Javascript 代码:

const getUrlFromId = ref => {
// Example ref: file-207fd9951e759130053d37cf0a558ffe84ddd1c9-mp3
// We don't need the first part, unless we're using the same function for files and images
const [_file, id, extension] = ref.split('-');
return `https://cdn.sanity.io/files/${PROJECT_ID}/${DATASET}/${id}.${extension}`
}

直接查询URL

但是,如果您可以使用 GROQ 查询文件的文档那会更容易:

*[(YOUR FILTER HERE)] {
file->{ url } // gets the URL from the referenced file
}

您也可以对图像执行相同的操作。

关于sanity - 如何通过 HTTP 从 Sanity 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64356284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com