gpt4 book ai didi

python - 从互联网下载文件并使用 Django 服务到网站而不暴露 URL

转载 作者:行者123 更新时间:2023-12-01 09:32:00 37 4
gpt4 key购买 nike

我正在使用某些 URL get 请求下载音频文件。我无法公开 URL,因为它包含 API key 。将此文件提供给网站的正确方法是什么?

最佳答案

创建一个下载文件的 View ,然后将文件作为对前端的响应返回。

Here Django 文档向您展示了如何使用生成器将文件作为响应进行流式传输。

import requests
from django.http import StreamingHttpResponse


def stream_audio(request):
file_url = "your.url.com"
r = requests.get(file_url, stream=True)
response = StreamingHttpResponse(streaming_content=r, content_type='audio/mpeg')
response['Content-Disposition'] = 'attachment; filename="somefilename.mp3"'
return response

这样您就永远不会公开您的 API key ,但可以向用户发送相同的信息。

关于python - 从互联网下载文件并使用 Django 服务到网站而不暴露 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884857/

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