diff --git a/dnanexus-make-url-example/dxapp.json b/dnanexus-make-url-example/dxapp.json new file mode 100644 index 0000000..0c5429d --- /dev/null +++ b/dnanexus-make-url-example/dxapp.json @@ -0,0 +1,54 @@ +{ + "name": "generate_external_download_urls", + "title": "Generate External Download URLs", + "summary": "Generates External Download URLs", + "dxapi": "1.0.0", + "version": "0.0.1", + "inputSpec": [ + { + "name": "input_files", + "label": "Input Files", + "class": "array:file", + "optional": false + }, + { + "name": "token_file", + "label": "Token File", + "class": "file", + "optional": false + } + ], + "outputSpec": [ + { + "name": "download_urls", + "label": "Download URLs", + "class": "array:string" + } + ], + "runSpec": { + "timeoutPolicy": { + "*": { + "hours": 48 + } + }, + "interpreter": "python3", + "file": "src/generate_external_download_urls.py", + "distribution": "Ubuntu", + "release": "20.04", + "version": "0" + }, + "access": { + "network": [ + "*" + ] + }, + "regionalOptions": { + "aws:us-east-1": { + "systemRequirements": { + "*": { + "instanceType": "mem1_ssd1_v2_x4" + } + } + } + } +} diff --git a/dnanexus-make-url-example/src/generate_external_download_urls.py b/dnanexus-make-url-example/src/generate_external_download_urls.py new file mode 100644 index 0000000..39254cc --- /dev/null +++ b/dnanexus-make-url-example/src/generate_external_download_urls.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +import os +import dxpy + +@dxpy.entry_point('main') +def main(input_files, token_file): + + # Parse token_hash from token file input + token_file = dxpy.DXFile(token_file) + dxpy.download_dxfile(token_file.get_id(), "token_file") + + with open('token_file', 'r') as f: + token_hash = f.readline().strip() + f.close() + + # Create the environment.json file and get pre-set values for parent_project and job ids + job_id = os.environ.get('DX_JOB_ID') + parent_project = os.environ.get('DX_PROJECT_CONTEXT_ID') + dxpy.config.write("DX_PROJECT_CONTEXT_ID", parent_project) + dxpy.config.write("DX_CLI_WD", "/") + dxpy.config.save() + + # Unset and clear the environment variables and the values in the environment.json file + del os.environ['DX_WORKSPACE_ID'] + del os.environ['DX_APISERVER_HOST'] + del os.environ['DX_PROJECT_CONTEXT_ID'] + del os.environ['DX_JOB_ID'] + del os.environ['DX_APISERVER_PORT'] + del os.environ['DX_APISERVER_PROTOCOL'] + del os.environ['DX_SECURITY_CONTEXT'] + dxpy.config.clear(reset=True) + + # set config variables and save them to environment.json file + dxpy.config.write("DX_JOB_ID", job_id) + dxpy.config.write("DX_PROJECT_CONTEXT_ID", parent_project) + dxpy.config.write("DX_CLI_WD", "/") + sec_context = '{"auth_token":"' + token_hash + '","auth_token_type":"Bearer"}' + os.environ['DX_SECURITY_CONTEXT'] = sec_context + dxpy.set_security_context(json.loads(sec_context)) + dxpy.config.write("DX_SECURITY_CONTEXT", sec_context) + dxpy.config.write("DX_USERNAME", dxpy.whoami()) + dxpy.config.save() + + # Get URLs for the input files + all_urls = [] + + for item in input_files: + + dxfile = dxpy.DXFile(item, project=parent_project) + + download_url, headers = dxfile.get_download_url(preauthenticated=True, duration=86400, filename=dxfile.name, project=parent_project) + + all_urls.append(download_url) + else: + print("All files processed successfully...") + + output = {} + output["download_urls"] = all_urls + + return output + +dxpy.run() \ No newline at end of file