Exemplo n.º 1
0
    def __init__(self, account_id, application_key, bucket_id,
                 enable_hashfiles, temp_folder, use_disk):
        account_info = InMemoryAccountInfo()
        self.api = B2Api(account_info)
        self.api.authorize_account('production', account_id, application_key)
        self.bucket_api = CachedBucket(self.api, bucket_id)

        self.logger = logging.getLogger("%s.%s" %
                                        (__name__, self.__class__.__name__))

        self.enable_hashfiles = enable_hashfiles
        self.temp_folder = temp_folder
        self.use_disk = use_disk

        if self.use_disk:
            if os.path.exists(self.temp_folder):
                self.logger.error("Temporary folder exists, exiting")
                exit(1)

            os.makedirs(self.temp_folder)
            self.B2File = B2FileDisk
        else:
            self.B2File = B2SequentialFileMemory

        self._directories = DirectoryStructure()
        self.local_directories = []

        self.open_files = defaultdict(self.B2File)

        self.fd = 0
Exemplo n.º 2
0
Arquivo: b2.py Projeto: jubalh/backy2
    def __init__(self, config):
        super().__init__(config)

        our_config = config.get('dataBackend.{}'.format(self.NAME), types=dict)
        account_id = config.get_from_dict(our_config, 'accountId', types=str)
        application_key = config.get_from_dict(our_config,
                                               'applicationKey',
                                               types=str)
        bucket_name = config.get_from_dict(our_config, 'bucketName', types=str)

        account_info_file = config.get_from_dict(our_config,
                                                 'accountInfoFile',
                                                 None,
                                                 types=str)
        if account_info_file is not None:
            account_info = SqliteAccountInfo(file_name=account_info_file)
        else:
            account_info = InMemoryAccountInfo()

        b2.bucket.Bucket.MAX_UPLOAD_ATTEMPTS = config.get_from_dict(
            our_config,
            'uploadAttempts',
            types=int,
            check_func=lambda v: v >= 1,
            check_message='Must be a positive integer')

        self._write_object_attempts = config.get_from_dict(
            our_config,
            'writeObjectAttempts',
            types=int,
            check_func=lambda v: v >= 1,
            check_message='Must be a positive integer')

        self._read_object_attempts = config.get_from_dict(
            our_config,
            'readObjectAttempts',
            types=int,
            check_func=lambda v: v >= 1,
            check_message='Must be a positive integer')

        self.service = b2.api.B2Api(account_info)
        if account_info_file is not None:
            try:
                # This temporarily disables all logging as the b2 library does some very verbose logging
                # of the exception we're trying to catch here...
                logging.disable(logging.ERROR)
                _ = self.service.get_account_id()
                logging.disable(logging.NOTSET)
            except MissingAccountData:
                self.service.authorize_account('production', account_id,
                                               application_key)
        else:
            self.service.authorize_account('production', account_id,
                                           application_key)

        self.bucket = self.service.get_bucket_by_name(bucket_name)
Exemplo n.º 3
0
 def __init__(self, account_id, application_key, bucket_id, db_file):
     try:
         account_info = InMemoryAccountInfo()
         self.api = B2Api(account_info)
         self.api.authorize_account("production", account_id,
                                    application_key)
     except B2ConnectionError as e:
         print(e)
         raise ConnectionError
     self.bucket_api = Bucket(self.api, bucket_id)
     self.file_info_store = FileInfoStore(db_file)
Exemplo n.º 4
0
    def __init__(self, *, config: Config, name: str,
                 module_configuration: ConfigDict):
        super().__init__(config=config,
                         name=name,
                         module_configuration=module_configuration)

        account_id = Config.get_from_dict(module_configuration,
                                          'accountId',
                                          None,
                                          types=str)
        if account_id is None:
            account_id_file = Config.get_from_dict(module_configuration,
                                                   'accountIdFile',
                                                   types=str)
            with open(account_id_file, 'r') as f:
                account_id = f.read().rstrip()
        application_key = Config.get_from_dict(module_configuration,
                                               'applicationKey',
                                               None,
                                               types=str)
        if application_key is None:
            application_key_file = Config.get_from_dict(module_configuration,
                                                        'applicationKeyFile',
                                                        types=str)
            with open(application_key_file, 'r') as f:
                application_key = f.read().rstrip()

        bucket_name = Config.get_from_dict(module_configuration,
                                           'bucketName',
                                           types=str)

        account_info_file = Config.get_from_dict(module_configuration,
                                                 'accountInfoFile',
                                                 None,
                                                 types=str)
        if account_info_file is not None:
            account_info = SqliteAccountInfo(file_name=account_info_file)
        else:
            account_info = InMemoryAccountInfo()

        b2.bucket.Bucket.MAX_UPLOAD_ATTEMPTS = Config.get_from_dict(
            module_configuration, 'uploadAttempts', types=int)

        self._write_object_attempts = Config.get_from_dict(
            module_configuration, 'writeObjectAttempts', types=int)

        self._read_object_attempts = Config.get_from_dict(module_configuration,
                                                          'readObjectAttempts',
                                                          types=int)

        self.service = b2.api.B2Api(account_info)
        if account_info_file is not None:
            try:
                # This temporarily disables all logging as the b2 library does some very verbose logging
                # of the exception we're trying to catch here...
                logging.disable(logging.ERROR)
                _ = self.service.get_account_id()
                logging.disable(logging.NOTSET)
            except MissingAccountData:
                self.service.authorize_account('production', account_id,
                                               application_key)
        else:
            self.service.authorize_account('production', account_id,
                                           application_key)

        self.bucket = self.service.get_bucket_by_name(bucket_name)