本文整理匯總了Python中swiftclient.Connection.put_object方法的典型用法代碼示例。如果您正苦於以下問題:Python Connection.put_object方法的具體用法?Python Connection.put_object怎麽用?Python Connection.put_object使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類swiftclient.Connection
的用法示例。
在下文中一共展示了Connection.put_object方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _upload_swift
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
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()
示例2: swift_upload
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
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
示例3: SwiftStorage
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [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))
示例4: __init__
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
#.........這裏部分代碼省略.........
if self.enabled_container is not None:
self.container = self.cdn_containers[self.enabled_container]
else:
raise cdn_exceptions.ClientException("CDN container not found")
self.logger.warning(
"The following CDN-enabled container has been selected %s",
self.container)
except cdn_exceptions.ClientException:
self.success, self.overall_success = False, False
self.failure = "Not found"
self.logger.error("CDN container not found")
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Selecting a CDN container failed %s", sys.exc_info()[1])
@monitoring.timeit
def upload_object(self):
"""
Create and upload a small object to the container
"""
try:
self.success = True
self.object = 'cdncheckob' + str(time.time())
contents = 'yeah this is a pretty small object'
self.swift_client.put_object(
self.container['name'], self.object, contents)
self.logger.warning("Created Object %s", self.object)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Uploading object to a CDN container failed %s",
sys.exc_info()[1])
@monitoring.timeit
def check_http_url(self):
"""
Access the object we created using the CDN http url
"""
try:
self.success = True
uri = self.container['x-cdn-uri'] + '/' + self.object
self.logger.warning("HTTP URL: %s", uri)
for x in range(1, 51):
sleep(10)
http_client = httplib2.Http(
timeout=9, disable_ssl_certificate_validation=True)
try:
response, content = http_client.request(uri, "GET")
except:
示例5: __init__
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
#.........這裏部分代碼省略.........
self.failure = e
self.logger.error(
"Listing objects failed %s", sys.exc_info()[1])
@monitoring.timeit
def create_container(self):
"""
Create a new container
"""
try:
self.success = True
self.container = 'swiftcheck' + str(time.time())
self.swift_client.put_container(self.container)
self.logger.warning("Created Container %s", self.container)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Create Container failed %s", sys.exc_info()[1])
@monitoring.timeit
def create_object(self):
"""
Create a new object in the newly created container
"""
try:
self.success = True
self.object = 'swiftcheckob' + str(time.time())
contents = 'yeah this is a pretty small object'
self.swift_client.put_object(self.container, self.object, contents)
self.logger.warning("Created Object %s", self.object)
except Exception as e:
self.success, self.overall_success = False, False
self.failure = e
self.logger.error(
"Create Object Failed %s", sys.exc_info()[1])
@monitoring.timeit
def get_container(self):
"""
Select the newly created container
"""
try:
self.success = True
self.containers = self.swift_client.get_account()[1]
self.flag = 0
for container in self.containers:
if container['name'] == self.container:
self.logger.warning(
"Getting Container Succeeded: %s", container['name'])
self.flag = self.flag + 1
if self.flag == 0:
raise cdn_exceptions.ClientException("Container not found")
except cdn_exceptions.ClientException:
self.success, self.overall_success = False, False
self.failure = "Not found"
self.logger.error("Container not found")
示例6: __init__
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
class Swift:
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())
def list_containers(self):
try:
self.containers = self.swift_client.get_account()[1]
self.logger.warning("Listing Containers")
for container in self.containers:
self.logger.warning(container['name'])
except Exception:
self.logger.error("Listing Containers Failed")
self.exit(1)
def list_objects(self):
try:
self.logger.warning("Listing Objects")
for container in self.containers:
for swift_container in self.swift_client.get_container(container['name'])[1]:
self.logger.warning(container['name'] + " " + swift_container['name'])
except Exception as e:
self.logger.error('Listing Objects Failed, Got exception: %s' % e)
self.exit(1)
def create_container(self, name):
try:
self.container = name
self.swift_client.put_container(self.container)
self.logger.warning("Created Container %s", self.container)
except Exception:
self.logger.error("Creating Container Failed")
self.exit(1)
def create_object(self, name, contents):
try:
self.object = name
self.swift_client.put_object(self.container, self.object, contents)
self.logger.warning("Created Object %s", self.object)
except Exception:
self.logger.error("Creating Object Failed")
self.exit(1)
def get_container(self):
try:
self.containers = self.swift_client.get_account()[1]
for container in self.containers:
if container['name'] == self.container:
self.logger.warning("Getting Container Succeeded")
return True
self.logger.error("Getting Container Failed")
self.exit(1)
except Exception as e:
self.logger.error('Getting Container Failed: Got exception: ' % e)
self.exit(1)
def get_object(self):
try:
object = self.swift_client.get_object(self.container, self.object)
self.logger.warning("Object Get: %s", object)
except Exception as e:
self.logger.error('Object Get Failed, Got exception: %s' % e)
self.exit(1)
def delete_object(self):
try:
self.swift_client.delete_object(self.container, self.object)
self.logger.warning("Object Deleted")
except Exception as e:
self.logger.error('Object Deletion Failed: Got exception: ' % e)
def delete_container(self):
try:
self.swift_client.delete_container(self.container)
self.logger.warning("Container Deleted")
#.........這裏部分代碼省略.........
示例7: SwiftBackend
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [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'])}
示例8: PCABackend
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
#.........這裏部分代碼省略.........
# 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)),
log.ErrorCode.connection_failed)
if container_metadata is None:
log.Info("Creating container %s" % self.container)
try:
headers = dict([[policy_header, policy]])
self.conn.put_container(self.container, headers=headers)
except Exception as e:
log.FatalError("Container creation failed: %s %s"
% (e.__class__.__name__, str(e)),
log.ErrorCode.connection_failed)
elif policy and container_metadata[policy_header.lower()] != policy:
log.FatalError("Container '%s' exists but its storage policy is '%s' not '%s'."
% (self.container, container_metadata[policy_header.lower()], policy))
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, self.prefix + remote_filename,
file(source_path.name))
def _get(self, remote_filename, local_path):
body = self.preprocess_download(remote_filename, 60)
if body:
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, path=self.prefix)
# removes prefix from return values. should check for the prefix ?
return [o['name'][len(self.prefix):] for o in objs]
def _delete(self, filename):
self.conn.delete_object(self.container, self.prefix + filename)
def _query(self, filename):
sobject = self.conn.head_object(self.container, self.prefix + filename)
return {'size': int(sobject['content-length'])}
def preprocess_download(self, remote_filename, retry_period, wait=True):
body = self.unseal(remote_filename)
try:
if wait:
while not body:
time.sleep(retry_period)
self.conn = self.conn_cls(**self.conn_kwargs)
body = self.unseal(remote_filename)
self.conn.close()
except Exception as e:
log.FatalError("Connection failed: %s %s" % (e.__class__.__name__, str(e)),
log.ErrorCode.connection_failed)
return body
def unseal(self, remote_filename):
try:
_, body = self.conn.get_object(self.container, self.prefix + remote_filename,
resp_chunk_size=1024)
log.Info("File %s was successfully unsealed." % remote_filename)
return body
except self.resp_exc as e:
# The object is sealed but being released.
if e.http_status == 429:
# The retry-after header contains the remaining duration before
# the unsealing operation completes.
duration = int(e.http_response_headers['Retry-After'])
m, s = divmod(duration, 60)
h, m = divmod(m, 60)
eta = "%dh%02dm%02ds" % (h, m, s)
log.Info("File %s is being unsealed, operation ETA is %s." %
(remote_filename, eta))
else:
raise
示例9: get_access_token
# 需要導入模塊: from swiftclient import Connection [as 別名]
# 或者: from swiftclient.Connection import put_object [as 別名]
access_token = get_access_token(
client_id=client_id,
client_secret=client_secret,
refresh_token=refresh_token,
)
credentials = get_open_stack_credentials(access_token=access_token)
except Exception, e:
lastExc = e
if lastExc is None:
break
time.sleep(5)
if lastExc is not None: raise lastExc
conn_kwargs = {}
if ('endpoint' in credentials) and ('token' in credentials):
conn_kwargs['preauthurl'] = credentials['endpoint']
conn_kwargs['preauthtoken'] = credentials['token']
else:
print "Asplod"
conn_kwargs['auth_version'] = '1'
conn = Connection(**conn_kwargs)
return conn
if len(sys.argv) < 6:
print "Wrong args. Use: client_id client_secret refresh_token out_path file"
conn = get_credentials(sys.argv[1], sys.argv[2], sys.argv[3])
conn.put_object(None, sys.argv[4] + os.path.basename(sys.argv[5]), open(sys.argv[5], "rb"))