-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#95] - add registration of existing metadata files stored on S3
- Loading branch information
1 parent
4496724
commit d91eca4
Showing
6 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
# import all adapters here to get them registered | ||
from api.adapters import hydroshare | ||
from api.adapters import s3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import boto3 | ||
import json | ||
from botocore.client import Config | ||
from botocore import UNSIGNED | ||
|
||
from api.adapters.base import AbstractRepositoryMetadataAdapter, AbstractRepositoryRequestHandler | ||
from api.adapters.utils import RepositoryType, register_adapter | ||
from api.models.catalog import DatasetMetadataDOC | ||
from api.models.user import Submission | ||
|
||
|
||
class _S3RequestHandler(AbstractRepositoryRequestHandler): | ||
|
||
def get_metadata(self, record_id: str): | ||
endpoint_url = record_id.split("+")[0] | ||
bucket_name = record_id.split("+")[1] | ||
file_key = record_id.split("+")[2] | ||
|
||
s3 = boto3.client('s3', config=Config(signature_version=UNSIGNED), endpoint_url=endpoint_url) | ||
|
||
response = s3.get_object(Bucket=bucket_name, Key=file_key) | ||
json_content = response['Body'].read().decode('utf-8') | ||
|
||
# Parse the JSON content | ||
data = json.loads(json_content) | ||
|
||
return data | ||
|
||
|
||
class S3MetadataAdapter(AbstractRepositoryMetadataAdapter): | ||
repo_api_handler = _S3RequestHandler() | ||
|
||
@staticmethod | ||
def to_catalog_record(metadata: dict) -> DatasetMetadataDOC: | ||
return DatasetMetadataDOC(**metadata) | ||
|
||
@staticmethod | ||
def to_repository_record(catalog_record: DatasetMetadataDOC): | ||
"""Converts dataset catalog record to hydroshare resource metadata""" | ||
raise NotImplementedError | ||
|
||
@staticmethod | ||
def update_submission(submission: Submission, repo_record_id: str) -> Submission: | ||
"""Sets additional hydroshare specific metadata to submission record""" | ||
|
||
submission.repository_identifier = repo_record_id | ||
submission.repository = RepositoryType.S3 | ||
return submission | ||
|
||
|
||
register_adapter(RepositoryType.S3, S3MetadataAdapter) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
class RepositoryType(str, Enum): | ||
HYDROSHARE = 'HYDROSHARE' | ||
S3 = 'S3' | ||
|
||
|
||
_adapter_registry = {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ uvicorn[standard] | |
motor | ||
beanie[httpx]==1.19.0 | ||
python-jose | ||
pydantic<2.* | ||
pydantic<2.* | ||
boto3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ beanie==1.19.0 | |
motor | ||
pydantic[dotenv]<2.* | ||
pydantic[email]<2.* | ||
rocketry==2.5.1 | ||
rocketry==2.5.1 | ||
boto3 |