| 
									
										
										
										
											2024-11-07 19:31:49 +08:00
										 |  |  | import posixpath | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | from collections.abc import Generator | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  | import oss2 as aliyun_s3  # type: ignore | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  | from configs import dify_config | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | from extensions.storage.base_storage import BaseStorage | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-09 14:15:27 +08:00
										 |  |  | class AliyunOssStorage(BaseStorage): | 
					
						
							|  |  |  |     """Implementation for Aliyun OSS storage.""" | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         super().__init__() | 
					
						
							|  |  |  |         self.bucket_name = dify_config.ALIYUN_OSS_BUCKET_NAME | 
					
						
							|  |  |  |         self.folder = dify_config.ALIYUN_OSS_PATH | 
					
						
							| 
									
										
										
										
											2024-05-06 11:56:04 +08:00
										 |  |  |         oss_auth_method = aliyun_s3.Auth | 
					
						
							|  |  |  |         region = None | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |         if dify_config.ALIYUN_OSS_AUTH_VERSION == "v4": | 
					
						
							| 
									
										
										
										
											2024-05-06 11:56:04 +08:00
										 |  |  |             oss_auth_method = aliyun_s3.AuthV4 | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             region = dify_config.ALIYUN_OSS_REGION | 
					
						
							|  |  |  |         oss_auth = oss_auth_method(dify_config.ALIYUN_OSS_ACCESS_KEY, dify_config.ALIYUN_OSS_SECRET_KEY) | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  |         self.client = aliyun_s3.Bucket( | 
					
						
							| 
									
										
										
										
											2024-05-06 11:56:04 +08:00
										 |  |  |             oss_auth, | 
					
						
							| 
									
										
										
										
											2024-10-22 11:01:32 +08:00
										 |  |  |             dify_config.ALIYUN_OSS_ENDPOINT, | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  |             self.bucket_name, | 
					
						
							| 
									
										
										
										
											2024-05-06 11:56:04 +08:00
										 |  |  |             connect_timeout=30, | 
					
						
							|  |  |  |             region=region, | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def save(self, filename, data): | 
					
						
							| 
									
										
										
										
											2024-09-14 11:07:16 +08:00
										 |  |  |         self.client.put_object(self.__wrapper_folder_filename(filename), data) | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def load_once(self, filename: str) -> bytes: | 
					
						
							| 
									
										
										
										
											2024-10-15 09:42:39 +09:00
										 |  |  |         obj = self.client.get_object(self.__wrapper_folder_filename(filename)) | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |         data: bytes = obj.read() | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  |         return data | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def load_stream(self, filename: str) -> Generator: | 
					
						
							| 
									
										
										
										
											2024-10-25 10:11:25 +08:00
										 |  |  |         obj = self.client.get_object(self.__wrapper_folder_filename(filename)) | 
					
						
							|  |  |  |         while chunk := obj.read(4096): | 
					
						
							|  |  |  |             yield chunk | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     def download(self, filename: str, target_filepath): | 
					
						
							| 
									
										
										
										
											2024-09-14 11:07:16 +08:00
										 |  |  |         self.client.get_object_to_file(self.__wrapper_folder_filename(filename), target_filepath) | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     def exists(self, filename: str): | 
					
						
							| 
									
										
										
										
											2024-09-14 11:07:16 +08:00
										 |  |  |         return self.client.object_exists(self.__wrapper_folder_filename(filename)) | 
					
						
							| 
									
										
										
										
											2024-04-29 18:22:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     def delete(self, filename: str): | 
					
						
							| 
									
										
										
										
											2024-09-14 11:07:16 +08:00
										 |  |  |         self.client.delete_object(self.__wrapper_folder_filename(filename)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-24 18:38:51 +08:00
										 |  |  |     def __wrapper_folder_filename(self, filename: str) -> str: | 
					
						
							| 
									
										
										
										
											2024-11-07 19:31:49 +08:00
										 |  |  |         return posixpath.join(self.folder, filename) if self.folder else filename |