本文整理汇总了Python中qiniu.BucketManager.stat方法的典型用法代码示例。如果您正苦于以下问题:Python BucketManager.stat方法的具体用法?Python BucketManager.stat怎么用?Python BucketManager.stat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qiniu.BucketManager
的用法示例。
在下文中一共展示了BucketManager.stat方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: QiniuUpload
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class QiniuUpload():
def __init__(self, bucket = config.PIC_BUCKET, domain = config.PIC_DOMAIN):
self.qiniuer = qiniu.Auth(config.QINIU_ACCESS_KEY, config.QINIU_SECRET_KEY)
self.bucket_manager = BucketManager(self.qiniuer)
self.bucket = bucket
self.domain = domain
def get_token(self):
return self.qiniuer.upload_token(self.bucket)
def del_file(self, key):
ret, info = self.bucket_manager.delete(self.bucket, key)
# 错误处理
assert ret is None
assert info.status_code == 612
return ret, info
def get_upload_info(self, key):
ret, info = self.bucket_manager.stat(self.bucket, key)
assert 'hash' in ret
return info
def get_file_info(self, key):
url = self.domain + key + '?imageInfo'
try:
response = urllib2.urlopen(url, timeout=5)
resp = response.read()
return resp
except Exception, e:
print e
return None
示例2: Qiniu
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class Qiniu(object):
def __init__(self, uri):
assert uri.startswith('http://')
self.qn = Auth(qnconf['access_key'], qnconf['secret_key'])
self.token = self.qn.upload_token(qnconf['bucket_name'])
self.bucket = BucketManager(self.qn)
# def stat_image(self, key):
# status, image_info = self.bucket.stat(qnconf['bucket_name'], key)
# last_modified = int(status['putTime'] / 1000000)
# return {'checksum': status['hash'], 'last_modified': last_modified}
# def persist_image(self, key, image, buf, info):
# buf.seek(0)
# return threads.deferToThread(put_data, self.token, key, buf.getvalue())
def get_file_stat(self, key):
stat, error = self.bucket.stat(qnconf['bucket_name'], key)
return stat
def stat_file(self, path, info):
def _onsuccess(stat):
if stat:
checksum = stat['hash']
timestamp = stat['putTime'] / 10000000
return {'checksum': checksum, 'last_modified': timestamp}
else:
return {}
return threads.deferToThread(self.get_file_stat, path).addCallback(_onsuccess)
def persist_file(self, path, buf, info, meta=None, headers=None):
buf.seek(0)
return threads.deferToThread(put_data, self.token, path, buf.getvalue())
示例3: upload
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def upload(q, bucket_name, local_path, remote_path):
bucket = BucketManager(q)
assert bucket.stat(bucket_name, '')[1].status_code != 401
for rel_path in list_file(local_path):
key = join(remote_path, rel_path)
stat, _ = bucket.stat(bucket_name, key)
if not stat:
token = q.upload_token(bucket_name, key, 3600)
log.info('uploading new file %s' % rel_path)
put_file(token, key, join(local_path, rel_path))
elif stat['fsize'] != getsize(join(local_path, rel_path)) \
or stat['hash'] != etag(join(local_path, rel_path)):
token = q.upload_token(bucket_name, key, 3600)
log.info('uploading diff file %s' % rel_path)
put_file(token, key, join(local_path, rel_path))
else:
log.debug('file %s is identical' % rel_path)
示例4: isFileExist
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def isFileExist(file_name):
q = getAuth()
# check if file already exist
bucket_name = os.environ['server']
bucket = BucketManager(getAuth())
ret, info = bucket.stat(bucket_name, file_name)
if ret != None:
return True
else:
return False
示例5: delete_file
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def delete_file(bucket_name, key):
global access_key
global secret_key
global q
bucket = BucketManager(q)
ret, info = bucket.stat(bucket_name, key)
if ret and ('hash' in ret):
ret, info = bucket.delete(bucket_name, key)
#print(ret)
#print(info)
print("%s delete in qiniu"%basename(key))
else:
print("%s not exist in qiniu"%basename(key))
示例6: test_fetch
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def test_fetch():
q = Auth(access_key, secret_key)
bm = BucketManager(q)
ret, info = bm.stat(bucket, mykey)
print(info)
print(ret)
bucket_domain = '7xpb8s.com1.z0.glb.clouddn.com'
base_url = 'http://%s/%s' % (bucket_domain, mykey)
private_url = q.private_download_url(base_url, expires=3600)
print(private_url)
r = requests.get(private_url)
assert r.status_code == 200
ret, info = bm.fetch(private_url, bucket, mykey)
print(info)
print(ret)
示例7: QiniuStorage
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class QiniuStorage(object):
"""docstring for QiniuStorage"""
logger = logging.getLogger("wechat")
def __init__(self, cfg):
super(QiniuStorage, self).__init__()
self.cfg = cfg
self.access_key = cfg.qiniu_access_key
self.secret_key = cfg.qiniu_secret_key
self.bucket_name = cfg.qiniu_bucket_name
self.bucket_domain = cfg.qiniu_bucket_domain
self._q = Auth(self.access_key, self.secret_key)
self._bucket = BucketManager(self._q)
def check_existance_and_get_url(self, file_key):
ret, info = self._bucket.stat(self.bucket_name, file_key)
if ret and info.status_code == 200:
return self.get_file_url(file_key)
return None
def get_file_url(self, file_key):
return "http://%s/%s" % (self.bucket_domain, file_key)
def upload_file(self, filepath, file_key=None, mime_type=None):
if file_key is None:
file_key = path.basename(filepath)
token = self._q.upload_token(self.bucket_name)
ret, info = put_file(token, file_key, filepath, mime_type=mime_type, check_crc=True)
#file_exists
if ret and info.status_code == 614:
return self.get_file_url(file_key)
#upload success
assert ret['key'] == file_key
assert ret['hash'] == etag(filepath)
return self.get_file_url(file_key)
def upload_and_get_url(self, filepath, file_key=None, mime_type=None):
if file_key is None:
file_key = path.basename(filepath)
url = self.check_existance_and_get_url(file_key)
if url:
return url
return self.upload_file(filepath, file_key, mime_type)
示例8: Auth
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth
from qiniu import BucketManager
access_key = '...'
secret_key = '...'
#初始化Auth状态
q = Auth(access_key, secret_key)
#初始化BucketManager
bucket = BucketManager(q)
#你要测试的空间, 并且这个key在你空间中存在
bucket_name = 'Bucket_Name'
key = 'python-logo.png'
#获取文件的状态信息
ret, info = bucket.stat(bucket_name, key)
print(info)
assert 'hash' in ret
示例9: qiniu_meta
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def qiniu_meta(self):
bucket = BucketManager(current_app.qiniu.qiniu_auth)
ret, info = bucket.stat(current_app.qiniu.PRIVATE_BUCKET_NAME, self.qiniu_key.encode('utf-8'))
return info
示例10: __init__
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class QiniuBucketManager:
def __init__(self, auth, bucket):
"""
@param auth: qiniu auth object
@param bucket: bucket name
"""
self.auth = auth
self.bucket = bucket
self.bktmanager = BucketManager(auth)
self.upload_token = auth.upload_token(bucket)
def data_info(self, key):
"""
return information with keys {fsize, hash, mimetype, putTime} else
raise `QiniuReadDataError` with status_code and error message
"""
r, info = self.bktmanager.stat(self.bucket, key)
if not r:
raise QiniuReadDataError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
else:
return r
def push_data(self, key, data):
"""
return hash code if upload sucess else raise `QiniuPushDataError`
with status_code and error message
"""
ret, info = put_data(self.upload_token, key, data)
if not ret:
raise QiniuPushDataError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
else:
return ret['hash']
def delete_data(self, key):
"""
delete data `key`
"""
_, info = self.bktmanager.delete(self.bucket, key)
if info.status_code != 200:
raise QiniuError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
def batch_delete_data(self, keys):
if keys:
ops = build_batch_delete(self.bucket, keys)
_, info = self.bktmanager.batch(ops)
if info.status_code != 200:
msg = ''
for e in info.text_body:
msg += msg + ';' + 'status_code:{0}, error:{1}'.format(e)
raise QiniuError(msg)
def copy_data_to(self, sdata, dbucket, ddata):
"""
copy data `sdata` in this bucket to destination bucket `dbucket` with name `ddata`
"""
_, info = self.bktmanager.copy(self.bucket, sdata, dbucket, ddata)
if info.status_code != 200:
raise QiniuError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
def copy_data_from(self, sbucket, sdata, ddata):
"""
copy data from `sdata` in bucket `sbucket` to this bucket with name `ddata`
"""
_, info = self.bktmanager.copy(sbucket, sdata, self.bucket, ddata)
if info.status_code != 200:
raise QiniuError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
def move_data_to(self, sdata, dbucket, ddata):
"""
move data `sdata` in this bucket to destination bucket `dbucket` with name `ddata`
"""
_, info = self.bktmanager.move(self.bucket, sdata, dbucket, ddata)
if info.status_code != 200:
raise QiniuError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
def move_data_from(self, sbucket, sdata, ddata):
"""
move data from `sdata` in bucket `sbucket` to this bucket with name `ddata`
"""
_, info = self.bktmanager.move(sbucket, sdata, self.bucket, ddata)
if info.status_code != 200:
raise QiniuError('status_code:{0}, error:{1}'.format(info.status_code,
info.text_body))
def datas(self, prefix=None, limit=None, marker=None):
"""
list datas in bucket with keys fsize, hash, key, mimeType, puttime of each data
"""
def list_files(marker, limit):
k = {'bucket':self.bucket}
if limit:
k['limit'] = limit
if prefix:
#.........这里部分代码省略.........
示例11: stat
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
def stat(self, key):
bucket = BucketManager(self.q)
ret, info = bucket.stat(self.bucket_name, key)
print(info)
assert 'hash' in ret
示例12: QiniuStorage
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class QiniuStorage(Storage):
"""
Qiniu Storage Service
"""
location = ""
def __init__(
self,
access_key=QINIU_ACCESS_KEY,
secret_key=QINIU_SECRET_KEY,
bucket_name=QINIU_BUCKET_NAME,
bucket_domain=QINIU_BUCKET_DOMAIN,
secure_url=QINIU_SECURE_URL):
self.auth = Auth(access_key, secret_key)
self.bucket_name = bucket_name
self.bucket_domain = bucket_domain
self.bucket_manager = BucketManager(self.auth)
self.secure_url = secure_url
def _clean_name(self, name):
"""
Cleans the name so that Windows style paths work
"""
# Normalize Windows style paths
clean_name = posixpath.normpath(name).replace('\\', '/')
# os.path.normpath() can strip trailing slashes so we implement
# a workaround here.
if name.endswith('/') and not clean_name.endswith('/'):
# Add a trailing slash as it was stripped.
return clean_name + '/'
else:
return clean_name
def _normalize_name(self, name):
"""
Normalizes the name so that paths like /path/to/ignored/../foo.txt
work. We check to make sure that the path pointed to is not outside
the directory specified by the LOCATION setting.
"""
base_path = force_text(self.location)
base_path = base_path.rstrip('/')
final_path = urljoin(base_path.rstrip('/') + "/", name)
base_path_len = len(base_path)
if (not final_path.startswith(base_path) or
final_path[base_path_len:base_path_len + 1] not in ('', '/')):
raise SuspiciousOperation("Attempted access to '%s' denied." %
name)
return final_path.lstrip('/')
def _open(self, name, mode='rb'):
return QiniuFile(name, self, mode)
def _save(self, name, content):
cleaned_name = self._clean_name(name)
name = self._normalize_name(cleaned_name)
if hasattr(content, 'open'):
# Since Django 1.6, content should be a instance
# of `django.core.files.File`
content.open()
if hasattr(content, 'chunks'):
content_str = b''.join(chunk for chunk in content.chunks())
else:
content_str = content.read()
self._put_file(name, content_str)
content.close()
return cleaned_name
def _put_file(self, name, content):
token = self.auth.upload_token(self.bucket_name)
ret, info = put_data(token, name, content)
if ret is None or ret['key'] != name:
raise QiniuError(info)
def _read(self, name):
return requests.get(self.url(name)).content
def delete(self, name):
name = self._normalize_name(self._clean_name(name))
if six.PY2:
name = name.encode('utf-8')
ret, info = self.bucket_manager.delete(self.bucket_name, name)
if ret is None or info.status_code == 612:
raise QiniuError(info)
def _file_stat(self, name, silent=False):
name = self._normalize_name(self._clean_name(name))
if six.PY2:
name = name.encode('utf-8')
ret, info = self.bucket_manager.stat(self.bucket_name, name)
if ret is None and not silent:
raise QiniuError(info)
#.........这里部分代码省略.........
示例13: QiniuStorage
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class QiniuStorage(Storage):
"""
Qiniu Storage Service
"""
location = ""
def __init__(
self,
access_key=QINIU_ACCESS_KEY,
secret_key=QINIU_SECRET_KEY,
bucket_name=QINIU_BUCKET_NAME,
bucket_domain=QINIU_BUCKET_DOMAIN):
self.auth = Auth(access_key, secret_key)
self.bucket_name = bucket_name
self.bucket_domain = bucket_domain
self.bucket_manager = BucketManager(self.auth)
def _clean_name(self, name):
return force_text(name)
def _normalize_name(self, name):
return ("%s/%s" % (self.location, name.lstrip('/').lstrip('./'))).lstrip('/')
def _open(self, name, mode='rb'):
return QiniuFile(name, self, mode)
def _save(self, name, content):
name = self._normalize_name(self._clean_name(name))
if hasattr(content, 'open'):
# Since Django 1.6, content should be a instance
# of `django.core.files.File`
content.open()
if hasattr(content, 'chunks'):
content_str = b''.join(chunk for chunk in content.chunks())
else:
content_str = content.read()
self._put_file(name, content_str)
content.close()
return name
def _put_file(self, name, content):
token = self.auth.upload_token(self.bucket_name)
ret, info = put_data(token, name, content)
if ret is None or ret['key'] != name:
raise QiniuError(info)
def _read(self, name):
return requests.get(self.url(name)).content
def delete(self, name):
name = self._normalize_name(self._clean_name(name))
ret, info = self.bucket_manager.delete(self.bucket_name, name)
if ret is None or info.status_code == 612:
raise QiniuError(info)
def _file_stat(self, name, silent=False):
name = self._normalize_name(self._clean_name(name))
ret, info = self.bucket_manager.stat(self.bucket_name, name)
if ret is None and not silent:
raise QiniuError(info)
return ret
def exists(self, name):
stats = self._file_stat(name, silent=True)
return True if stats else False
def size(self, name):
stats = self._file_stat(name)
return stats['fsize']
def modified_time(self, name):
stats = self._file_stat(name)
time_stamp = float(stats['putTime'])/10000000
return datetime.datetime.fromtimestamp(time_stamp)
def listdir(self, name):
name = self._normalize_name(self._clean_name(name))
if name and not name.endswith('/'):
name += '/'
dirlist = bucket_lister(self.bucket_manager, self.bucket_name, prefix=name)
files = []
dirs = set()
base_parts = name.split("/")[:-1]
for item in dirlist:
parts = item['key'].split("/")
parts = parts[len(base_parts):]
if len(parts) == 1:
# File
files.append(parts[0])
elif len(parts) > 1:
# Directory
dirs.add(parts[0])
return list(dirs), files
def url(self, name):
#.........这里部分代码省略.........
示例14: Qiniu
# 需要导入模块: from qiniu import BucketManager [as 别名]
# 或者: from qiniu.BucketManager import stat [as 别名]
class Qiniu(object):
def __init__(self, _ak, _sk):
import os
from qiniu import Auth, BucketManager
if os.name in ['nt']:
os.environ["HOME"] = r"../"
self.q = Auth(_ak, _sk)
self.bucket = BucketManager(self.q)
self.domain = "images.miasun.cn"
def upload_file_by_file(self, _bucket_name, local_file, key=None):
import os
from qiniu import put_file
if not os.path.exists(local_file):
print("文件不存在,请检查文件地址!")
return False
if key is None:
# 未传入key时,使用文件名作为key
key = os.path.split(local_file)[-1]
token = self.q.upload_token(bucket=_bucket_name, key=key)
ret, info = self.get_file_info(bucket_name, key)
if ret:
print("已上传: http://{domain}/{key}".format(domain=self.domain, key=key))
else:
ret, info = put_file(token, key, local_file)
return ret, info
def upload_files_by_dir(self, _bucket_name, dir_):
failed = []
for dirpath, dirnames, filenames in os.walk(dir_):
for filename in filenames:
local_file = os.path.join(dirpath, filename)
key = local_file[len(os.path.split(dir_)[0]) + 1:].replace("\\", "/")
if self.is_file_uploaded(bucket_name, key):
print("已上传: http://{domain}/{key}".format(domain=self.domain, key=key))
continue
ret, info = self.upload_file_by_file(_bucket_name, local_file, key)
if ret is None:
failed.append(key)
print("上传失败:{key}".format(key=key))
else:
print("上传成功:\n![{filename}](http://{domain}/{key}) \n".format(
filename=filename,
key=key,
domain=self.domain
))
return failed
def upload(self, bucket_name, to_upload):
if os.path.isfile(to_upload):
return self.upload_file_by_file(bucket_name, to_upload)
elif os.path.isdir(to_upload):
return self.upload_files_by_dir(bucket_name, to_upload)
else:
return False
def upload_and_callback(self, bucket_name, local_file, key=None):
import os
from qiniu import put_file
if not os.path.exists(local_file):
print("文件不存在,请检查文件地址!")
return False
if key is None:
key = os.path.split(local_file)[-1]
policy = {
"callbackUrl": "http://your.domain.com/callback.php",
"callbackBody": "filename=$(fname)&filesize=$(fsize)"
}
token = self.q.upload_token(bucket=bucket_name, key=key, policy=policy)
ret, info = put_file(token, key, local_file)
return ret, info
def get_file_info(self, bucket_name, key):
return self.bucket.stat(bucket_name, key)
def is_file_uploaded(self, bucket_name, key):
ret, info = self.get_file_info(bucket_name, key)
if ret is None:
return False
else:
return True
def move_file(self, old_bucket, old_key, new_bucket, new_key):
return self.bucket.move(old_bucket, old_key, new_bucket, new_key)
def copy_file(self, old_bucket, old_key, new_bucket, new_key):
return self.bucket.copy(old_bucket, old_key, new_bucket, new_key)
def delete_file(self, bucket_name, key):
return self.bucket.delete(bucket_name, key)