Spaces:
Build error
Build error
Update src/auth/auth.py
Browse files- src/auth/auth.py +44 -76
src/auth/auth.py
CHANGED
|
@@ -78,87 +78,56 @@ def get_direct_access_token(username, password):
|
|
| 78 |
|
| 79 |
|
| 80 |
class S3Connector:
|
| 81 |
-
"""
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
"""
|
| 87 |
-
|
| 88 |
-
def __init__(self, endpoint_url, access_key_id, secret_access_key, region_name='default'):
|
| 89 |
-
"""
|
| 90 |
-
Initialize S3 connector with credentials and endpoint information.
|
| 91 |
-
|
| 92 |
-
Parameters
|
| 93 |
-
----------
|
| 94 |
-
endpoint_url : str
|
| 95 |
-
The URL of the S3 endpoint
|
| 96 |
-
access_key_id : str
|
| 97 |
-
The access key for authentication
|
| 98 |
-
secret_access_key : str
|
| 99 |
-
The secret key for authentication
|
| 100 |
-
region_name : str, optional
|
| 101 |
-
The AWS region name, by default 'default'
|
| 102 |
-
"""
|
| 103 |
self.endpoint_url = endpoint_url
|
| 104 |
self.access_key_id = access_key_id
|
| 105 |
self.secret_access_key = secret_access_key
|
| 106 |
self.region_name = region_name
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
print(f"Connection failed: {e}")
|
| 130 |
-
return False
|
| 131 |
-
|
| 132 |
-
def get_s3(self):
|
| 133 |
-
"""
|
| 134 |
-
Return the S3 resource object.
|
| 135 |
-
|
| 136 |
-
If not already connected, this method will first establish a connection.
|
| 137 |
-
|
| 138 |
-
Returns
|
| 139 |
-
-------
|
| 140 |
-
boto3.resources.factory.s3.ServiceResource
|
| 141 |
-
The boto3 S3 resource object for interacting with S3 storage
|
| 142 |
-
"""
|
| 143 |
-
if not self.s3:
|
| 144 |
-
self.connect()
|
| 145 |
-
return self.s3
|
| 146 |
-
|
| 147 |
def get_s3_client(self):
|
| 148 |
-
"""
|
| 149 |
-
Return the S3 client object.
|
| 150 |
-
|
| 151 |
-
If not already connected, this method will first establish a connection.
|
| 152 |
-
|
| 153 |
-
Returns
|
| 154 |
-
-------
|
| 155 |
-
boto3.client.S3
|
| 156 |
-
The boto3 S3 client object for interacting with S3 storage
|
| 157 |
-
"""
|
| 158 |
-
if not self.s3_client:
|
| 159 |
-
self.connect()
|
| 160 |
return self.s3_client
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
# if __name__ == "__main__":
|
| 163 |
|
| 164 |
|
|
@@ -177,4 +146,3 @@ class S3Connector:
|
|
| 177 |
# # Connect to S3
|
| 178 |
# s3_connector.connect()
|
| 179 |
# s3_client = s3_connector.get_s3_client()
|
| 180 |
-
|
|
|
|
| 78 |
|
| 79 |
|
| 80 |
class S3Connector:
|
| 81 |
+
"""A clean connector for S3-compatible storage services"""
|
| 82 |
+
|
| 83 |
+
def __init__(self, endpoint_url, access_key_id,
|
| 84 |
+
secret_access_key, region_name='default'):
|
| 85 |
+
"""Initialize the S3Connector with connection parameters"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
self.endpoint_url = endpoint_url
|
| 87 |
self.access_key_id = access_key_id
|
| 88 |
self.secret_access_key = secret_access_key
|
| 89 |
self.region_name = region_name
|
| 90 |
+
|
| 91 |
+
# Create session
|
| 92 |
+
self.session = boto3.session.Session()
|
| 93 |
+
|
| 94 |
+
# Initialize S3 resource
|
| 95 |
+
self.s3 = self.session.resource(
|
| 96 |
+
's3',
|
| 97 |
+
endpoint_url=self.endpoint_url,
|
| 98 |
+
aws_access_key_id=self.access_key_id,
|
| 99 |
+
aws_secret_access_key=self.secret_access_key,
|
| 100 |
+
region_name=self.region_name
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# Initialize S3 client
|
| 104 |
+
self.s3_client = self.session.client(
|
| 105 |
+
's3',
|
| 106 |
+
endpoint_url=self.endpoint_url,
|
| 107 |
+
aws_access_key_id=self.access_key_id,
|
| 108 |
+
aws_secret_access_key=self.secret_access_key,
|
| 109 |
+
region_name=self.region_name
|
| 110 |
+
)
|
| 111 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
def get_s3_client(self):
|
| 113 |
+
"""Get the boto3 S3 client"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return self.s3_client
|
| 115 |
+
|
| 116 |
+
def get_s3_resource(self):
|
| 117 |
+
"""Get the boto3 S3 resource"""
|
| 118 |
+
return self.s3
|
| 119 |
+
|
| 120 |
+
def get_bucket(self, bucket_name):
|
| 121 |
+
"""Get a specific bucket by name"""
|
| 122 |
+
return self.s3.Bucket(bucket_name)
|
| 123 |
+
|
| 124 |
+
def list_buckets(self):
|
| 125 |
+
"""List all available buckets"""
|
| 126 |
+
response = self.s3_client.list_buckets()
|
| 127 |
+
if 'Buckets' in response:
|
| 128 |
+
return [bucket['Name'] for bucket in response['Buckets']]
|
| 129 |
+
return []
|
| 130 |
+
|
| 131 |
# if __name__ == "__main__":
|
| 132 |
|
| 133 |
|
|
|
|
| 146 |
# # Connect to S3
|
| 147 |
# s3_connector.connect()
|
| 148 |
# s3_client = s3_connector.get_s3_client()
|
|
|