예제 #1
0
def qiniu_upload_img(key, imgpath, timeout=4):
    if not os.path.exists(imgpath):
        print('oops! this image not found')
        return ''

    if key is None:
        print('oops! no  key value')
        return ''

    #url = 'http://onm4mnb4w.bkt.clouddn.com/' + key
    url = 'http://workaiossqn.tiegushi.com/' + key
    localfile = imgpath

    try:
        set_default(connection_timeout=timeout)
        token = q.upload_token(bucket_name, key, 3600)
        ret, info = put_file(token, key, localfile)
        if info.status_code == 200:
            print('uploaded ' + localfile + ' as ' + url)
        else:
            url = ''
    except Exception as e:
        print(e)
        print("upload to qiniu failed")
        # os.remove(imgpath)
        return ''
    else:
        # os.remove(imgpath)
        return url
예제 #2
0
 def test_retry(self):
     key = 'retry'
     data = 'hello retry!'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret = put(token, key, data)
     assert ret['key'] == key
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #3
0
 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
예제 #4
0
 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
예제 #5
0
 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, key)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret is not None
예제 #6
0
 def test_retry(self):
     localfile = __file__
     key = 'test_file_r_retry'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name, key)
     ret = resumable_putfile(token, key, localfile, self.params, self.mime_type)
     assert ret['key'] == key
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #7
0
 def test_retry(self):
     key = 'retry'
     data = 'hello retry!'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret = put(token, key, data)
     assert ret['key'] == key
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #8
0
 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
예제 #9
0
 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"
예제 #10
0
 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"
예제 #11
0
 def test_hasRead_WithoutSeek_retry2(self):
     key = 'withReadAndWithoutSeek_retry2'
     data = urlopen("http://www.qiniu.com")
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret == None
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #12
0
 def test_hasRead_withoutSeek_retry(self):
     key = 'withReadAndWithoutSeek_retry'
     data = ReadWithoutSeek('I only have read attribute!')
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret == None
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #13
0
 def test_retry(self):
     localfile = __file__
     key = 'test_file_r_retry'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name, key)
     ret = resumable_putfile(token, key, localfile, self.params,
                             self.mime_type)
     assert ret['key'] == key
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #14
0
 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'
예제 #15
0
 def test_hasRead_withoutSeek_retry(self):
     key = 'withReadAndWithoutSeek_retry'
     data = ReadWithoutSeek('I only have read attribute!')
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret == None
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #16
0
 def test_hasRead_WithoutSeek_retry2(self):
     key = 'withReadAndWithoutSeek_retry2'
     data = urlopen("http://www.qiniu.com")
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret == None
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #17
0
 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'
예제 #18
0
 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'
예제 #19
0
 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'
예제 #20
0
 def test_withoutRead_withoutSeek_retry(self):
     key = 'retry'
     data = 'hello retry!'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret['key'] == key
     assert ret['hash'] == 'FlYu0iBR1WpvYi4whKXiBuQpyLLk'
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #21
0
 def test_withoutRead_withoutSeek_retry(self):
     key = 'retry'
     data = 'hello retry!'
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret['key'] == key
     assert ret['hash'] == 'FlYu0iBR1WpvYi4whKXiBuQpyLLk'
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #22
0
 def test_hasRead_hasSeek_retry(self):
     key = 'withReadAndSeek_retry'
     data = StringIO('hello retry again!')
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret['key'] == key
     assert ret['hash'] == 'FuEbdt6JP2BqwQJi7PezYhmuVYOo'
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #23
0
 def test_hasRead_hasSeek_retry(self):
     key = 'withReadAndSeek_retry'
     data = StringIO('hello retry again!')
     set_default(default_up_host='a')
     token = self.q.upload_token(bucket_name)
     ret, info = put_data(token, key, data)
     print(info)
     assert ret['key'] == key
     assert ret['hash'] == 'FuEbdt6JP2BqwQJi7PezYhmuVYOo'
     qiniu.set_default(default_up_host=qiniu.config.UPAUTO_HOST)
예제 #24
0
 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
예제 #25
0
 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)
예제 #26
0
    def _save_to_qiniu(self, token, key):
        self._source.seek(0)

        import qiniu
        qiniu.set_default(connection_timeout=self.timeout)

        if six.PY3:
            # use patched put_data implementation for py3k
            ret, info = self._save_to_qiniu_internal_py3(token, key)
        else:
            # use put_data implementation provided by qiniu-sdk
            ret, info = qiniu.put_data(token, key, self._source)
        self._source.seek(0)

        if info.status_code != 200:
            self._save_callback(token, False)
            raise LeanCloudError(
                1, 'the file is not saved, qiniu status code: {0}'.format(
                    info.status_code))
        self._save_callback(token, True)
예제 #27
0
 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)
예제 #28
0
    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
예제 #29
0
def qiniu_upload_data(key, data, timeout=4):
    """上传二进制流"""
    if data == '':
        return ''
    key = key + SUFFIX
    # url = 'http://onm4mnb4w.bkt.clouddn.com/' + key
    url = 'http://workaiossqn.tiegushi.com/' + key

    try:
        set_default(connection_timeout=timeout)
        token = q.upload_token(bucket_name, key, 3600)
        ret, info = put_data(token, key, data)
        if info.status_code == 200:
            print('uploaded embedding as ' + url)
        else:
            url = ''
    except Exception as e:
        print(e)
        print("upload to qiniu failed")
        # os.remove(imgpath)
        return ''
    else:
        # os.remove(imgpath)
        return url
예제 #30
0

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):
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo-project.settings")

try:
    django.setup()
except AttributeError:
   # Setup isn't necessary in Django < 1.7
   pass

from django.conf import settings
from qiniustorage.backends import QiniuStorage, QiniuFile
from qiniustorage.utils import QiniuError

USING_TRAVIS = os.environ.get('USING_TRAVIS', None) is None
if USING_TRAVIS:
    set_default(default_up_host='up.qiniug.com', connection_timeout=100, connection_retries=20)

UNIQUE_PATH = str(uuid.uuid4())



class QiniuStorageTest(unittest.TestCase):
    def setUp(self):
        self.storage = QiniuStorage()

    def test_file_init(self):
        fil = QiniuFile('foo', self.storage, mode='rb')
        assert fil._mode == 'rb'
        assert fil._name == "foo"

    def test_write_to_read_only_file(self):
예제 #32
0
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
from qiniustorage.backends import QiniuStorage, QiniuFile
from qiniustorage.utils import QiniuError

USING_TRAVIS = os.environ.get('USING_TRAVIS', None) is None
if USING_TRAVIS:
    set_default(default_zone=zone_overseas, connection_retries=20, connection_timeout=100)

UNIQUE_PATH = str(uuid.uuid4())



class QiniuStorageTest(unittest.TestCase):
    def setUp(self):
        self.storage = QiniuStorage()

    def test_file_init(self):
        fil = QiniuFile('foo', self.storage, mode='rb')
        assert fil._mode == 'rb'
        assert fil._name == "foo"

    def test_write_to_read_only_file(self):
import os
import re
import time
import sys

from config import access_key, secret_key, bucket_name, MAX_TRIES, \
                   QINIU_UPLOAD_URL, LIBCHROMIUMCONTENT_BINARIES
from qiniu import Auth, put_file, set_default, Zone, BucketManager

auth = Auth(access_key, secret_key)
last_progress = 0
last_record_time = time.time()

# The default qiniu upload host doesn't support uploading files outside China.
# Use up.qiniug.com provided from qiniu's custom service
set_default(default_zone=Zone(QINIU_UPLOAD_URL, QINIU_UPLOAD_URL))


def get_all_uploaded_files():
  sys.stdout.write('Retrieve uploaded file in qiniu...\n')
  sys.stdout.flush()
  bucket = BucketManager(auth)
  marker = None
  eof = False
  uploaded_files = []
  while not eof:
    ret, eof, info = bucket.list(bucket_name, marker=marker)
    marker = ret.get('marker', None)
    for item in ret['items']:
      uploaded_files.append(item['key'])
  return uploaded_files
예제 #34
0
        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",
                        "--bucket",
                        help='bucket name',
예제 #35
0
    str += ", rquest_uri:" + rquest_uri + "\r\n"
    remote_addr = environ["REMOTE_ADDR"]
    str += ",remote_addr:" + remote_addr + "\r\n"
    remote_port = environ["REMOTE_PORT"]
    str += ",remote_port:" + remote_port + "\r\n"
    data = environ["wsgi.input"].read()
    str += ", data:" + data + "\r\n"
    return str


def application(environ, start_response):
    #logging.basicConfig(level=logging.DEBUG,
    #                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    #                datefmt='%a, %d %b %Y %H:%M:%S',
    #                filename='/tmp/test.log',
    #                filemode='w')
    start_response('200 OK', [('Content-Type', 'text/plain')])
    (ret, params) = get_params(environ)
    #logging.debug("application------------", params)
    if not ret:
        return ["failed"]
    (ret, status) = upload_qiniu(params)
    #content = get_environ(environ)
    return [status]


if __name__ == "__main__":
    set_default(connection_pool=30)
    #flups.WSGIServer(application, multithreaded=True, multiprocess=False, bindAddress=('127.0.0.1, 21000')).run()
    flups.WSGIServer(application).run()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo-project.settings")

try:
    django.setup()
except AttributeError:
    # Setup isn't necessary in Django < 1.7
    pass

from django.conf import settings
from qiniustorage.backends import QiniuStorage, QiniuFile
from qiniustorage.utils import QiniuError

USING_TRAVIS = os.environ.get('USING_TRAVIS', None) is None
if USING_TRAVIS:
    set_default(default_up_host='up.qiniug.com',
                connection_timeout=100,
                connection_retries=20)

UNIQUE_PATH = str(uuid.uuid4())


class QiniuStorageTest(unittest.TestCase):
    def setUp(self):
        self.storage = QiniuStorage()

    def test_file_init(self):
        fil = QiniuFile('foo', self.storage, mode='rb')
        assert fil._mode == 'rb'
        assert fil._name == "foo"

    def test_write_to_read_only_file(self):
예제 #37
0
import qiniu.config
from qiniu import Zone, set_default

# 需要填写你的 Access Key 和 Secret Key
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)