Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s3: adds SCOAP3_BUCKET_UPLOAD_DIR and SCOAP3_REPO_S3_ENABLED #201

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions dags/common/scoap3_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@ class Scoap3Repository(IRepository):
def __init__(self):
super().__init__()
self.bucket = os.getenv("SCOAP3_BUCKET_NAME", "scoap3")
self.upload_dir = os.getenv("SCOAP3_BUCKET_UPLOAD_DIR", "files")
self.upload_enabled = os.getenv("SCOAP3_REPO_S3_ENABLED", False)
self.s3 = S3Service(self.bucket)
self.client = self.s3.meta.client

def copy_file(self, source_bucket, source_key, prefix=None):
if not self.upload_enabled:
return ""

if not prefix:
prefix = str(uuid4())

copy_source = {"Bucket": source_bucket, "Key": source_key}
filename = os.path.basename(source_key)
destination_key = f"{prefix}/{filename}"
destination_key = f"{self.upload_dir}/{prefix}/{filename}"

logger.info("Copying file from", copy_source=copy_source)
self.client.copy(
Expand Down Expand Up @@ -91,11 +96,14 @@ def download_files_for_aps(self, files, prefix=None):
return downloaded_files

def download_and_upload_to_s3(self, url, prefix=None, headers=None):
if not self.upload_enabled:
return ""

if not prefix:
prefix = str(uuid4())

filename = os.path.basename(url)
destination_key = f"{prefix}/{filename}"
destination_key = f"{self.upload_dir}/{prefix}/{filename}"

response = requests.get(url, headers=headers)
try:
Expand Down
Loading