本文整理汇总了Python中swiftclient.Connection.head_container方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.head_container方法的具体用法?Python Connection.head_container怎么用?Python Connection.head_container使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swiftclient.Connection
的用法示例。
在下文中一共展示了Connection.head_container方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SwiftBackend
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
class SwiftBackend(BakthatBackend):
"""Backend to handle OpenStack Swift upload/download."""
def __init__(self, conf={}, profile="default"):
BakthatBackend.__init__(self, conf, profile)
from swiftclient import Connection, ClientException
self.con = Connection(self.conf["auth_url"],
self.conf["access_key"],
self.conf["secret_key"],
auth_version=self.conf["auth_version"],
tenant_name=self.conf["tenant_name"],
insecure=self.conf["insecure"])
region_name = self.conf["region_name"]
if region_name == DEFAULT_LOCATION:
region_name = ""
try:
self.con.head_container(self.conf["s3_bucket"])
except ClientException, e:
self.con.put_container(self.conf["s3_bucket"])
self.container = self.conf["s3_bucket"]
self.container_key = "s3_bucket"
示例2: SwiftStorage
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
class SwiftStorage(object):
def __init__(self, *args, **kwargs):
super(SwiftStorage, self).__init__()
self.__dict__.update(kwargs)
config_data = get_config_json()
# Load config variables.
self.auth_url = config_data.get('swift').get('auth_url')
self.access_key = config_data.get('swift').get('access_key')
self.secret_key = config_data.get('swift').get('secret_key')
self.auth_version = config_data.get('swift').get('auth_version')
self.tenant_name = config_data.get('swift').get('tenant_name')
self.insecure = True
self.container = config_data.get('swift').get('container')
self.name_backup = kwargs.get('name_backup', None)
self.conn = Connection(
authurl=self.auth_url,
user=self.access_key,
key=self.secret_key,
auth_version=self.auth_version,
tenant_name=self.tenant_name,
insecure=self.insecure)
try:
self.conn.head_container(self.container)
except:
self.conn.put_container(self.container)
if not self.name_backup:
raise SystemExit(_error_codes.get(103))
def send_file(self, filename, **kwargs):
try:
backup_file = open(filename, 'rb')
response = self.conn.put_object(
self.container, filename, backup_file)
print('Uploading file {0} to container "{1}" on swift'.
format(filename,
self.container))
except Exception, e:
raise SystemExit(_error_codes.get(115).format(
filename, 'Swift', e))
示例3: __init__
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
#.........这里部分代码省略.........
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Retrieving Object Head failed %s", sys.exc_info()[1])
@monitoring.timeit
def retrieve_metadata_account(self):
"""
Retrieve the metadata of the account
"""
try:
self.success = True
account_info = self.swift_client.head_account()
self.logger.warning(
"Getting Account Head succeeded: %s", account_info)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Retrieving Account Head failed %s", sys.exc_info()[1])
@monitoring.timeit
def retrieve_metadata_container(self):
"""
Retrieve the metadata of the container
"""
try:
self.success = True
container_info = self.swift_client.head_container(self.container)
self.logger.warning(
"Getting Container Head succeeded: %s", container_info)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Getting Container Head failed: %s", sys.exc_info()[1])
@monitoring.timeit
def update_metadata_account(self):
"""
Update the metadata of the account
"""
try:
self.success = True
headers = {"X-Account-Meta-Temp-User": "testuser"}
self.swift_client.post_account(headers)
self.logger.warning(
"Posting Metadata to Account succeeded: %s", headers)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Posting Metadata to Account failed %s", sys.exc_info()[1])
@monitoring.timeit
def update_metadata_container(self):
"""
Update the metadata of the container
示例4: SwiftBackend
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
class SwiftBackend(duplicity.backend.Backend):
"""
Backend for Swift
"""
def __init__(self, parsed_url):
try:
from swiftclient import Connection
from swiftclient import ClientException
except ImportError:
raise BackendException("This backend requires "
"the python-swiftclient library.")
self.resp_exc = ClientException
conn_kwargs = {}
# if the user has already authenticated
if 'SWIFT_PREAUTHURL' in os.environ and 'SWIFT_PREAUTHTOKEN' in os.environ:
conn_kwargs['preauthurl'] = os.environ['SWIFT_PREAUTHURL']
conn_kwargs['preauthtoken'] = os.environ['SWIFT_PREAUTHTOKEN']
else:
if 'SWIFT_USERNAME' not in os.environ:
raise BackendException('SWIFT_USERNAME environment variable '
'not set.')
if 'SWIFT_PASSWORD' not in os.environ:
raise BackendException('SWIFT_PASSWORD environment variable '
'not set.')
if 'SWIFT_AUTHURL' not in os.environ:
raise BackendException('SWIFT_AUTHURL environment variable '
'not set.')
conn_kwargs['user'] = os.environ['SWIFT_USERNAME']
conn_kwargs['key'] = os.environ['SWIFT_PASSWORD']
conn_kwargs['authurl'] = os.environ['SWIFT_AUTHURL']
if 'SWIFT_AUTHVERSION' in os.environ:
conn_kwargs['auth_version'] = os.environ['SWIFT_AUTHVERSION']
else:
conn_kwargs['auth_version'] = '1'
if 'SWIFT_TENANTNAME' in os.environ:
conn_kwargs['tenant_name'] = os.environ['SWIFT_TENANTNAME']
if 'SWIFT_REGIONNAME' in os.environ:
conn_kwargs['os_options'] = {'region_name': os.environ['SWIFT_REGIONNAME']}
self.container = parsed_url.path.lstrip('/')
container_metadata = None
try:
self.conn = Connection(**conn_kwargs)
container_metadata = self.conn.head_container(self.container)
except ClientException:
pass
except Exception as e:
log.FatalError("Connection failed: %s %s"
% (e.__class__.__name__, str(e)),
log.ErrorCode.connection_failed)
if container_metadata is None:
log.Info("Creating container %s" % self.container)
try:
self.conn.put_container(self.container)
except Exception as e:
log.FatalError("Container creation failed: %s %s"
% (e.__class__.__name__, str(e)),
log.ErrorCode.connection_failed)
def _error_code(self, operation, e):
if isinstance(e, self.resp_exc):
if e.http_status == 404:
return log.ErrorCode.backend_not_found
def _put(self, source_path, remote_filename):
self.conn.put_object(self.container, remote_filename,
file(source_path.name))
def _get(self, remote_filename, local_path):
headers, body = self.conn.get_object(self.container, remote_filename)
with open(local_path.name, 'wb') as f:
for chunk in body:
f.write(chunk)
def _list(self):
headers, objs = self.conn.get_container(self.container, full_listing=True)
return [o['name'] for o in objs]
def _delete(self, filename):
self.conn.delete_object(self.container, filename)
def _query(self, filename):
sobject = self.conn.head_object(self.container, filename)
return {'size': int(sobject['content-length'])}
示例5: PCABackend
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
class PCABackend(duplicity.backend.Backend):
"""
Backend for OVH PCA
"""
def __init__(self, parsed_url):
duplicity.backend.Backend.__init__(self, parsed_url)
try:
from swiftclient import Connection
from swiftclient import ClientException
except ImportError as e:
raise BackendException("""\
PCA backend requires the python-swiftclient library.
Exception: %s""" % str(e))
self.resp_exc = ClientException
self.conn_cls = Connection
conn_kwargs = {}
# if the user has already authenticated
if 'PCA_PREAUTHURL' in os.environ and 'PCA_PREAUTHTOKEN' in os.environ:
conn_kwargs['preauthurl'] = os.environ['PCA_PREAUTHURL']
conn_kwargs['preauthtoken'] = os.environ['PCA_PREAUTHTOKEN']
else:
if 'PCA_USERNAME' not in os.environ:
raise BackendException('PCA_USERNAME environment variable '
'not set.')
if 'PCA_PASSWORD' not in os.environ:
raise BackendException('PCA_PASSWORD environment variable '
'not set.')
if 'PCA_AUTHURL' not in os.environ:
raise BackendException('PCA_AUTHURL environment variable '
'not set.')
conn_kwargs['user'] = os.environ['PCA_USERNAME']
conn_kwargs['key'] = os.environ['PCA_PASSWORD']
conn_kwargs['authurl'] = os.environ['PCA_AUTHURL']
os_options = {}
if 'PCA_AUTHVERSION' in os.environ:
conn_kwargs['auth_version'] = os.environ['PCA_AUTHVERSION']
if os.environ['PCA_AUTHVERSION'] == '3':
if 'PCA_USER_DOMAIN_NAME' in os.environ:
os_options.update({'user_domain_name': os.environ['PCA_USER_DOMAIN_NAME']})
if 'PCA_USER_DOMAIN_ID' in os.environ:
os_options.update({'user_domain_id': os.environ['PCA_USER_DOMAIN_ID']})
if 'PCA_PROJECT_DOMAIN_NAME' in os.environ:
os_options.update({'project_domain_name': os.environ['PCA_PROJECT_DOMAIN_NAME']})
if 'PCA_PROJECT_DOMAIN_ID' in os.environ:
os_options.update({'project_domain_id': os.environ['PCA_PROJECT_DOMAIN_ID']})
if 'PCA_TENANTNAME' in os.environ:
os_options.update({'tenant_name': os.environ['PCA_TENANTNAME']})
if 'PCA_ENDPOINT_TYPE' in os.environ:
os_options.update({'endpoint_type': os.environ['PCA_ENDPOINT_TYPE']})
if 'PCA_USERID' in os.environ:
os_options.update({'user_id': os.environ['PCA_USERID']})
if 'PCA_TENANTID' in os.environ:
os_options.update({'tenant_id': os.environ['PCA_TENANTID']})
if 'PCA_REGIONNAME' in os.environ:
os_options.update({'region_name': os.environ['PCA_REGIONNAME']})
else:
conn_kwargs['auth_version'] = '2'
if 'PCA_TENANTNAME' in os.environ:
conn_kwargs['tenant_name'] = os.environ['PCA_TENANTNAME']
if 'PCA_REGIONNAME' in os.environ:
os_options.update({'region_name': os.environ['PCA_REGIONNAME']})
conn_kwargs['os_options'] = os_options
conn_kwargs['retries'] = 0
self.conn_kwargs = conn_kwargs
# This folds the null prefix and all null parts, which means that:
# //MyContainer/ and //MyContainer are equivalent.
# //MyContainer//My/Prefix/ and //MyContainer/My/Prefix are equivalent.
url_parts = [x for x in parsed_url.path.split('/') if x != '']
self.container = url_parts.pop(0)
if url_parts:
self.prefix = '%s/' % '/'.join(url_parts)
else:
self.prefix = ''
policy = 'PCA'
policy_header = 'X-Storage-Policy'
container_metadata = None
try:
self.conn = Connection(**self.conn_kwargs)
container_metadata = self.conn.head_container(self.container)
except ClientException:
pass
except Exception as e:
log.FatalError("Connection failed: %s %s"
% (e.__class__.__name__, str(e)),
#.........这里部分代码省略.........
示例6: OptionParser
# 需要导入模块: from swiftclient import Connection [as 别名]
# 或者: from swiftclient.Connection import head_container [as 别名]
del log_config['swift_user']
return log_config
if __name__ == '__main__':
parser = OptionParser(version='%prog 1.0', usage='Usage: %prog -c CONFIG_FILE')
parser.add_option('-c', '--config', dest='config', default='/etc/swilog/swilog.conf', help='Configuration file')
(options, args) = parser.parse_args(sys.argv[1:])
(config, logfiles) = read_configuration(options.config)
# Star the clock here
start_time = time.time()
logger.warn('Initiating log processing.')
conn = Connection(config['swift_auth'], config['swift_user'], config['swift_password'])
cont_name = config['container']
try:
container = conn.head_container(cont_name)
except ClientException as e:
if config['create_container']:
logger.warn('No container by that name - Creating %s.' % cont_name)
container = conn.put_container(cont_name)
else:
logger.fatal('Container %s does not exist. Exiting.' % cont_name)
sys.exit(-1)
# Loop through each declared log file and process
for logfile in logfiles:
log_conf = build_log_config(config, logfile)
processor = LogProcessor(logger, log_conf)
processor.process(conn)
elapsed_time = time.time() - start_time