本文整理匯總了Python中swiftclient.Connection方法的典型用法代碼示例。如果您正苦於以下問題:Python swiftclient.Connection方法的具體用法?Python swiftclient.Connection怎麽用?Python swiftclient.Connection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類swiftclient
的用法示例。
在下文中一共展示了swiftclient.Connection方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: init_swift
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def init_swift(self, name, config):
"""
Hack needed because Flask FS backend supports only swift 1.0 authentication.
"""
import swiftclient
super(SwiftBackend, self).__init__(name, config)
version = "3"
if "2.0" in config.authurl:
version = "2.0"
self.conn = swiftclient.Connection(
user=config.user,
key=config.key,
authurl=config.authurl,
auth_version=version,
os_options={
"tenant_name": config.tenant_name,
"region_name": config.region_name,
},
)
self.conn.put_container(self.name)
示例2: get_manager
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def get_manager(self, store_location, context=None, allow_reauth=False):
"""Return appropriate connection manager for store
The method detects store type (singletenant or multitenant) and returns
appropriate connection manager (singletenant or multitenant) that
allows to request swiftclient connections.
:param store_location: StoreLocation object that define image location
:param context: user context
:param allow_reauth: defines if we allow re-authentication when user
token is expired and refresh swift connection
:return: connection manager for store
"""
msg = _("There is no Connection Manager implemented for %s class.")
raise NotImplementedError(msg % self.__class__.__name__)
示例3: setup
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def setup(self):
self.conn = swiftclient.Connection(
user=USER,
key=KEY,
authurl=AUTHURL,
)
self.container = 'test'
self.config = Config({
'user': USER,
'key': KEY,
'authurl': AUTHURL,
})
self.backend = SwiftBackend(self.container, self.config)
yield
try:
headers, items = self.conn.get_container(self.backend.name)
for i in items:
self.conn.delete_object(self.backend.name, i['name'])
self.conn.delete_container(self.backend.name)
except swiftclient.ClientException as e:
assert False, "Failed to delete container ->" + str(e)
示例4: test_integration_fileresource_download_success
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def test_integration_fileresource_download_success(self):
# initiate a Swift service connection
conn = swiftclient.Connection(
user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL,
)
# upload file to Swift storage
with io.StringIO("test file") as file1:
conn.put_object(settings.SWIFT_CONTAINER_NAME, '/tests/file1.txt',
contents=file1.read(),
content_type='text/plain')
self.client.login(username=self.username, password=self.password)
response = self.client.get(self.download_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.content, 'utf-8'), "test file")
# delete file from Swift storage
conn.delete_object(settings.SWIFT_CONTAINER_NAME, '/tests/file1.txt')
示例5: test_integration_uploadedfile_create_success
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def test_integration_uploadedfile_create_success(self):
# POST request using multipart/form-data to be able to upload file
self.client.login(username=self.username, password=self.password)
upload_path = "{}/uploads/file2.txt".format(self.username)
with io.StringIO("test file") as f:
post = {"fname": f, "upload_path": upload_path}
response = self.client.post(self.create_read_url, data=post)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
# initiate a Swift service connection
conn = swiftclient.Connection(
user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL,
)
# delete file from Swift storage
conn.delete_object(settings.SWIFT_CONTAINER_NAME, upload_path)
示例6: test_integration_uploadedfileresource_download_success
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def test_integration_uploadedfileresource_download_success(self):
# initiate a Swift service connection
conn = swiftclient.Connection(
user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL,
)
# upload file to Swift storage
upload_path = "{}/uploads/file1.txt".format(self.username)
with io.StringIO("test file") as file1:
conn.put_object(settings.SWIFT_CONTAINER_NAME, upload_path,
contents=file1.read(),
content_type='text/plain')
self.client.login(username=self.username, password=self.password)
response = self.client.get(self.download_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.content, 'utf-8'), "test file")
# delete file from Swift storage
conn.delete_object(settings.SWIFT_CONTAINER_NAME, upload_path)
示例7: validate_path
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def validate_path(self, path):
"""
Overriden to check whether the provided path is under SERVICES/PACS/ path.
"""
path = path.strip(' ').strip('/')
if not path.startswith('SERVICES/PACS/'):
raise serializers.ValidationError(
["File path must start with 'SERVICES/PACS/'."])
# verify that the file is indeed already in Swift
conn = swiftclient.Connection(user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL)
object_list = conn.get_container(settings.SWIFT_CONTAINER_NAME, prefix=path)[1]
if not object_list:
raise serializers.ValidationError(["Could not find this path."])
return path
示例8: test_integration_servicefileresource_download_success
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def test_integration_servicefileresource_download_success(self):
# initiate a Swift service connection
conn = swiftclient.Connection(
user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL,
)
# create container in case it doesn't already exist
conn.put_container(settings.SWIFT_CONTAINER_NAME
)
# upload file to Swift storage
with io.StringIO("test file") as file1:
conn.put_object(settings.SWIFT_CONTAINER_NAME, self.path,
contents=file1.read(),
content_type='text/plain')
self.client.login(username=self.username, password=self.password)
response = self.client.get(self.download_url)
self.assertEqual(response.status_code, 200)
self.assertEqual(str(response.content, 'utf-8'), "test file")
# delete file from Swift storage
conn.delete_object(settings.SWIFT_CONTAINER_NAME, self.path)
示例9: _get_swift_connection
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def _get_swift_connection(instance):
"""
Get Connection object.
"""
return swiftclient.Connection(
user=instance.swift_openstack_user,
key=instance.swift_openstack_password,
authurl=instance.swift_openstack_auth_url,
tenant_name=instance.swift_openstack_tenant,
auth_version='3',
os_options={'region_name': instance.swift_openstack_region}
)
示例10: _create_container_if_missing
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def _create_container_if_missing(self, container, connection):
"""
Creates a missing container in Swift if the
``swift_store_create_container_on_put`` option is set.
:param container: Name of container to create
:param connection: Connection to swift service
"""
if self.backend_group:
store_conf = getattr(self.conf, self.backend_group)
else:
store_conf = self.conf.glance_store
try:
connection.head_container(container)
except swiftclient.ClientException as e:
if e.http_status == http_client.NOT_FOUND:
if store_conf.swift_store_create_container_on_put:
try:
msg = (_LI("Creating swift container %(container)s") %
{'container': container})
LOG.info(msg)
connection.put_container(container)
except swiftclient.ClientException as e:
msg = (_("Failed to add container to Swift.\n"
"Got error from Swift: %s.")
% encodeutils.exception_to_unicode(e))
raise glance_store.BackendException(msg)
else:
msg = (_("The container %(container)s does not exist in "
"Swift. Please set the "
"swift_store_create_container_on_put option "
"to add container to Swift automatically.") %
{'container': container})
raise glance_store.BackendException(msg)
else:
raise
示例11: get_store_connection
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def get_store_connection(self, auth_token, storage_url):
"""Get initialized swift connection
:param auth_token: auth token
:param storage_url: swift storage url
:return: swiftclient connection that allows to request container and
others
"""
# initialize a connection
return swiftclient.Connection(
preauthurl=storage_url,
preauthtoken=auth_token,
insecure=self.insecure,
ssl_compression=self.ssl_compression,
cacert=self.cacert)
示例12: get_connection
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def get_connection(self, location, context=None):
if not location.user:
reason = _("Location is missing user:password information.")
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
auth_url = location.swift_url
if not auth_url.endswith('/'):
auth_url += '/'
if self.auth_version in ('2', '3'):
try:
tenant_name, user = location.user.split(':')
except ValueError:
reason = (_("Badly formed tenant:user '%(user)s' in "
"Swift URI") % {'user': location.user})
LOG.info(reason)
raise exceptions.BadStoreUri(message=reason)
else:
tenant_name = None
user = location.user
os_options = {}
if self.region:
os_options['region_name'] = self.region
os_options['endpoint_type'] = self.endpoint_type
os_options['service_type'] = self.service_type
if self.user_domain_id:
os_options['user_domain_id'] = self.user_domain_id
if self.user_domain_name:
os_options['user_domain_name'] = self.user_domain_name
if self.project_domain_id:
os_options['project_domain_id'] = self.project_domain_id
if self.project_domain_name:
os_options['project_domain_name'] = self.project_domain_name
return swiftclient.Connection(
auth_url, user, location.key, preauthurl=self.conf_endpoint,
insecure=self.insecure, tenant_name=tenant_name,
auth_version=self.auth_version, os_options=os_options,
ssl_compression=self.ssl_compression, cacert=self.cacert)
示例13: __init__
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def __init__(self, auth_url, tenant, container, user, password):
self.swift_client = swiftclient.Connection(
authurl=auth_url, user=":".join([user, tenant]), key=password
)
self._container = container
self.swift_client.head_container(container)
示例14: __init__
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def __init__(self, name, config):
super(SwiftBackend, self).__init__(name, config)
self.conn = swiftclient.Connection(
user=config.user,
key=config.key,
authurl=config.authurl
)
self.conn.put_container(self.name)
示例15: connect
# 需要導入模塊: import swiftclient [as 別名]
# 或者: from swiftclient import Connection [as 別名]
def connect(*args, **kwargs):
"""
Connect to swift storage and return the connection object,
as well an optional "prepend" string to fully qualify
object location in swift storage.
"""
b_status = True
b_prependBucketPath = False
for k, v in kwargs.items():
if k == 'prependBucketPath': b_prependBucketPath = v
d_ret = {
'status': b_status,
'conn': None,
'prependBucketPath': ""
}
# initiate a swift service connection, based on internal
# settings already available in the django variable space.
try:
d_ret['conn'] = swiftclient.Connection(
user=settings.SWIFT_USERNAME,
key=settings.SWIFT_KEY,
authurl=settings.SWIFT_AUTH_URL,
)
except:
d_ret['status'] = False
if b_prependBucketPath:
d_ret['prependBucketPath'] = ''
return d_ret