Do not always import boto3

Currently boto3 is not part of requirements but stevedore always tries
to import it and shows error in case boto3 is missing. This is not
a real error unless users actually enable s3 backends and can be quite
confusing.

This makes the driver code ignore ImportError and actually fail only
if users try to enable s3 backend without installing boto3.

Closes-Bug: #2007924
Change-Id: Ia94dd1d12a3d723f6263bdfb0966d416dfbae1af
This commit is contained in:
Takashi Kajinami 2023-02-21 15:57:58 +09:00
parent 64e25979a9
commit 7dc94f7a85
1 changed files with 17 additions and 4 deletions

View File

@ -21,10 +21,17 @@ import math
import re
import urllib
from boto3 import session as boto_session
from botocore import client as boto_client
from botocore import exceptions as boto_exceptions
from botocore import utils as boto_utils
try:
from boto3 import session as boto_session
from botocore import client as boto_client
from botocore import exceptions as boto_exceptions
from botocore import utils as boto_utils
except ImportError:
boto_session = None
boto_client = None
boto_exceptions = None
boto_utils = None
import eventlet
from oslo_config import cfg
from oslo_utils import encodeutils
@ -390,6 +397,12 @@ class Store(glance_store.driver.Store):
this method. If the store was not able to successfully configure
itself, it should raise `exceptions.BadStoreConfiguration`
"""
if boto_session is None:
reason = _("boto3 or botocore is not available.")
LOG.error(reason)
raise exceptions.BadStoreConfiguration(store_name="s3",
reason=reason)
self.s3_host = self._option_get('s3_store_host')
self.region_name = self._option_get('s3_store_region_name')
self.access_key = self._option_get('s3_store_access_key')