本文整理匯總了Python中cloudstorage.listbucket方法的典型用法代碼示例。如果您正苦於以下問題:Python cloudstorage.listbucket方法的具體用法?Python cloudstorage.listbucket怎麽用?Python cloudstorage.listbucket使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cloudstorage
的用法示例。
在下文中一共展示了cloudstorage.listbucket方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _next_file
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def _next_file(self):
"""Find next filename.
self._filenames may need to be expanded via listbucket.
Returns:
None if no more file is left. Filename otherwise.
"""
while True:
if self._bucket_iter:
try:
return self._bucket_iter.next().filename
except StopIteration:
self._bucket_iter = None
self._bucket = None
if self._index >= len(self._filenames):
return
filename = self._filenames[self._index]
self._index += 1
if self._delimiter is None or not filename.endswith(self._delimiter):
return filename
self._bucket = cloudstorage.listbucket(filename,
delimiter=self._delimiter)
self._bucket_iter = iter(self._bucket)
示例2: GetBillingProjects
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def GetBillingProjects():
"""return a list of all projects we have billing export informaiton for."""
projects = Projects.get_by_id('Projects')
if projects is not None:
logging.debug('using cached projects')
return projects.projects
project_list = []
current_project = None
for billing_object in gcs.listbucket(BUCKET,
delimiter='/'):
project_match = MatchProjectDate(billing_object.filename)
if not project_match:
continue
project_name = project_match[0]
if current_project != project_name:
project_list.append(project_name)
current_project = project_name
projects = Projects(id='Projects')
projects.projects = project_list
projects.put()
return project_list
示例3: list_bucket
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def list_bucket(self, bucket):
"""Create several files and paginate through them."""
self.response.write('Listbucket result:\n')
# Production apps should set page_size to a practical value.
page_size = 1
stats = cloudstorage.listbucket(bucket + '/foo', max_keys=page_size)
while True:
count = 0
for stat in stats:
count += 1
self.response.write(repr(stat))
self.response.write('\n')
if count != page_size or count == 0:
break
stats = cloudstorage.listbucket(
bucket + '/foo', max_keys=page_size, marker=stat.filename)
# [END list_bucket]
示例4: _try_to_clean_garbage
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def _try_to_clean_garbage(self, writer_spec, exclude_list=()):
"""Tries to remove any files created by this shard that aren't needed.
Args:
writer_spec: writer_spec for the MR.
exclude_list: A list of filenames (strings) that should not be
removed.
"""
tmpl = string.Template(self._TMPFILE_PREFIX)
prefix = tmpl.substitute(
id=self.status.mapreduce_id, shard=self.status.shard)
bucket = self._get_tmp_gcs_bucket(writer_spec)
account_id = self._get_tmp_account_id(writer_spec)
for f in cloudstorage.listbucket("/%s/%s" % (bucket, prefix),
_account_id=account_id):
if f.filename not in exclude_list:
self._remove_tmpfile(f.filename, self.status.writer_spec)
示例5: _try_to_clean_garbage
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def _try_to_clean_garbage(self, writer_spec, exclude_list=()):
"""Tries to remove any files created by this shard that aren't needed.
Args:
writer_spec: writer_spec for the MR.
exclude_list: A list of filenames (strings) that should not be
removed.
"""
# Try to remove garbage (if any). Note that listbucket is not strongly
# consistent so something might survive.
tmpl = string.Template(self._TMPFILE_PREFIX)
prefix = tmpl.substitute(
id=self.status.mapreduce_id, shard=self.status.shard)
bucket = self._get_tmp_gcs_bucket(writer_spec)
account_id = self._get_tmp_account_id(writer_spec)
for f in cloudstorage.listbucket("/%s/%s" % (bucket, prefix),
_account_id=account_id):
if f.filename not in exclude_list:
self._remove_tmpfile(f.filename, self.status.writer_spec)
示例6: _get_matching_stats
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def _get_matching_stats(self, patterned_uris):
stats = []
patterns = {}
for patterned_uri in patterned_uris:
patterned_uri_split = patterned_uri.split('/')
bucket = '/'.join(patterned_uri_split[1:3])
pattern = '/'.join(patterned_uri_split[1:])
try:
if pattern not in patterns[bucket]:
patterns[bucket].append(pattern)
except KeyError:
patterns[bucket] = [pattern]
for bucket in patterns:
for stat in gcs.listbucket(bucket):
if not stat.is_dir:
for pattern in patterns[bucket]:
if fnmatch(stat.filename, pattern):
stats.append(stat)
break
return stats
示例7: split_input
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def split_input(cls, mapper_spec):
"""Returns a list of input readers.
An equal number of input files are assigned to each shard (+/- 1). If there
are fewer files than shards, fewer than the requested number of shards will
be used. Input files are currently never split (although for some formats
could be and may be split in a future implementation).
Args:
mapper_spec: an instance of model.MapperSpec.
Returns:
A list of InputReaders. None when no input data can be found.
"""
reader_spec = _get_params(mapper_spec, allow_old=False)
bucket = reader_spec[cls.BUCKET_NAME_PARAM]
filenames = reader_spec[cls.OBJECT_NAMES_PARAM]
delimiter = reader_spec.get(cls.DELIMITER_PARAM)
account_id = reader_spec.get(cls._ACCOUNT_ID_PARAM)
buffer_size = reader_spec.get(cls.BUFFER_SIZE_PARAM)
# Gather the complete list of files (expanding wildcards)
all_filenames = []
for filename in filenames:
if filename.endswith("*"):
all_filenames.extend(
[file_stat.filename for file_stat in cloudstorage.listbucket(
"/" + bucket + "/" + filename[:-1], delimiter=delimiter,
_account_id=account_id)])
else:
all_filenames.append("/%s/%s" % (bucket, filename))
# Split into shards
readers = []
for shard in range(0, mapper_spec.shard_count):
shard_filenames = all_filenames[shard::mapper_spec.shard_count]
if shard_filenames:
readers.append(cls(
shard_filenames, buffer_size=buffer_size, _account_id=account_id,
delimiter=delimiter))
return readers
示例8: tearDown
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def tearDown(self):
# for gcs_object in gcs.listbucket(main.BUCKET):
# gcs.delete(gcs_object.filename)
self.testbed.deactivate()
示例9: create_files_for_list_bucket
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def create_files_for_list_bucket(self, bucket):
self.response.write('Creating more files for listbucket...\n')
filenames = [bucket + n for n in [
'/foo1', '/foo2', '/bar', '/bar/1', '/bar/2', '/boo/']]
for f in filenames:
self.create_file(f)
# [START list_bucket]
示例10: list_bucket_directory_mode
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def list_bucket_directory_mode(self, bucket):
self.response.write('Listbucket directory mode result:\n')
for stat in cloudstorage.listbucket(bucket + '/b', delimiter='/'):
self.response.write(stat)
self.response.write('\n')
if stat.is_dir:
for subdir_file in cloudstorage.listbucket(
stat.filename, delimiter='/'):
self.response.write(' {}'.format(subdir_file))
self.response.write('\n')
# [START delete_files]
示例11: list_files
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def list_files(bucket, subdir=None, batch_size=100):
"""Yields filenames and stats of files inside subdirectory of a bucket.
It always lists directories recursively.
Arguments:
bucket: a bucket to list.
subdir: subdirectory to list files from or None for an entire bucket.
Yields:
Tuples of (filename, stats), where filename is relative to the bucket root
directory.
"""
# When listing an entire bucket, gcs expects /<bucket> without ending '/'.
path_prefix = '/%s/%s' % (bucket, subdir) if subdir else '/%s' % bucket
bucket_prefix = '/%s/' % bucket
marker = None
retry_params = _make_retry_params()
while True:
files_stats = cloudstorage.listbucket(
path_prefix=path_prefix,
marker=marker,
max_keys=batch_size,
retry_params=retry_params)
# |files_stats| is an iterable, need to iterate through it to figure out
# whether it's empty or not.
empty = True
for stat in files_stats:
# Restart next listing from the last fetched file.
marker = stat.filename
# pylint: disable=C0301
# https://developers.google.com/appengine/docs/python/googlecloudstorageclient/gcsfilestat_class
if stat.is_dir:
continue
empty = False
assert stat.filename.startswith(bucket_prefix)
yield stat.filename[len(bucket_prefix):], stat
# Last batch was empty -> listed all files.
if empty:
break
示例12: ls
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def ls(self, path, dir_only=False):
padded = path if path[-1] == '/' else path+'/'
return [f.filename for f in gcs.listbucket(padded, delimiter='/') if f.is_dir or not dir_only]
示例13: file_exists
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def file_exists(self, path):
match = list(gcs.listbucket(path.rstrip('/')))
return path.rstrip('/') in [stat.filename for stat in match]
示例14: path_exists
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def path_exists(self, path):
match = list(gcs.listbucket(path.rstrip('/'), delimiter='/'))
return path.rstrip('/') in [stat.filename.rstrip('/') for stat in match]
示例15: _execute
# 需要導入模塊: import cloudstorage [as 別名]
# 或者: from cloudstorage import listbucket [as 別名]
def _execute(self):
self._get_ml_job_id()
# Find directory where newest saved model is located
bucket = self._params['jobDir']
stats = gcs.listbucket(bucket[4:])
newest_file = None
for stat in stats:
if stat.filename.find('saved_model.pb') != -1:
if newest_file is None:
newest_file = stat
if newest_file:
if stat.st_ctime > newest_file.st_ctime:
newest_file = stat
body = {
"name": self._params['versionName'],
"description": "Test from python",
"deploymentUri": ("gs:/" + newest_file.
filename[0:newest_file.filename.rfind('/')]),
"pythonVersion": self._params['pythonVersion'],
"runtimeVersion": self._params['runtimeVersion'],
"framework": self._params['framework']
}
project_id = 'projects/%s' % self._params['project']
self._get_ml_client()
request = self._ml_client.projects().models().versions().create(
parent=project_id + "/models/" + self._params['modelName'], body=body)
response = self.retry(request.execute)()
self._enqueue('MLOperationWaiter', {'operation_name': response['name']}, 60)