def test_hasRead_withoutSeek_retry(self): key = 'withReadAndWithoutSeek_retry' data = ReadWithoutSeek('I only have read attribute!') set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name) ret, info = put_data(token, key, data) print(info) assert ret is None
def test_hasRead_WithoutSeek_retry2(self): key = 'withReadAndWithoutSeek_retry2' data = urlopen("http://www.qiniu.com") set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name) ret, info = put_data(token, key, data) print(info) assert ret is not None
def test_withoutRead_withoutSeek_retry(self): key = 'retry' data = 'hello retry!' set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name) ret, info = put_data(token, key, data) print(info) assert ret['key'] == key assert ret['hash'] == 'FlYu0iBR1WpvYi4whKXiBuQpyLLk'
def test_hasRead_hasSeek_retry(self): key = 'withReadAndSeek_retry' data = StringIO('hello retry again!') set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name) ret, info = put_data(token, key, data) print(info) assert ret['key'] == key assert ret['hash'] == 'FuEbdt6JP2BqwQJi7PezYhmuVYOo'
def test_retry(self): localfile = __file__ key = 'test_file_r_retry' qiniu.set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) token = self.q.upload_token(bucket_name, key) ret, info = put_file(token, key, localfile, self.params, self.mime_type) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
def test_big_file(self): key = 'big' token = self.q.upload_token(bucket_name, key) localfile = create_temp_file(4 * 1024 * 1024 + 1) progress_handler = lambda progress, total: progress qiniu.set_default(default_zone=Zone('http://a', 'http://upload.qiniu.com')) ret, info = put_file(token, key, localfile, self.params, self.mime_type, progress_handler=progress_handler) print(info) assert ret['key'] == key remove_temp_file(localfile)
def test_put_stream_v2_without_bucket_name(self): localfile = __file__ key = 'test_file_r' size = os.stat(localfile).st_size set_default(default_zone=Zone('http://upload.qiniup.com')) with open(localfile, 'rb') as input_stream: token = self.q.upload_token(bucket_name, key) ret, info = put_stream(token, key, input_stream, os.path.basename(__file__), size, hostscache_dir, self.params, self.mime_type, part_size=1024 * 1024 * 10, version='v2') assert ret['key'] == key
def test_put_zone_without_host(self): zone = Zone(up_host=None, up_host_backup=None, io_host=None, scheme='https') set_default(default_zone=zone) key = 'big' token = self.q.upload_token(bucket_name, key) localfile = create_temp_file(4 * 1024 * 1024 + 1) progress_handler = lambda progress, total: progress ret, info = put_file(token, key, localfile, self.params, self.mime_type, progress_handler=progress_handler) print(info) assert ret['key'] == key remove_temp_file(localfile)
def test_put_10m_stream_v2(self): localfile = create_temp_file(10 * 1024 * 1024 + 1) key = 'test_file_r' size = os.stat(localfile).st_size set_default(default_zone=Zone('http://upload.qiniup.com')) with open(localfile, 'rb') as input_stream: token = self.q.upload_token(bucket_name, key) ret, info = put_stream(token, key, input_stream, os.path.basename(localfile), size, hostscache_dir, self.params, self.mime_type, part_size=1024 * 1024 * 4, version='v2', bucket_name=bucket_name) assert ret['key'] == key remove_temp_file(localfile)
def upload_file(self, report_path, file_name): #name 是上传的文件名 # 构建鉴权对象 q = Auth(self.access_key, self.secret_key) # 上传到七牛后保存的文件名 key = file_name # 生成上传 Token,可以指定过期时间等 token = q.upload_token(self.bucket_name, key, 3600) # 要上传文件的本地路径 localfile = os.path.join(report_path, file_name) #print(localfile) # 指定固定域名的zone,不同区域uphost域名见下文档 # https://developer.qiniu.com/kodo/manual/1671/region-endpoint # 未指定或上传错误,sdk会根据token自动查询对应的上传域名 # *.qiniup.com 支持https上传 # 备用*.qiniu.com域名 不支持https上传 # 要求https上传时,如果客户指定的两个host都错误,且sdk自动查询的第一个*.qiniup.com上传域名因意外不可用导致访问到备用*.qiniu.com会报ssl错误 # 建议https上传时查看上面文档,指定正确的host # 设置七牛云上传文件服务器地址 zone = Zone( up_host='https://up-z2.qiniup.com', # 上传文件的主机域名 up_host_backup='https://upload.qiniup.com', io_host='http://iovip.qbox.me', scheme='https') set_default(default_zone=zone) try: ret, info = put_file(token, key, localfile) except Exception as err: print(info) print(ret) print(err) finally: return True
QINIU_ACCESS_KEY = get_qiniu_config('QINIU_ACCESS_KEY') QINIU_SECRET_KEY = get_qiniu_config('QINIU_SECRET_KEY') QINIU_BUCKET_NAME = get_qiniu_config('QINIU_BUCKET_NAME') QINIU_BUCKET_DOMAIN = get_qiniu_config('QINIU_BUCKET_DOMAIN', '').rstrip('/') QINIU_SECURE_URL = get_qiniu_config('QINIU_SECURE_URL', 'False') if isinstance(QINIU_SECURE_URL, six.string_types): if QINIU_SECURE_URL.lower() in ('true', '1'): QINIU_SECURE_URL = True else: QINIU_SECURE_URL = False # Set faster up host for foreign VPS set_default(default_zone=Zone('up.qiniug.com', 'up.qiniu.com')) 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):
@retry(tries=5, delay=0.5, jitter=0.1, logger=logging) def upload_file(self, key, localfile): token = self._auth.upload_token(self._bucket, key) ret, info = put_file(token, key, localfile) if ret: # ret possibly is None assert ret['key'] == key assert ret['hash'] == etag(localfile) return info # Get key from https://portal.qiniu.com/user/key access_key = 'caBese-UQYXwQeigGqtdgwybP2Qh2AlDPdcEd42C' # here only-for-test secret_key = '........' zone = Zone(up_host='https://up.qiniup.com', up_host_backup='https://upload.qiniup.com', io_host='http://iovip.qbox.me', scheme='https') set_default(default_zone=zone) def main(): parser = argparse.ArgumentParser() parser.add_argument("-A", '--access-key', help='access key, env-var QINIU_ACCESS_KEY', default=os.environ.get("QINIU_ACCESS_KEY", access_key)) parser.add_argument("-S", "--secret-key", help='secret key, env-var QINIU_SECRET_KEY', default=os.environ.get("QINIU_SECRET_KEY", secret_key)) parser.add_argument("-B",
secret_key = '...' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'Bucket_Name' # 上传到七牛后保存的文件名 key = 'my-python-logo.png' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = 'stat.py' # up_host, 指定上传域名,注意不同区域的qvm上传域名不同 # https://developer.qiniu.com/qvm/manual/4269/qvm-kodo zone = Zone(up_host='free-qvm-z1-zz.qiniup.com', up_host_backup='free-qvm-z1-zz.qiniup.com', io_host='iovip.qbox.me', scheme='http') set_default(default_zone=zone) ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
access_key = '...' secret_key = '...' # 构建鉴权对象 q = Auth(access_key, secret_key) # 要上传的空间 bucket_name = 'Bucket_Name' # 上传到七牛后保存的文件名 key = 'my-python-logo.png' # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 localfile = 'stat.py' # 指定固定的zone zone = Zone( up_host='uptest.qiniu.com', up_host_backup='uptest.qiniu.com', io_host='iovip.qbox.me', scheme='http') set_default(default_zone=zone) ret, info = put_file(token, key, localfile) print(info) assert ret['key'] == key assert ret['hash'] == etag(localfile)
import unittest import uuid import logging LOGGING_FORMAT = '\n%(levelname)s %(asctime)s %(message)s' logging.basicConfig(level=logging.INFO, format=LOGGING_FORMAT) logger = logging.getLogger(__name__) import six import django import pytest from requests.exceptions import ConnectionError from qiniu import set_default, BucketManager, Zone zone_overseas = Zone('up.qiniug.com', 'upload.qiniug.com') from .utils import retry # Add repo/demo_site to sys.path DEMO_SITE_DIR = join(dirname(dirname(__file__)), 'demo_site') sys.path.append(DEMO_SITE_DIR) os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo_site.settings") try: django.setup() except AttributeError: # Setup isn't necessary in Django < 1.7 pass from django.conf import settings