本文整理汇总了Python中swift.common.internal_client.InternalClient.delete_object方法的典型用法代码示例。如果您正苦于以下问题:Python InternalClient.delete_object方法的具体用法?Python InternalClient.delete_object怎么用?Python InternalClient.delete_object使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类swift.common.internal_client.InternalClient
的用法示例。
在下文中一共展示了InternalClient.delete_object方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ObjectExpirer
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
class ObjectExpirer(Daemon):
"""
Daemon that queries the internal hidden expiring_objects_account to
discover objects that need to be deleted.
:param conf: The daemon configuration.
"""
def __init__(self, conf):
self.conf = conf
self.logger = get_logger(conf, log_route='object-expirer')
self.interval = int(conf.get('interval') or 300)
self.expiring_objects_account = \
(conf.get('auto_create_account_prefix') or '.') + \
'expiring_objects'
conf_path = conf.get('__file__') or '/etc/swift/object-expirer.conf'
request_tries = int(conf.get('request_tries') or 3)
self.swift = InternalClient(conf_path,
'Swift Object Expirer',
request_tries)
self.report_interval = int(conf.get('report_interval') or 300)
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
self.recon_cache_path = conf.get('recon_cache_path',
'/var/cache/swift')
self.rcache = join(self.recon_cache_path, 'object.recon')
def report(self, final=False):
"""
Emits a log line report of the progress so far, or the final progress
is final=True.
:param final: Set to True for the last report once the expiration pass
has completed.
"""
if final:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass completed in %ds; %d objects expired') %
(elapsed, self.report_objects))
dump_recon_cache({'object_expiration_pass': elapsed,
'expired_last_pass': self.report_objects},
self.rcache, self.logger)
elif time() - self.report_last_time >= self.report_interval:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass so far %ds; %d objects expired') %
(elapsed, self.report_objects))
self.report_last_time = time()
def run_once(self, *args, **kwargs):
"""
Executes a single pass, looking for objects to expire.
:param args: Extra args to fulfill the Daemon interface; this daemon
has no additional args.
:param kwargs: Extra keyword args to fulfill the Daemon interface; this
daemon has no additional keyword args.
"""
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
try:
self.logger.debug(_('Run begin'))
containers, objects = \
self.swift.get_account_info(self.expiring_objects_account)
self.logger.info(_('Pass beginning; %s possible containers; %s '
'possible objects') % (containers, objects))
for c in self.swift.iter_containers(self.expiring_objects_account):
container = c['name']
timestamp = int(container)
if timestamp > int(time()):
break
for o in self.swift.iter_objects(self.expiring_objects_account,
container):
obj = o['name']
timestamp, actual_obj = obj.split('-', 1)
timestamp = int(timestamp)
if timestamp > int(time()):
break
start_time = time()
try:
self.delete_actual_object(actual_obj, timestamp)
self.swift.delete_object(self.expiring_objects_account,
container, obj)
self.report_objects += 1
self.logger.increment('objects')
except (Exception, Timeout), err:
self.logger.increment('errors')
self.logger.exception(
_('Exception while deleting object %s %s %s') %
(container, obj, str(err)))
self.logger.timing_since('timing', start_time)
self.report()
try:
self.swift.delete_container(
self.expiring_objects_account,
container,
acceptable_statuses=(2, HTTP_NOT_FOUND, HTTP_CONFLICT))
except (Exception, Timeout), err:
self.logger.exception(
_('Exception while deleting container %s %s') %
(container, str(err)))
#.........这里部分代码省略.........
示例2: ContainerReconciler
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
class ContainerReconciler(Daemon):
"""
Move objects that are in the wrong storage policy.
"""
def __init__(self, conf):
self.conf = conf
self.reclaim_age = int(conf.get('reclaim_age', 86400 * 7))
self.interval = int(conf.get('interval', 30))
conf_path = conf.get('__file__') or \
'/etc/swift/container-reconciler.conf'
self.logger = get_logger(conf, log_route='container-reconciler')
request_tries = int(conf.get('request_tries') or 3)
self.swift = InternalClient(conf_path,
'Swift Container Reconciler',
request_tries)
self.stats = defaultdict(int)
self.last_stat_time = time.time()
def stats_log(self, metric, msg, *args, **kwargs):
"""
Update stats tracking for metric and emit log message.
"""
level = kwargs.pop('level', logging.DEBUG)
log_message = '%s: ' % metric + msg
self.logger.log(level, log_message, *args, **kwargs)
self.stats[metric] += 1
def log_stats(self, force=False):
"""
Dump stats to logger, noop when stats have been already been
logged in the last minute.
"""
now = time.time()
should_log = force or (now - self.last_stat_time > 60)
if should_log:
self.last_stat_time = now
self.logger.info('Reconciler Stats: %r', dict(**self.stats))
def pop_queue(self, container, obj, q_ts, q_record):
"""
Issue a delete object request to the container for the misplaced
object queue entry.
:param container: the misplaced objects container
:param q_ts: the timestamp of the misplaced object
:param q_record: the timestamp of the queue entry
N.B. q_ts will normally be the same time as q_record except when
an object was manually re-enqued.
"""
q_path = '/%s/%s/%s' % (MISPLACED_OBJECTS_ACCOUNT, container, obj)
x_timestamp = slightly_later_timestamp(max(q_record, q_ts))
self.stats_log('pop_queue', 'remove %r (%f) from the queue (%s)',
q_path, q_ts, x_timestamp)
headers = {'X-Timestamp': x_timestamp}
direct_delete_container_entry(
self.swift.container_ring, MISPLACED_OBJECTS_ACCOUNT,
container, obj, headers=headers)
def throw_tombstones(self, account, container, obj, timestamp,
policy_index, path):
"""
Issue a delete object request to the given storage_policy.
:param account: the account name
:param container: the container name
:param account: the object name
:param timestamp: the timestamp of the object to delete
:param policy_index: the policy index to direct the request
:param path: the path to be used for logging
"""
x_timestamp = slightly_later_timestamp(timestamp)
self.stats_log('cleanup_attempt', '%r (%f) from policy_index '
'%s (%s) will be deleted',
path, timestamp, policy_index, x_timestamp)
headers = {
'X-Timestamp': x_timestamp,
'X-Backend-Storage-Policy-Index': policy_index,
}
success = False
try:
self.swift.delete_object(account, container, obj,
acceptable_statuses=(2, 404),
headers=headers)
except UnexpectedResponse as err:
self.stats_log('cleanup_failed', '%r (%f) was not cleaned up '
'in storage_policy %s (%s)', path, timestamp,
policy_index, err)
else:
success = True
self.stats_log('cleanup_success', '%r (%f) was successfully '
'removed from policy_index %s', path, timestamp,
policy_index)
return success
def _reconcile_object(self, account, container, obj, q_policy_index, q_ts,
q_op, path, **kwargs):
"""
Perform object reconciliation.
#.........这里部分代码省略.........
示例3: ObjectExpirer
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
class ObjectExpirer(Daemon):
"""
Daemon that queries the internal hidden expiring_objects_account to
discover objects that need to be deleted.
:param conf: The daemon configuration.
"""
def __init__(self, conf):
self.conf = conf
self.logger = get_logger(conf, log_route='object-expirer')
self.interval = int(conf.get('interval') or 300)
self.expiring_objects_account = \
(conf.get('auto_create_account_prefix') or '.') + \
(conf.get('expiring_objects_account_name') or 'expiring_objects')
conf_path = conf.get('__file__') or '/etc/swift/object-expirer.conf'
request_tries = int(conf.get('request_tries') or 3)
self.swift = InternalClient(conf_path,
'Swift Object Expirer',
request_tries)
self.report_interval = int(conf.get('report_interval') or 300)
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
self.recon_cache_path = conf.get('recon_cache_path',
'/var/cache/swift')
self.rcache = join(self.recon_cache_path, 'object.recon')
self.concurrency = int(conf.get('concurrency', 1))
if self.concurrency < 1:
raise ValueError("concurrency must be set to at least 1")
self.processes = int(self.conf.get('processes', 0))
self.process = int(self.conf.get('process', 0))
def report(self, final=False):
"""
Emits a log line report of the progress so far, or the final progress
is final=True.
:param final: Set to True for the last report once the expiration pass
has completed.
"""
if final:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass completed in %ds; %d objects expired') %
(elapsed, self.report_objects))
dump_recon_cache({'object_expiration_pass': elapsed,
'expired_last_pass': self.report_objects},
self.rcache, self.logger)
elif time() - self.report_last_time >= self.report_interval:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass so far %ds; %d objects expired') %
(elapsed, self.report_objects))
self.report_last_time = time()
def run_once(self, *args, **kwargs):
"""
Executes a single pass, looking for objects to expire.
:param args: Extra args to fulfill the Daemon interface; this daemon
has no additional args.
:param kwargs: Extra keyword args to fulfill the Daemon interface; this
daemon accepts processes and process keyword args.
These will override the values from the config file if
provided.
"""
processes, process = self.get_process_values(kwargs)
pool = GreenPool(self.concurrency)
containers_to_delete = []
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
try:
self.logger.debug(_('Run begin'))
containers, objects = \
self.swift.get_account_info(self.expiring_objects_account)
self.logger.info(_('Pass beginning; %s possible containers; %s '
'possible objects') % (containers, objects))
for c in self.swift.iter_containers(self.expiring_objects_account):
container = c['name']
timestamp = int(container)
if timestamp > int(time()):
break
containers_to_delete.append(container)
for o in self.swift.iter_objects(self.expiring_objects_account,
container):
obj = o['name'].encode('utf8')
if processes > 0:
obj_process = int(
hashlib.md5('%s/%s' % (container, obj)).
hexdigest(), 16)
if obj_process % processes != process:
continue
timestamp, actual_obj = obj.split('-', 1)
timestamp = int(timestamp)
if timestamp > int(time()):
break
pool.spawn_n(
self.delete_object, actual_obj, timestamp,
container, obj)
pool.waitall()
for container in containers_to_delete:
try:
#.........这里部分代码省略.........
示例4: UtilizationAggregator
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
#.........这里部分代码省略.........
if processes < 0:
raise ValueError(
'processes must be an integer greater than or equal to 0')
if processes and process >= processes:
raise ValueError(
'process must be less than or equal to processes')
return processes, process
def aggregate_container(self, container):
start_time = time()
try:
objs_to_delete = list()
bytes_recvs = dict()
bytes_sents = dict()
ts, tenant_id, account = container.split('_', 2)
ts = int(float(ts))
for o in self.swift.iter_objects(self.sample_account, container):
name = o['name']
objs_to_delete.append(name)
ts, bytes_rv, bytes_st, trans_id, client_ip = name.split('/')
bill_type = self.get_billtype_by_client_ip(client_ip, ts)
bytes_recvs[bill_type] = bytes_recvs.get(bill_type,
0) + int(bytes_rv)
bytes_sents[bill_type] = bytes_sents.get(bill_type,
0) + int(bytes_st)
self.report_objects += 1
for o in objs_to_delete:
self.swift.delete_object(self.sample_account, container, o)
for bill_type, bt_rv in bytes_recvs.items():
t_object = 'transfer/%d/%d/%d_%d_%d' % (ts, bill_type, bt_rv,
bytes_sents[bill_type],
self.report_objects)
self._hidden_update(tenant_id, t_object)
except (Exception, Timeout) as err:
self.logger.increment('errors')
self.logger.exception(
_('Exception while aggregating sample %s %s') %
(container, str(err)))
self.logger.timing_since('timing', start_time)
self.report()
def account_info(self, tenant_id, timestamp):
path = '/v1/%s/%s?prefix=usage/%d&limit=1' % (self.aggregate_account,
tenant_id, timestamp)
resp = self.swift.make_request('GET', path, {}, (2,))
if len(resp.body) == 0:
return 0, 0, 0
usages = resp.body.split('/', 2)[2].rstrip()
cont_cnt, obj_cnt, bt_used = usages.split('_')
return int(cont_cnt), int(obj_cnt), int(bt_used)
def _hidden_update(self, container, obj, method='PUT'):
hidden_path = '/%s/%s/%s' % (self.aggregate_account, container, obj)
part, nodes = self.container_ring.get_nodes(self.aggregate_account,
container)
for node in nodes:
ip = node['ip']
port = node['port']
示例5: ObjectRestorer
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
#.........这里部分代码省略.........
"""
if kwargs.get('processes') is not None:
processes = int(kwargs['processes'])
else:
processes = self.processes
if kwargs.get('process') is not None:
process = int(kwargs['process'])
else:
process = self.process
if process < 0:
raise ValueError(
'process must be an integer greater than or equal to 0')
if processes < 0:
raise ValueError(
'processes must be an integer greater than or equal to 0')
if processes and process >= processes:
raise ValueError(
'process must be less than or equal to processes')
return processes, process
def start_object_restoring(self, obj):
start_time = time()
try:
actual_obj = obj
account, container, obj = actual_obj.split('/', 2)
archiveId = self.get_archiveid(account, container, obj)
if archiveId is None:
self.swift.delete_object(self.restoring_object_account,
self.todo_container, actual_obj)
return
jobId = self.glacier.retrieve_archive(archiveId).id
restoring_obj = make_glacier_hidden_object_name(actual_obj, jobId)
meta_prefix = 'X-Object-Meta'
meta = self.swift.get_object_metadata(account, container, obj,
metadata_prefix=meta_prefix)
meta = {'X-Object-Meta' + key: value for key, value in
meta.iteritems()}
self.update_action_hidden(self.restoring_object_account,
self.restoring_container,
restoring_obj, metadata=meta)
self.swift.delete_object(self.restoring_object_account,
self.todo_container, actual_obj)
self.report_objects += 1
self.logger.increment('start')
except (Exception, Timeout) as err:
self.logger.increment('errors')
report_exception(self.logger.exception,
_('Exception while restoring object %s. %s') %
(obj, str(err)), self.client)
self.logger.timing_since('timing', start_time)
self.report()
def get_archiveid(self, account, container, obj):
glacier_account = '%s%s' % (self.glacier_account_prefix, account)
glacier_obj = None
for o in get_objects_by_prefix(glacier_account, container, obj,
示例6: ObjectExpirer
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
class ObjectExpirer(Daemon):
def __init__(self, conf):
super(ObjectExpirer, self).__init__(conf)
self.conf = conf
self.logger = get_logger(conf, log_route='s3-object-expirer')
self.logger.set_statsd_prefix('s3-object-expirer')
self.interval = int(conf.get('interval') or 300)
self.s3_expiring_objects_account = \
(conf.get('auto_create_account_prefix') or '.') + \
(conf.get('expiring_objects_account_name') or
's3_expiring_objects')
conf_path = conf.get('__file__') or '/etc/swift/s3-object-expirer.conf'
request_tries = int(conf.get('request_tries') or 3)
self.swift = InternalClient(conf_path,
'Swift Object Expirer',
request_tries)
self.glacier = self._init_glacier()
self.glacier_account_prefix = '.glacier_'
self.report_interval = int(conf.get('report_interval') or 300)
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
self.recon_cache_path = conf.get('recon_cache_path',
'/var/cache/swift')
self.rcache = join(self.recon_cache_path, 'object.recon')
self.concurrency = int(conf.get('concurrency', 1))
if self.concurrency < 1:
raise ValueError("concurrency must be set to at least 1")
self.processes = int(self.conf.get('processes', 0))
self.process = int(self.conf.get('process', 0))
self.client = Client(self.conf.get('sentry_sdn', ''))
def _init_glacier(self):
con = Layer2(region_name='ap-northeast-1')
return con.get_vault('swift-s3-transition')
def report(self, final=False):
"""
Emits a log line report of the progress so far, or the final progress
is final=True.
:param final: Set to True for the last report once the expiration pass
has completed.
"""
if final:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass completed in %ds; %d objects expired') %
(elapsed, self.report_objects))
dump_recon_cache({'object_expiration_pass': elapsed,
'expired_last_pass': self.report_objects},
self.rcache, self.logger)
elif time() - self.report_last_time >= self.report_interval:
elapsed = time() - self.report_first_time
self.logger.info(_('Pass so far %ds; %d objects expired') %
(elapsed, self.report_objects))
self.report_last_time = time()
def run_once(self, *args, **kwargs):
"""
Executes a single pass, looking for objects to expire.
:param args: Extra args to fulfill the Daemon interface; this daemon
has no additional args.
:param kwargs: Extra keyword args to fulfill the Daemon interface; this
daemon accepts processes and process keyword args.
These will override the values from the config file if
provided.
"""
processes, process = self.get_process_values(kwargs)
pool = GreenPool(self.concurrency)
containers_to_delete = []
self.report_first_time = self.report_last_time = time()
self.report_objects = 0
try:
self.logger.debug(_('Run begin'))
containers, objects = \
self.swift.get_account_info(self.s3_expiring_objects_account)
self.logger.info(_('Pass beginning; %s possible containers; %s '
'possible objects') % (containers, objects))
for c in self.swift.iter_containers(self.
s3_expiring_objects_account):
container = c['name']
timestamp = int(container)
if timestamp > int(time()):
break
containers_to_delete.append(container)
for o in self.swift.iter_objects(self
.s3_expiring_objects_account,
container):
obj = o['name'].encode('utf8')
if processes > 0:
obj_process = int(
hashlib.md5('%s/%s' % (container, obj)).
hexdigest(), 16)
if obj_process % processes != process:
continue
pool.spawn_n(self.delete_object, container, obj)
pool.waitall()
for container in containers_to_delete:
#.........这里部分代码省略.........
示例7: ObjectTransitor
# 需要导入模块: from swift.common.internal_client import InternalClient [as 别名]
# 或者: from swift.common.internal_client.InternalClient import delete_object [as 别名]
#.........这里部分代码省略.........
except (Exception, Timeout):
report_exception(self.logger, _('Unhandled exception'), self.client)
def run_forever(self, *args, **kwargs):
"""
Executes passes forever, looking for objects to expire.
:param args: Extra args to fulfill the Daemon interface; this daemon
has no additional args.
:param kwargs: Extra keyword args to fulfill the Daemon interface; this
daemon has no additional keyword args.
"""
sleep(random() * self.interval)
while True:
begin = time()
try:
self.run_once(*args, **kwargs)
except (Exception, Timeout):
report_exception(self.logger, _('Unhandled exception'), self.client)
elapsed = time() - begin
if elapsed < self.interval:
sleep(random() * (self.interval - elapsed))
def get_process_values(self, kwargs):
"""
Gets the processes, process from the kwargs if those values exist.
Otherwise, return processes, process set in the config file.
:param kwargs: Keyword args passed into the run_forever(), run_once()
methods. They have values specified on the command
line when the daemon is run.
"""
if kwargs.get('processes') is not None:
processes = int(kwargs['processes'])
else:
processes = self.processes
if kwargs.get('process') is not None:
process = int(kwargs['process'])
else:
process = self.process
if process < 0:
raise ValueError(
'process must be an integer greater than or equal to 0')
if processes < 0:
raise ValueError(
'processes must be an integer greater than or equal to 0')
if processes and process >= processes:
raise ValueError(
'process must be less than or equal to processes')
return processes, process
def transition_object(self, container, obj):
start_time = time()
try:
obj_account, obj_container, obj_object = obj.split('/', 2)
lifecycle = Lifecycle(obj_account, obj_container, obj_object,
swift_client=self.swift)
if is_success(lifecycle.object.status):
object_header = lifecycle.object.headers
object_rule = lifecycle.get_object_rule_by_action(
'Transition')
last_modified = object_header['Last-Modified']
last_modified = gmt_to_timestamp(last_modified)
validation_flg = lifecycle.object_lifecycle_validation()
if (validation_flg == LIFECYCLE_OK) or \
(validation_flg == DISABLED_EXPIRATION):
times = calc_when_actions_do(object_rule, last_modified)
actual_expire_time = int(times['Transition'])
if actual_expire_time == int(container):
self.request_transition(obj)
self.swift.delete_object(self.s3_tr_objects_account,
container, obj)
except (Exception, Timeout) as err:
self.logger.increment('errors')
report_exception(self.logger,
_('Exception while transitioning object %s %s %s') %
(container, obj, str(err)), self.client)
self.logger.timing_since('timing', start_time)
self.report()
def request_transition(self, actual_obj):
path = '/v1/' + urllib.quote(actual_obj.lstrip('/'))
headers = {GLACIER_FLAG_META: True,
'X-S3-Object-Transition': True}
resp = self.swift.make_request('POST', path, headers, (2, 5))
if resp.status_int == 500:
raise Exception(resp.body)
self.report_objects += 1
self.logger.increment('objects')