|
| 1 | +from operator import truediv |
| 2 | +from pathlib import Path |
| 3 | + |
1 | 4 | import oss2 |
2 | 5 | from oss2.credentials import EnvironmentVariableCredentialsProvider |
3 | 6 |
|
|
6 | 9 |
|
7 | 10 | class OSS(ObjStore): |
8 | 11 | def __init__(self, endpoint: str, bucket: str): |
9 | | - """ |
10 | | - Construct a new client to communicate with the provider. |
11 | | - """ |
12 | | - |
13 | 12 | # Make sure environments OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET are exist. |
14 | 13 | auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider()) |
15 | 14 | self._bucket = oss2.Bucket(auth, endpoint, bucket) |
16 | 15 |
|
17 | | - def upload(self, src: str, dest: str): |
18 | | - """ |
19 | | - Upload will upload the obj to the provider. |
20 | | - """ |
| 16 | + def create_dir(self, dirname: str): |
| 17 | + self._bucket.put_object(dirname, "") |
| 18 | + |
| 19 | + def delete_dir(self, dirname: str): |
| 20 | + for obj in oss2.ObjectIterator(self._bucket, prefix=dirname): |
| 21 | + self._bucket.delete_object(obj.key) |
21 | 22 |
|
| 23 | + def upload(self, src: str, dest: str): |
22 | 24 | oss2.resumable_upload(self._bucket, dest, src) |
23 | 25 |
|
24 | | - def download(self, src: str, dest: str): |
25 | | - """ |
26 | | - Download will download the required obj from the provider. |
27 | | - """ |
| 26 | + def upload_dir(self, src_dir: str, dest_dir: str): |
| 27 | + for file in Path(src_dir).rglob("*"): |
| 28 | + if file.is_file(): |
| 29 | + self.upload(file, dest_dir + "/" + file.name) |
| 30 | + continue |
28 | 31 |
|
| 32 | + if file.is_dir(): |
| 33 | + # TODO: Support uploading subdirectory. |
| 34 | + print("Don't support uploading subdirectory yet") |
| 35 | + |
| 36 | + def download(self, src: str, dest: str): |
29 | 37 | oss2.resumable_download(self._bucket, src, dest) |
30 | 38 |
|
31 | | - def delete(self, filename: str): |
32 | | - """ |
33 | | - Delete will delete the obj from the provider. |
34 | | - """ |
| 39 | + def download_dir(self, src_dir: str, dest_dir: str): |
| 40 | + if not src_dir.endswith("/"): |
| 41 | + src_dir += "/" |
| 42 | + |
| 43 | + if not dest_dir.endswith("/"): |
| 44 | + dest_dir += "/" |
| 45 | + |
| 46 | + path = Path(dest_dir) |
| 47 | + if not path.exists(): |
| 48 | + path.mkdir(parents=True, exist_ok=True) |
| 49 | + |
| 50 | + for obj in oss2.ObjectIterator(self._bucket, prefix=src_dir, delimiter="/"): |
| 51 | + if obj.is_prefix(): # If this is a folder |
| 52 | + # TODO: This is enough for download models, but not enough for general download usage. |
| 53 | + print(f"Don't support downloading subdirectory: {obj.key} yet") |
| 54 | + else: # If this is a file |
| 55 | + self.download(obj.key, dest_dir + obj.key.split("/")[-1]) |
35 | 56 |
|
| 57 | + def delete(self, filename: str): |
36 | 58 | return self._bucket.delete_object(filename) |
37 | 59 |
|
38 | 60 | def exists(self, filename: str): |
39 | | - """ |
40 | | - Exists checks whether the obj exists in the provider. |
41 | | - """ |
42 | | - |
43 | 61 | return self._bucket.object_exists(filename) |
44 | | - |
45 | | - def accessible(self) -> bool: |
46 | | - """ |
47 | | - Accessible checks whether the obj is visitable. |
48 | | - """ |
49 | | - raise NotImplementedError("OSS not implemented") |
|
0 commit comments