Skip to content

Commit

Permalink
Use b2sdk.v2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
elemental-lf committed Apr 25, 2023
1 parent f988214 commit 64407af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/benji/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def _handle_exception(exc_type, exc_value, exc_traceback):
# See: https://github.com/boto/boto3/issues/454
warnings.filterwarnings("ignore", category=ResourceWarning, message=r'unclosed.*<(?:ssl.SSLSocket|socket\.socket).*>')
# silence b2
logging.getLogger('b2').setLevel(logging.WARN)
logging.getLogger('b2sdk').setLevel(logging.WARN)

if os.getenv('BENJI_DEBUG_SQL') == '1':
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
26 changes: 11 additions & 15 deletions src/benji/storage/b2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
import logging
import random
import time
from io import BytesIO
from typing import Union, Iterable, Tuple

import b2sdk
import b2sdk.api
import b2sdk.file_version
from b2sdk.account_info.exception import MissingAccountData
from b2sdk.account_info.in_memory import InMemoryAccountInfo
from b2sdk.account_info.sqlite_account_info import SqliteAccountInfo
from b2sdk.download_dest import DownloadDestBytes
from b2sdk.exception import B2Error, FileNotPresent
from b2sdk.v2 import B2Api, UploadManager
from b2sdk.v2 import InMemoryAccountInfo
from b2sdk.v2 import SqliteAccountInfo
from b2sdk.v2.exception import MissingAccountData, B2Error, FileNotPresent

from benji.config import Config, ConfigDict
from benji.logging import logger
from benji.storage.base import ReadCacheStorageBase
Expand Down Expand Up @@ -45,15 +43,13 @@ def __init__(self, *, config: Config, name: str, module_configuration: ConfigDic
else:
account_info = InMemoryAccountInfo()

b2sdk.bucket.Bucket.MAX_UPLOAD_ATTEMPTS = Config.get_from_dict(module_configuration,
'uploadAttempts',
types=int)
UploadManager.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 = b2sdk.api.B2Api(account_info)
self.service = 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
Expand Down Expand Up @@ -93,9 +89,9 @@ def _write_object(self, key: str, data: bytes) -> None:

def _read_object(self, key: str) -> bytes:
for i in range(self._read_object_attempts):
data_io = DownloadDestBytes()
data_io = BytesIO()
try:
self.bucket.download_file_by_name(key, data_io)
self.bucket.download_file_by_name(key).save(data_io)
# This is overly broad!
except B2Error as exception:
if isinstance(exception, FileNotPresent):
Expand All @@ -112,7 +108,7 @@ def _read_object(self, key: str) -> bytes:
else:
break

return data_io.get_bytes_written()
return data_io.getvalue()

def _read_object_length(self, key: str) -> int:
for i in range(self._read_object_attempts):
Expand Down

0 comments on commit 64407af

Please sign in to comment.