MCPcopy Create free account
hub / github.com/modelscope/modelscope / HTTPStorage

Class HTTPStorage

modelscope/fileio/file.py:124–173  ·  view source on GitHub ↗

HTTP and HTTPS storage.

Source from the content-addressed store, hash-verified

122
123
124class HTTPStorage(Storage):
125 """HTTP and HTTPS storage."""
126
127 def read(self, url):
128 # TODO @wenmeng.zwm add progress bar if file is too large
129 r = requests.get(url)
130 r.raise_for_status()
131 return r.content
132
133 def read_text(self, url):
134 r = requests.get(url)
135 r.raise_for_status()
136 return r.text
137
138 @contextlib.contextmanager
139 def as_local_path(
140 self, filepath: str) -> Generator[Union[str, Path], None, None]:
141 """Download a file from ``filepath``.
142
143 ``as_local_path`` is decorated by :meth:`contextlib.contextmanager`. It
144 can be called with ``with`` statement, and when exists from the
145 ``with`` statement, the temporary path will be released.
146
147 Args:
148 filepath (str): Download a file from ``filepath``.
149
150 Examples:
151 >>> storage = HTTPStorage()
152 >>> # After existing from the ``with`` clause,
153 >>> # the path will be removed
154 >>> with storage.get_local_path('http://path/to/file') as path:
155 ... # do something here
156 """
157 try:
158 f = tempfile.NamedTemporaryFile(delete=False)
159 f.write(self.read(filepath))
160 f.close()
161 yield f.name
162 finally:
163 os.remove(f.name)
164
165 def write(self, obj: bytes, url: Union[str, Path]) -> None:
166 raise NotImplementedError('write is not supported by HTTP Storage')
167
168 def write_text(self,
169 obj: str,
170 url: Union[str, Path],
171 encoding: str = 'utf-8') -> None:
172 raise NotImplementedError(
173 'write_text is not supported by HTTP Storage')
174
175
176class OSSStorage(Storage):

Callers 6

load_bytes_from_urlFunction · 0.90
generate_scp_from_urlFunction · 0.90
generate_text_from_urlFunction · 0.90
generate_scp_for_svFunction · 0.90
generate_sd_scp_from_urlFunction · 0.90
test_http_storageMethod · 0.90

Calls

no outgoing calls

Tested by 1

test_http_storageMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…