Skip to content
Snippets Groups Projects
Select Git revision
  • d29c8a71f76e5262034512c02dd31f707008cc03
  • master default protected
  • dev protected
  • replication_test
  • release-1.10 protected
  • release-1.9 protected
  • 551-init-broker-service-permissions
  • 549-test-oai-pmh
  • 545-saving-multiple-times-breaks-pid-metadata
  • 499-standalone-compute-service-2
  • 539-load-tests
  • hotfix/helm-chart
  • luca_ba_new_interface
  • 534-bug-when-adding-access-to-user-that-is-not-registered-at-dashboard-service
  • release-1.8 protected
  • 533-integrate-semantic-recommendation
  • feature/openshift
  • 518-spark-doesn-t-map-the-headers-correct
  • 485-fixity-checks
  • 530-various-schema-problems-with-subsets
  • release-1.7 protected
  • v1.10.2 protected
  • v1.10.1 protected
  • v1.10.0-rc13 protected
  • v1.10.0-rc12 protected
  • v1.10.0-rc11 protected
  • v1.10.0-rc10 protected
  • v1.10.0-rc9 protected
  • v1.10.0-rc8 protected
  • v1.10.0-rc7 protected
  • v1.10.0-rc6 protected
  • v1.10.0-rc5 protected
  • v1.10.0-rc4 protected
  • v1.10.0-rc3 protected
  • v1.10.0-rc2 protected
  • v1.10.0rc1 protected
  • v1.10.0rc0 protected
  • v1.10.0 protected
  • v1.9.3 protected
  • v1.9.2 protected
  • v1.9.2-rc0 protected
41 results

UploadClient.py

  • Martin Weise's avatar
    Martin Weise authored
    0fff43ed
    History
    UploadClient.py 1.57 KiB
    import logging
    import os
    import re
    import sys
    from tusclient import client
    
    
    class UploadClient:
        """
        The UploadClient class for communicating with the DBRepo REST API. All parameters can be set also via environment \
        variables, e.g. set endpoint with DBREPO_ENDPOINT, username with DBREPO_USERNAME, etc. You can override the \
        constructor parameters with the environment variables.
    
        :param endpoint: The REST API endpoint. Optional. Default: "http://gateway-service/api/upload/files"
        """
        endpoint: str = None
    
        def __init__(self, endpoint: str = 'http://gateway-service/api/upload/files') -> None:
            logging.getLogger('requests').setLevel(logging.INFO)
            logging.getLogger('urllib3').setLevel(logging.INFO)
            logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-6s %(message)s', level=logging.DEBUG,
                                stream=sys.stdout)
            self.endpoint = os.environ.get('REST_UPLOAD_ENDPOINT', endpoint)
    
        def upload(self, file_path: str) -> str:
            """
            Imports a file through the Upload Service into the Storage Service.
    
            :param file_path: The file path on the local machine.
    
            :returns: Filename on the Storage Service, if successful.
            """
            tus_client = client.TusClient(url=self.endpoint)
            uploader = tus_client.uploader(file_path=file_path)
            uploader.upload()
            m = re.search('\\/([a-f0-9]+)\\+', uploader.url)
            filename = m.group(0)[1:-1]
            logging.debug(f'uploaded file {file_path} to storage service with key: {filename}')
            return filename