Last active
February 10, 2022 08:59
-
-
Save statguy/f92b8fc08211b232c4ae77ca8662a704 to your computer and use it in GitHub Desktop.
Write pickle to and read from S3 and local file system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any | |
import fsspec | |
import pickle | |
def write_pickle(data: Any, path: str) -> None: | |
with fsspec.open(path, "wb") as file: | |
pickle.dump(data, file) | |
def read_pickle(path: str) -> Any: | |
with fsspec.open(path, "rb") as file: | |
return pickle.load(file) | |
data = {"a": 1} | |
write_pickle(data, "s3://my_bucket/data.tmp") | |
write_pickle(data, "file:///tmp/data.tmp") | |
assert read_pickle("s3://my_bucket/data.tmp") == data | |
assert read_pickle("file:///tmp/data.tmp") == data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment