Цель
API сервера 2 должен получать и немедленно загружать каждый фрагмент по мере его потоковой передачи из API сервера 1.
Проблема
API Сервера 2 продолжает буферизовать фрагменты перед загрузкой файла, сводя на нет цель непрерывной загрузки на основе фрагментов для больших файлов, поскольку фрагменты поступают на Сервер 2.
Логика API сервера 1
// Utils
class OneDrive:
def generate_chunks(response, chunk_size = 10 * 1024 * 1024): # 10 MB chunks
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
yield chunk
def full_file_loader(user_name, file_uri):
// auth logic etc
url = f"https://graph.microsoft.com/v1.0/drives ... i}/content"
response = requests.get(url, headers=headers, stream=True)
if response.status_code != 200:
return False
return lambda: OneDrive.generate_chunks(response)
except Exception as e:
print(f"An error occurred: {e}")
# View
file = OneDrive.full_file_loader(user_name, file_uri)
response = Response(file(), content_type='application/octet-stream')
filename = "download.mp4"
response.headers['Content-Disposition'] = f"attachment; filename*=UTF-8''{quote(filename)}"
return response
Логика API Сервера 2
def download_entity_view(request):
try:
response = requests.get(cse_api_url, params=params, stream=True)
if response.status_code == 200:
total_downloaded = 0
def generate_chunks():
nonlocal total_downloaded
for chunk in response.iter_content(chunk_size=10*1024*1024):
if chunk:
total_downloaded += len(chunk)
print(f"Downloaded chunk size: {len(chunk)} bytes (Total: {total_downloaded} bytes)")
yield chunk
filename = "downloaded_file.mp4"
streamed_response = StreamingHttpResponse(
generate_chunks(),
content_type='application/octet-stream'
)
streamed_response['Content-Disposition'] = f'attachment; filename="{quote(filename)}"'
return streamed_response
Подробнее здесь: https://stackoverflow.com/questions/790 ... uous-strea
Как отправить большой файл частями с сервера 1 на сервер 2 с непрерывной потоковой загрузкой на сервер 2? ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение