Example #1
0
    def file(self, name_or_file_handler, properties=None):
        """ Upload a file - file can be either a filename or a file handler """
        close_file = False
        if isinstance(name_or_file_handler, str):
            filepath = name_or_file_handler
            fh = open(filepath, 'rb')
            close_file = True
        else:
            filepath = name_or_file_handler.name
            fh = name_or_file_handler
        filename = os.path.basename(filepath)

        params = dict(filename=filename, properties=properties)

        first_response = self.api.request(
            endpoint='%s/files' % self.api._UPLOAD_BASE_URL,
            method='post',
            json={k: v
                  for k, v in params.items() if v is not None},
        )

        promise = first_response.json()
        file_id = promise['file_id']
        location = promise['Location']

        self.api.request(endpoint=location,
                         method='put',
                         data=fh,
                         headers={
                             "x-amz-server-side-encryption": "AES256",
                         })

        if close_file:  # we opened the handler, so let's close it
            fh.close()
        return File(file_id, api=self.api, name=filename)
Example #2
0
    def list(
        self,
        start_date=None,
        end_date=None,
        tags=None,
        status=None,
        filename=None,
    ):
        params = dict(
            start_date=as_datetime_str(start_date),
            end_date=as_datetime_str(end_date),
            tags=tags,
            status=status,
            filename=filename,
        )

        # the list of files is splitted in pages - let's collect them
        while True:
            response = self.api.request(
                '%s/files' % self.api._UPLOAD_BASE_URL,
                params={k: v
                        for k, v in params.items() if v is not None})
            data = response.json()
            if 'results' in data:
                for r in data['results']:
                    yield File(
                        r['file_id'],
                        status=r.get('status'),
                        name=r.get('name'),
                        api=self.api,
                    )
            if data.get('next_page_key'):  # next page
                params['next_page_key'] = json.dumps(data['next_page_key'])
            else:
                break
Example #3
0
    def file(self, name_or_file_handler, folder=None, properties=None):
        """ Upload a file - file can be either a file name or a file handler """
        close_file = False
        if isinstance(name_or_file_handler, str):
            filepath = name_or_file_handler
            fh = open(filepath, 'rb')
            close_file = True
        else:
            filepath = name_or_file_handler.name
            fh = name_or_file_handler
        file_name = os.path.basename(filepath)
        content_type = mimetypes.guess_type(file_name)[0]
        if isinstance(folder, Folder):
            folder_id = folder.folder_id
        else:
            folder_id = folder

        params = dict(
            file_name=file_name,
            properties=properties,
            folder_id=folder_id,
        )

        first_response = self.api.request(
            endpoint='%s/files' % self.api._UPLOAD_BASE_URL,
            method='post',
            json={k: v
                  for k, v in params.items() if v is not None},
        )

        promise = first_response.json()
        file_id = promise['file_id']
        location = promise['Location']

        attempt = 3
        while attempt:
            try:
                fh.seek(0)
                self.api.request(endpoint=location,
                                 method='put',
                                 data=fh,
                                 headers={
                                     "x-amz-server-side-encryption": "AES256",
                                     "Content-Type": content_type,
                                 })
            except APIException as e:
                attempt -= 1
                if attempt == 0:
                    raise e  # raise the exception
                logger.warning("Error with PUT file operation - retring")
                sleep(1)
            else:
                break

        if close_file:  # we opened the handler, so let's close it
            fh.close()
        return File(file_id, api=self.api, file_name=file_name)
Example #4
0
    def list(
        self,
        start_date=None,
        end_date=None,
        tags=None,
        status=None,
        file_name=None,
        folder_id=None,
        page_size=50,
    ):
        params = dict(
            start_date=as_datetime_str(start_date),
            end_date=as_datetime_str(end_date),
            tags=tags,
            status=status,
            file_name=file_name,
            folder_id=folder_id,
            page_size=page_size,
        )

        # the list of files is splitted in pages - let's collect them
        get_next, offset = False, 0
        while True:
            response = self.api.request(
                '%s/files' % self.api._UPLOAD_BASE_URL,
                params={k: v
                        for k, v in params.items() if v is not None})
            data = response.json()
            if 'results' in data:
                results = data['results']
                for r in results:
                    get_next = len(results) == page_size
                    file_params = {
                        field: r.get(field)
                        for field in FILE_FIELDS
                    }
                    yield File(api=self.api, **file_params)
            if not get_next:
                break
            offset += page_size
            params['offset'] = offset
Example #5
0
 def get(self, file_id):
     return File(file_id, api=self.api)