Skip to content

Commit

Permalink
Download cached resource from S3
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed May 14, 2024
1 parent 75cd56d commit 9442ae4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ckanext/archiver/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ckanext.archiver import lib
from ckanext.archiver.model import Archival, aggregate_archivals_for_a_dataset
from ckanext.archiver import cli
from ckanext.archiver.views import s3_archived

log = logging.getLogger(__name__)

Expand All @@ -27,6 +28,7 @@ class ArchiverPlugin(p.SingletonPlugin, p.toolkit.DefaultDatasetForm):
p.implements(p.IAuthFunctions)
p.implements(p.ITemplateHelpers)
p.implements(p.IPackageController, inherit=True)
p.implements(p.IBlueprint)

if p.toolkit.check_ckan_version(min_version='2.9.0'):
p.implements(p.IClick)
Expand Down Expand Up @@ -230,6 +232,12 @@ def before_dataset_index(self, pkg_dict):

def get_commands(self):
return cli.get_commands()

# IBlueprint

def get_blueprint(self):

return s3_archived


class TestIPipePlugin(p.SingletonPlugin):
Expand Down
2 changes: 2 additions & 0 deletions ckanext/archiver/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,8 @@ def archive_resource(context, resource, log, result=None, url_timeout=30):
# delete the file from archive storage
if os.path.isfile(saved_file):
os.remove(saved_file)
# save in database for cachefile_path the path from S3 storage
saved_file = s3_file_path

else:
shutil.move(result['saved_file'], saved_file)
Expand Down
23 changes: 23 additions & 0 deletions ckanext/archiver/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import Blueprint
from ckan import model
from ckanext.archiver.model import Archival
from ckanext.cloudstorage.helpers import generate_download_signed_url_v4
import ckantoolkit as tk


redirect = tk.redirect_to
config = tk.config

s3_archived = Blueprint("s3_archived_redirect", __name__)


def s3_archived_redirect(id, resource_id, filename):

q = model.Session.query(Archival).filter_by(resource_id=resource_id)
filepath = model.Session.execute(q).first()[8]
url = generate_download_signed_url_v4(filepath)
return redirect(url)


s3_archived.add_url_rule(u'/tmp/<id>/<resource_id>/<filename>',
view_func=s3_archived_redirect)

0 comments on commit 9442ae4

Please sign in to comment.