Skip to content

http: allow put_file()ing file-like objects #764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions fsspec/implementations/http.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function

import asyncio
import io
import logging
import re
import weakref
Expand All @@ -14,7 +15,7 @@
from fsspec.callbacks import _DEFAULT_CALLBACK
from fsspec.exceptions import FSTimeoutError
from fsspec.spec import AbstractBufferedFile
from fsspec.utils import DEFAULT_BLOCK_SIZE, tokenize
from fsspec.utils import DEFAULT_BLOCK_SIZE, nullcontext, tokenize

from ..caching import AllBytes

Expand Down Expand Up @@ -254,17 +255,29 @@ async def _get_file(

async def _put_file(
self,
rpath,
lpath,
rpath,
chunk_size=5 * 2 ** 20,
callback=_DEFAULT_CALLBACK,
method="post",
**kwargs,
):
async def gen_chunks():
with open(rpath, "rb") as f:
callback.set_size(f.seek(0, 2))
f.seek(0)
# Support passing arbitrary file-like objects
# and use them instead of streams.
if isinstance(lpath, io.IOBase):
context = nullcontext(lpath)
use_seek = False # might not support seeking
else:
context = open(lpath, "rb")
use_seek = True

with context as f:
if use_seek:
callback.set_size(f.seek(0, 2))
f.seek(0)
else:
callback.set_size(getattr(f, "size", None))

chunk = f.read(64 * 1024)
while chunk:
Expand All @@ -283,8 +296,8 @@ async def gen_chunks():
)

meth = getattr(session, method)
async with meth(lpath, data=gen_chunks(), **kw) as resp:
self._raise_not_found_for_status(resp, lpath)
async with meth(rpath, data=gen_chunks(), **kw) as resp:
self._raise_not_found_for_status(resp, rpath)

async def _exists(self, path, **kwargs):
kw = self.kwargs.copy()
Expand Down
9 changes: 9 additions & 0 deletions fsspec/implementations/tests/test_http.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import contextlib
import io
import os
import sys
import threading
Expand Down Expand Up @@ -465,6 +466,14 @@ def test_put_file(server, tmp_path, method, reset_files):
fs.get_file(server + "/hey", dwl_file)
assert dwl_file.read_bytes() == data

src_file.write_bytes(b"xxx")
with open(src_file, "rb") as stream:
fs.put_file(stream, server + "/hey_2", method=method)
assert fs.cat(server + "/hey_2") == b"xxx"

fs.put_file(io.BytesIO(b"yyy"), server + "/hey_3", method=method)
assert fs.cat(server + "/hey_3") == b"yyy"


@pytest.mark.xfail(
condition=sys.flags.optimize > 1, reason="no docstrings when optimised"
Expand Down
6 changes: 6 additions & 0 deletions fsspec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pathlib
import re
import sys
from contextlib import contextmanager
from functools import partial
from hashlib import md5
from urllib.parse import urlsplit
Expand Down Expand Up @@ -466,3 +467,8 @@ def wrapper(cls):
return cls

return wrapper


@contextmanager
def nullcontext(obj):
yield obj