-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy paths3.py
More file actions
232 lines (198 loc) · 7.89 KB
/
s3.py
File metadata and controls
232 lines (198 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import io
import logging
from pathlib import Path
from typing import TYPE_CHECKING, Dict, Iterator, List, Optional, Type, TypeVar
from docarray.store.abstract_doc_store import AbstractDocStore
from docarray.store.helpers import _from_binary_stream, _to_binary_stream
from docarray.utils._internal.cache import _get_cache_path
from docarray.utils._internal.misc import import_library
if TYPE_CHECKING: # pragma: no cover
import boto3
import botocore
from smart_open import open
from docarray import BaseDoc, DocList
else:
open = import_library('smart_open', raise_error=True).open
boto3 = import_library('boto3', raise_error=True)
botocore = import_library('botocore', raise_error=True)
SelfS3DocStore = TypeVar('SelfS3DocStore', bound='S3DocStore')
class _BufferedCachingReader:
"""A buffered reader that writes to a cache file while reading."""
def __init__(
self, iter_bytes: io.BufferedReader, cache_path: Optional['Path'] = None
):
self._data = iter_bytes
self._cache = None
if cache_path:
self._cache_path = cache_path.with_suffix('.tmp')
self._cache = open(self._cache_path, 'wb')
self.closed = False
def read(self, size: Optional[int] = -1) -> bytes:
bytes = self._data.read(size)
if self._cache:
self._cache.write(bytes)
return bytes
def close(self):
if not self.closed and self._cache:
self._cache_path.rename(self._cache_path.with_suffix('.docs'))
self._cache.close()
class S3DocStore(AbstractDocStore):
"""Class to push and pull [`DocList`][docarray.DocList] to and from S3."""
@staticmethod
def list(namespace: str, show_table: bool = False) -> List[str]:
"""List all [`DocList`s][docarray.DocList] in the specified bucket and namespace.
:param namespace: The bucket and namespace to list. e.g. my_bucket/my_namespace
:param show_table: If true, a rich table will be printed to the console.
:return: A list of `DocList` names.
"""
bucket, namespace = namespace.split('/', 1)
s3 = boto3.resource('s3')
s3_bucket = s3.Bucket(bucket)
da_files = [
obj
for obj in s3_bucket.objects.all()
if obj.key.startswith(namespace) and obj.key.endswith('.docs')
]
da_names = [f.key.split('/')[-1].split('.')[0] for f in da_files]
if show_table:
from rich import box, filesize
from rich.console import Console
from rich.table import Table
table = Table(
title=f'You have {len(da_files)} DocLists in bucket s3://{bucket} under the namespace "{namespace}"',
box=box.SIMPLE,
highlight=True,
)
table.add_column('Name')
table.add_column('Last Modified', justify='center')
table.add_column('Size')
for da_name, da_file in zip(da_names, da_files):
table.add_row(
da_name,
str(da_file.last_modified),
str(filesize.decimal(da_file.size)),
)
Console().print(table)
return da_names
@staticmethod
def delete(name: str, missing_ok: bool = True) -> bool:
"""Delete the [`DocList`][docarray.DocList] object at the specified bucket and key.
:param name: The bucket and key to delete. e.g. my_bucket/my_key
:param missing_ok: If true, no error will be raised if the object does not exist.
:return: True if the object was deleted, False if it did not exist.
"""
bucket, name = name.split('/', 1)
s3 = boto3.resource('s3')
object = s3.Object(bucket, name + '.docs')
try:
object.load()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
if missing_ok:
return False
else:
raise ValueError(f'Object {name} does not exist')
else:
raise
object.delete()
return True
@classmethod
def push(
cls: Type[SelfS3DocStore],
docs: 'DocList',
name: str,
show_progress: bool = False,
) -> Dict:
"""Push this [`DocList`][docarray.DocList] object to the specified bucket and key.
:param docs: The `DocList` to push.
:param name: The bucket and key to push to. e.g. my_bucket/my_key
:param show_progress: If true, a progress bar will be displayed.
"""
return cls.push_stream(iter(docs), name, show_progress)
@staticmethod
def push_stream(
docs: Iterator['BaseDoc'],
name: str,
show_progress: bool = False,
) -> Dict:
"""Push a stream of documents to the specified bucket and key.
:param docs: a stream of documents
:param name: The bucket and key to push to. e.g. my_bucket/my_key
:param show_progress: If true, a progress bar will be displayed.
"""
bucket, name = name.split('/', 1)
binary_stream = _to_binary_stream(
docs, protocol='pickle', compress=None, show_progress=show_progress
)
# Upload to S3
with open(
f"s3://{bucket}/{name}.docs",
'wb',
compression='.gz',
transport_params={'multipart_upload': False},
) as fout:
while True:
try:
fout.write(next(binary_stream))
except StopIteration:
break
return {}
@classmethod
def pull(
cls: Type[SelfS3DocStore],
docs_cls: Type['DocList'],
name: str,
show_progress: bool = False,
local_cache: bool = False,
) -> 'DocList':
"""Pull a [`DocList`][docarray.DocList] from the specified bucket and key.
:param name: The bucket and key to pull from. e.g. my_bucket/my_key
:param show_progress: if true, display a progress bar.
:param local_cache: store the downloaded DocList to local cache
:return: a `DocList` object
"""
docs = docs_cls( # type: ignore
cls.pull_stream(
docs_cls, name, show_progress=show_progress, local_cache=local_cache
)
)
return docs
@classmethod
def pull_stream(
cls: Type[SelfS3DocStore],
docs_cls: Type['DocList'],
name: str,
show_progress: bool,
local_cache: bool,
) -> Iterator['BaseDoc']:
"""Pull a stream of Documents from the specified name.
Name is expected to be in the format of bucket/key.
:param name: The bucket and key to pull from. e.g. my_bucket/my_key
:param show_progress: if true, display a progress bar.
:param local_cache: store the downloaded DocList to local cache
:return: An iterator of Documents
"""
bucket, name = name.split('/', 1)
save_name = name.replace('/', '_')
cache_path = _get_cache_path() / f'{save_name}.docs'
source = _BufferedCachingReader(
open(f"s3://{bucket}/{name}.docs", 'rb', compression='.gz'),
cache_path=cache_path if local_cache else None,
)
if local_cache:
if cache_path.exists():
object_header = boto3.client('s3').head_object(
Bucket=bucket, Key=name + '.docs'
)
if cache_path.stat().st_size == object_header['ContentLength']:
logging.info(
f'Using cached file for {name} (size: {cache_path.stat().st_size})'
)
source = open(cache_path, 'rb')
return _from_binary_stream(
docs_cls.doc_type,
source,
protocol='pickle',
compress=None,
show_progress=show_progress,
)