Exemplo n.º 1
0
    def upload(cls, path, project, file_name=None, overwrite=False, retry=5,
               timeout=10, part_size=PartSize.UPLOAD_MINIMUM_PART_SIZE,
               wait=True, api=None):

        """
        Uploads a file using multipart upload and returns an upload handle
        if the wait parameter is set to False. If wait is set to True it
        will block until the upload is completed.

        :param path: File path on local disc.
        :param project: Project identifier
        :param file_name: Optional file name.
        :param overwrite: If true will overwrite the file on the server.
        :param retry:  Number of retries if error occurs during upload.
        :param timeout:  Timeout for http requests.
        :param part_size:  Part size in bytes.
        :param wait:  If true will wait for upload to complete.
        :param api: Api instance.
        """

        api = api or cls._API
        project = Transform.to_project(project)
        upload = Upload(
            path, project, file_name=file_name, overwrite=overwrite,
            retry_count=retry, timeout=timeout, part_size=part_size, api=api
        )
        if wait:
            upload.start()
            upload.wait()
            return upload
        else:
            return upload
Exemplo n.º 2
0
    def upload(cls, path, project=None, parent=None, file_name=None,
               overwrite=False, retry=5, timeout=10,
               part_size=PartSize.UPLOAD_MINIMUM_PART_SIZE, wait=True,
               api=None):
        """
        Uploads a file using multipart upload and returns an upload handle
        if the wait parameter is set to False. If wait is set to True it
        will block until the upload is completed.

        :param path: File path on local disc.
        :param project: Project identifier
        :param parent: Parent folder identifier
        :param file_name: Optional file name.
        :param overwrite: If true will overwrite the file on the server.
        :param retry:  Number of retries if error occurs during upload.
        :param timeout:  Timeout for http requests.
        :param part_size:  Part size in bytes.
        :param wait:  If true will wait for upload to complete.
        :param api: Api instance.
        """

        api = api or cls._API
        extra = {'resource': cls.__name__, 'query': {
            'path': path,
            'project': project,
            'file_name': file_name,
            'overwrite': overwrite,
            'retry': retry,
            'timeout': timeout,
            'part_size': part_size,
            'wait': wait,
        }}
        logger.info('Uploading file', extra=extra)

        if not project and not parent:
            raise SbgError('A project or parent identifier is required.')

        if project and parent:
            raise SbgError(
                'Project and parent identifiers are mutually exclusive.'
            )

        if project:
            project = Transform.to_project(project)

        if parent:
            parent = Transform.to_file(parent)

        upload = Upload(
            file_path=path, project=project, parent=parent,
            file_name=file_name, overwrite=overwrite, retry_count=retry,
            timeout=timeout, part_size=part_size, api=api
        )
        if wait:
            upload.start()
            upload.wait()
            return upload
        else:
            return upload
Exemplo n.º 3
0
    def upload(cls, path, project=None, parent=None, file_name=None,
               overwrite=False, retry=5, timeout=60, part_size=None, wait=True,
               api=None):
        """
        Uploads a file using multipart upload and returns an upload handle
        if the wait parameter is set to False. If wait is set to True it
        will block until the upload is completed.

        :param path: File path on local disc.
        :param project: Project identifier
        :param parent: Parent folder identifier
        :param file_name: Optional file name.
        :param overwrite: If true will overwrite the file on the server.
        :param retry:  Number of retries if error occurs during upload.
        :param timeout:  Timeout for http requests.
        :param part_size:  Part size in bytes.
        :param wait:  If true will wait for upload to complete.
        :param api: Api instance.
        """

        api = api or cls._API
        extra = {'resource': cls.__name__, 'query': {
            'path': path,
            'project': project,
            'file_name': file_name,
            'overwrite': overwrite,
            'retry': retry,
            'timeout': timeout,
            'part_size': part_size,
            'wait': wait,
        }}
        logger.info('Uploading file', extra=extra)

        if not project and not parent:
            raise SbgError('A project or parent identifier is required.')

        if project and parent:
            raise SbgError(
                'Project and parent identifiers are mutually exclusive.'
            )

        if project:
            project = Transform.to_project(project)

        if parent:
            parent = Transform.to_file(parent)

        upload = Upload(
            file_path=path, project=project, parent=parent,
            file_name=file_name, overwrite=overwrite, retry_count=retry,
            timeout=timeout, part_size=part_size, api=api
        )
        if wait:
            upload.start()
            upload.wait()
            return upload
        else:
            return upload
Exemplo n.º 4
0
def test_file_upload(api, given, empty_file, project_id, parent_id, no_api):
    upload_id = generator.uuid4()
    api_instance = api
    file_id = generator.uuid4()
    file_part_url = generator.url()
    file_content = generator.uuid4()
    given.uploads.initialized_upload(part_size=1, upload_id=upload_id)
    given.uploads.got_file_part(file_part_url)
    given.uploads.got_etag(file_part_url)
    given.uploads.reported_part()
    given.uploads.finalized_upload(file_id)

    temp_file = tempfile.NamedTemporaryFile('w', delete=False, dir='.')
    if not empty_file:
        temp_file.write(file_content)
    temp_file.close()

    project_and_parent = project_id is not None and parent_id is not None
    no_project_or_parent = project_id is None and parent_id is None
    if no_api:
        api_instance = None
    if empty_file or project_and_parent or no_project_or_parent or no_api:
        with pytest.raises(SbgError):
            Upload(temp_file.name,
                   project=project_id,
                   parent=parent_id,
                   api=api_instance,
                   part_size=1)
    else:
        upload = Upload(temp_file.name,
                        project=project_id,
                        parent=parent_id,
                        api=api,
                        part_size=1)
        upload.start()
        upload.wait()
        result = upload.result()

        assert upload.status == 'COMPLETED'
        assert result.id == file_id
        assert upload.duration > 0
        assert upload.file_name == os.path.basename(temp_file.name)
        assert upload.start_time > 0

    os.remove(temp_file.name)
Exemplo n.º 5
0
def test_file_upload(api, given, empty_file, project_id, parent_id, no_api,
                     tmpdir):
    upload_id = generator.uuid4()
    api_instance = api
    file_id = generator.uuid4()
    file_part_url = generator.url()
    file_name = generator.uuid4()
    file_content = generator.uuid4()
    given.uploads.initialized_upload(
        part_size=PartSize.UPLOAD_RECOMMENDED_SIZE, upload_id=upload_id)
    given.uploads.got_file_part(file_part_url)
    given.uploads.got_etag(file_part_url)
    given.uploads.reported_part()
    given.uploads.finalized_upload(file_id)

    with open(tmpdir / file_name, 'w') as temp_file:
        if not empty_file:
            temp_file.write(str(file_content))

    project_and_parent = project_id is not None and parent_id is not None
    no_project_or_parent = project_id is None and parent_id is None
    if no_api:
        api_instance = None
    if project_and_parent or no_project_or_parent or no_api:
        with pytest.raises(SbgError):
            Upload(temp_file.name,
                   project=project_id,
                   parent=parent_id,
                   api=api_instance,
                   part_size=PartSize.UPLOAD_RECOMMENDED_SIZE)
    else:
        upload = Upload(temp_file.name,
                        project=project_id,
                        parent=parent_id,
                        api=api,
                        part_size=PartSize.UPLOAD_RECOMMENDED_SIZE)
        upload.start()
        upload.wait()
        result = upload.result()

        assert upload.status == TransferState.COMPLETED
        assert result.id == file_id
        assert upload.duration > 0
        assert upload.file_name == os.path.basename(temp_file.name)
        assert upload.start_time > 0