本文整理汇总了Python中swiftclient.Connection类的典型用法代码示例。如果您正苦于以下问题:Python Connection类的具体用法?Python Connection怎么用?Python Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SwiftBackend
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: get_session
def get_session():
SWIFT_USER = get_reg_value('HKLM', APP_STORAGE_REG_KEY, 'account')
SWIFT_PASS = get_reg_value('HKLM', APP_STORAGE_REG_KEY, 'password')
SWIFT_KEY = get_reg_value('HKLM', APP_STORAGE_REG_KEY, 'key')
SWIFT_AUTH_URL = get_reg_value('HKLM', APP_STORAGE_REG_KEY, 'auth_url')
cnx = Connection(SWIFT_AUTH_URL, '%s:%s' % (SWIFT_USER,SWIFT_PASS), SWIFT_KEY)
cnx.get_auth()
return cnx
示例3: SwiftStorage
class SwiftStorage(Storage):
def __init__(self, config):
self._config = config
self._root_path = self._config.storage_path
self._connection = Connection(self._config.storage_auth_url, self._config.storage_account + ':' + self._config.storage_username, self._config.storage_password)
self._container = self._config.storage_container
self._index_lastupdate = None
def _update_index(self):
logger.info("_update_index")
try:
headers = self._connection.head_object(self._container, 'docker-registry.index')
lastupdatestr = headers['last-modified']
if not lastupdatestr:
return
lastupdate = datetime.strptime(lastupdatestr, "%a, %d %b %Y %H:%M:%S %Z")
logger.info('lastupdate: ' + str(lastupdate))
refresh_index = False
if not self._index_lastupdate:
refresh_index = True
elif self._index_lastupdate < lastupdate:
refresh_index = True
if not refresh_index:
return
logger.info("refresh index...")
(headers, data) = self._connection.get_object(self._container, 'docker-registry.index')
(level, temppath) = mkstemp()
tempfile = open(temppath, 'w')
tempfile.write(data)
tempfile.close()
with open(temppath, 'r') as f:
for line in f:
line = line.strip()
if line.startswith("#"):
continue
tokens = line.split('\t')
if len(tokens) < 2:
continue
logger.info("Index: " + str(tokens))
realpath = os.path.join(self._root_path, tokens[0])
if not os.path.exists(realpath):
os.makedirs(realpath)
metafile = open(os.path.join(realpath, '.object'), 'w')
metafile.write(tokens[1])
metafile.close()
os.remove(temppath)
self._index_lastupdate = lastupdate
except ClientException, inst:
if inst.http_status != 404:
raise inst
示例4: SwiftBackend
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 os.environ.has_key('SWIFT_PREAUTHURL') and os.environ.has_key('SWIFT_PREAUTHTOKEN'):
conn_kwargs['preauthurl'] = os.environ['SWIFT_PREAUTHURL']
conn_kwargs['preauthtoken'] = os.environ['SWIFT_PREAUTHTOKEN']
else:
if not os.environ.has_key('SWIFT_USERNAME'):
raise BackendException('SWIFT_USERNAME environment variable '
'not set.')
if not os.environ.has_key('SWIFT_PASSWORD'):
raise BackendException('SWIFT_PASSWORD environment variable '
'not set.')
if not os.environ.has_key('SWIFT_AUTHURL'):
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 os.environ.has_key('SWIFT_AUTHVERSION'):
conn_kwargs['auth_version'] = os.environ['SWIFT_AUTHVERSION']
else:
conn_kwargs['auth_version'] = '1'
if os.environ.has_key('SWIFT_TENANTNAME'):
conn_kwargs['tenant_name'] = os.environ['SWIFT_TENANTNAME']
self.container = parsed_url.path.lstrip('/')
try:
self.conn = Connection(**conn_kwargs)
self.conn.put_container(self.container)
except Exception, e:
log.FatalError("Connection failed: %s %s"
% (e.__class__.__name__, str(e)),
log.ErrorCode.connection_failed)
示例5: SwiftBackend
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 os.environ.has_key("SWIFT_PREAUTHURL") and os.environ.has_key("SWIFT_PREAUTHTOKEN"):
conn_kwargs["preauthurl"] = os.environ["SWIFT_PREAUTHURL"]
conn_kwargs["preauthtoken"] = os.environ["SWIFT_PREAUTHTOKEN"]
else:
if not os.environ.has_key("SWIFT_USERNAME"):
raise BackendException("SWIFT_USERNAME environment variable " "not set.")
if not os.environ.has_key("SWIFT_PASSWORD"):
raise BackendException("SWIFT_PASSWORD environment variable " "not set.")
if not os.environ.has_key("SWIFT_AUTHURL"):
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 os.environ.has_key("SWIFT_AUTHVERSION"):
conn_kwargs["auth_version"] = os.environ["SWIFT_AUTHVERSION"]
else:
conn_kwargs["auth_version"] = "1"
if os.environ.has_key("SWIFT_TENANTNAME"):
conn_kwargs["tenant_name"] = os.environ["SWIFT_TENANTNAME"]
self.container = parsed_url.path.lstrip("/")
try:
self.conn = Connection(**conn_kwargs)
self.conn.put_container(self.container)
except Exception, e:
log.FatalError("Connection failed: %s %s" % (e.__class__.__name__, str(e)), log.ErrorCode.connection_failed)
示例6: __init__
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))
示例7: __init__
def __init__(self, logger, exec_time, **kwargs):
self.keystone_client = keystone_client.Client(
username=kwargs['os_username'],
password=kwargs['os_password'],
tenant_name=kwargs['os_tenant_name'],
auth_url=kwargs['os_auth_url'])
self.token = self.keystone_client.auth_ref['token']['id']
self.tenant_id = self.keystone_client.auth_ref['token']['tenant']['id']
self.end_points = self.keystone_client.service_catalog.get_endpoints()
self.region1 = str(self.end_points['hpext:cdn'][0]['region'])
self.region2 = str(self.end_points['hpext:cdn'][1]['region'])
self.url1 = str(self.end_points['hpext:cdn'][0]['versionList'])
self.url1 = self.url1[:-1]
self.url2 = str(self.end_points['hpext:cdn'][1]['versionList'])
self.url2 = self.url2[:-1]
self.END_POINTS = {
self.region1: self.url1,
self.region2: self.url2
}
self.cdn_client = Connection(
user=kwargs['os_username'],
key=kwargs['os_password'],
tenant_name=kwargs['os_tenant_name'],
authurl=kwargs['os_auth_url'],
auth_version="2.0",
os_options={'region_name': kwargs['os_region'],
'service_type': 'hpext:cdn'})
self.swift_client = Connection(
user=kwargs['os_username'],
key=kwargs['os_password'],
tenant_name=kwargs['os_tenant_name'],
authurl=kwargs['os_auth_url'],
auth_version="2.0",
os_options={'region_name': kwargs['os_region']})
self.service = 'cdn'
self.authurl = kwargs['os_auth_url']
self.logger = logger
self.exec_time = exec_time
self.zone = kwargs['os_zone']
self.failure = None
self.overall_success = True
self.region = kwargs['os_region']
self.tenant_name = kwargs['os_tenant_name']
示例8: _upload_swift
def _upload_swift(self):
tag = str(int(time.time()))
(tenant, user) = self.cfg["s3user"].split(':')
auth_url = "http://" + self.cfg["s3host"] + "/v2.0/"
conn = Connection(auth_url, user, self.cfg["s3pass"],
snet=False, tenant_name=tenant, auth_version="2")
# Not calling conn.put_container() for the same reason of permissions.
key_name = self.cfg["prefix"] + '/' + "i" + tag
mimetype = "image/jpeg"
headers = { "Content-Type": mimetype }
fp = open(self.cfg["file"], 'rb')
conn.put_object(self.cfg["bucket"], key_name, fp, headers=headers)
fp.close()
示例9: swift_upload
def swift_upload(container, path, auth, user, key):
conn = SwiftConnection(auth, user, key, snet=False, insecure=True)
put_headers = { 'x-object-meta-mtime': "%f" % getmtime(path) }
retry = SWIFT_RETRY_TIMES
while retry > 0:
try:
with open(path, 'rb') as fd:
conn.put_object(container, path, fd,
content_length=getsize(path), headers=put_headers)
return True
except ClientException:
log_normal(logger, {
'action': 'upload-error',
'error': 'swift client exception'
}, LOG_ERROR)
conn.put_container(container, headers={})
retry -= 1
return False
示例10: SwiftStorage
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))
示例11: __init__
def __init__(self, auth_url, user_name, key, logger, cache_file_path,
object_store_url=None, auth_version="2",
os_options=None):
socket.setdefaulttimeout(30.0) # timeout set at socket level
Connection.__init__(self, auth_url, user_name, key, retries=0,
os_options=os_options,
auth_version=auth_version)
# needed if keystone-get-token does not work
self.object_store_url = object_store_url
self.state = dict()
for sev in severities:
for component_name in self.component_names():
self.state[component_name + sev] = \
dict(current_state=component_states.unknown,
reason='',
metrics={})
self.latency = dict()
self.metric_data = []
self.latency_reset()
self.logger = logger
self.cache_file_path = cache_file_path
示例12: __init__
def __init__(self, username, password, tenant, auth_url, region):
self.swift_client = Connection(
user=username,
key=password,
tenant_name=tenant,
authurl=auth_url,
auth_version="2.0",
os_options={'region_name': region}
)
# Setup logging
self.logger = logging.getLogger()
self.logger.setLevel(logging.WARNING)
# Allow the logger to write all output to stdout too
self.logger.addHandler(logging.StreamHandler())
示例13: __init__
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"],
insecure=True)
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"])
示例14: __init__
def __init__(self, logger, exec_time, **kwargs):
self.swift_client = Connection(
user=kwargs['os_username'],
key=kwargs['os_password'],
tenant_name=kwargs['os_tenant_name'],
authurl=kwargs['os_auth_url'],
auth_version="2.0",
os_options={'region_name': kwargs['os_region']}
)
self.service = 'Object Storage'
self.logger = logger
self.exec_time = exec_time
self.zone = kwargs['os_zone']
self.region = kwargs['os_region']
self.failure = None
self.overall_success = True
self.tenant_name = kwargs['os_tenant_name']
示例15: __init__
def __init__(self, conf):
'''
根据配置文件初始化连接Swift云存储的客户端,包括认证系统和存储系统。
'''
self.conf = conf
colon = conf['storuser'].find(":")
if colon > 0 :
self.user = conf['storuser'][:colon]
self.tenant = conf['storuser'][colon+1:]
else:
self.user = self.tenant = conf['storuser']
print self.user, self.tenant
self.pawd = conf['storpass']
self.authurl = "http://" + conf['storip'] + ":" + conf['storport'] + "/v2.0/"
# self.keystone = client.Client(username="admin", password="admin",
# tenant_name="admin", auth_url=self.authurl)
self.swift = Connection(authurl=self.authurl, user=self.user, key=self.pawd,
auth_version="2", tenant_name=self.tenant, insecure = True)