Is there a way to stream data directly from python request to minio bucket

前端 未结 2 1812
走了就别回头了
走了就别回头了 2021-01-14 03:40

I am trying to make a GET request to a server to retrieve a tiff image. I then want to stream it directly to MinIO using the put_object method in the MinIO python SDK.

2条回答
  •  佛祖请我去吃肉
    2021-01-14 04:20

    You can stream your file directly into a minio bucket like this:

    import requests
    
    from pathlib import Path
    from urllib.parse import urlparse
    
    from django.conf import settings
    from django.core.files.storage import default_storage
    
    client = default_storage.client
    object_name = Path(urlparse(response.url).path).name
    bucket_name = settings.MINIO_STORAGE_MEDIA_BUCKET_NAME
    
    with requests.get(url_to_download, stream=True) as r:
        content_length = int(r.headers["Content-Length"])
        result = client.put_object(bucket_name, object_name, r.raw, content_length)
    

    Or you can use a django file field directly:

    with requests.get(url_to_download, stream=True) as r:
        # patch the stream to make django-minio-storage belief
        # it's about to read from a legit file
        r.raw.seek = lambda x: 0
        r.raw.size = int(r.headers["Content-Length"])
        model = MyModel()
        model.file.save(object_name, r.raw, save=True)
    

    The RawIOBase hint from Dinko Pehar was really helpful, thanks a lot. But you have to use response.raw not response.content which would download your file immediately and be really inconvenient when trying to store a large video for example.

提交回复
热议问题