1
0
mirror of https://github.com/mail-in-a-box/mailinabox.git synced 2026-01-23 09:22:52 +01:00

Adding a guard so that if the user uses a brand new S3 bucket with no contents a KeyError is not received when configuring backups.

This commit is contained in:
kloeffler95 2026-01-11 18:06:42 -07:00
parent 3b53bf5ae4
commit 5aecfdc725

View File

@ -505,12 +505,10 @@ def list_target_files(config):
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
# separate bucket from path in target # separate bucket from path in target
bucket = target.path[1:].split('/')[0] bucket_path = target.path.lstrip('/')
path = '/'.join(target.path[1:].split('/')[1:]) + '/' bucket, _, path = bucket_path.partition('/')
if path and not path.endswith('/'):
# If no prefix is specified, set the path to '', otherwise boto won't list the files path += '/'
if path == '/':
path = ''
if bucket == "": if bucket == "":
msg = "Enter an S3 bucket name." msg = "Enter an S3 bucket name."
@ -525,10 +523,11 @@ def list_target_files(config):
endpoint_url=f'https://{target.hostname}', \ endpoint_url=f'https://{target.hostname}', \
aws_access_key_id=config['target_user'], \ aws_access_key_id=config['target_user'], \
aws_secret_access_key=config['target_pass']) aws_secret_access_key=config['target_pass'])
bucket_objects = s3.list_objects_v2(Bucket=bucket, Prefix=path)['Contents'] response = s3.list_objects_v2(Bucket=bucket, Prefix=path)
bucket_objects = response.get('Contents', [])
backup_list = [(key['Key'][len(path):], key['Size']) for key in bucket_objects] backup_list = [(key['Key'][len(path):], key['Size']) for key in bucket_objects]
except ClientError as e: except ClientError as e:
raise ValueError(e) raise ValueError(msg)
return backup_list return backup_list
if target.scheme == 'b2': if target.scheme == 'b2':
from b2sdk.v1 import InMemoryAccountInfo, B2Api from b2sdk.v1 import InMemoryAccountInfo, B2Api