Example #1
0
def upload_qiniu_by_path(access_key, secret_key, bucket_name, key_prefix,
                         pool_number, path, delete=False):
    q = Auth(access_key, secret_key)
    mime_type = "text/plain"
    params = {'x:a': 'a'}

    pool = Pool(pool_number)

    for dirpath, dirnames, filenames in os.walk(path):
        print(dirpath)
        if len(filenames) > 0:
            for filename in filenames:
                if filename.startswith('.'):
                    continue
                localfile = os.path.join(dirpath, filename)
                key = os.path.join(key_prefix, localfile.replace(path, '')[1:])
                token = q.upload_token(bucket_name, key)

                pool.spawn(
                    down,
                    token=token,
                    key=key,
                    localfile=localfile,
                    mime_type=mime_type,
                    delete=delete
                )
Example #2
0
def upload_photo():
    """上传照片到七牛,并返回私有链接地址"""
    from qiniu import Auth
    from qiniu import put_file

    global config
    progress_handler = lambda progress, total: progress

    photo_path = http_get("http://127.0.0.1:9876/photo/shot").content
    # Upload to qiniu
    mime_type = "image/jpeg"
    auth = Auth(str(config["qiniu"]["api_key"]), str(config["qiniu"]["secret"]))
    print auth
    filename = os.path.basename(photo_path)
    print "filename: ", filename, type(filename)
    token = auth.upload_token(str(config["qiniu"]["bucket"]))
    print token
    ret, info = put_file(token, filename, photo_path, {}, mime_type, progress_handler=progress_handler)
    print "uploaded: ", ret, info
    try:
        os.remove(photo_path)
    except Exception:
        pass

    # Return URL
    base_url = "{}/{}".format(str(config["qiniu"]["domain"]), filename)
    return auth.private_download_url(base_url, expires=3600)
Example #3
0
	def get(self):

		access_key = 'YOUR_ACCESS_KEY'
		secret_key = 'YOUR_SECRET_KEY'

		q = Auth(access_key, secret_key)

		bucket_name = 'YOUR_BUCKET_NAME'
		
		# to set the uploaded file rename to YYYYMMDD_HHMMSS_filename
		# if the filename was "test.jpg", then the new filename will be "20160101_083054_test.jpg"
		# SEE MORE: http://developer.qiniu.com/article/kodo/kodo-developer/up/vars.html#magicvar
		policy = {"saveKey": "$(year)$(mon)$(day)_$(hour)$(min)$(sec)_$(fname)"}


		# in the source of qiniu/auth.py, the function upload_token takes 5 arguments:
		#	 bucket:your bucket_name
		#		key: the filename you want to set, None means set as ths "saveKey" above
		#	expires: expires time (second)
		#	 policy: the options to upload,see more: http://developer.qiniu.com/article/developer/security/put-policy.html
		#
		# def upload_token(self, bucket, key=None, expires=3600, policy=None, strict_policy=True):

		token = q.upload_token(bucket_name, None, 3600, policy)

		# return as json
		self.write(json.dumps({"uptoken":token}))
Example #4
0
File: utils.py Project: xuemy/btbbs
def upload_to_qiniu(AK, SK, BUCKET_NAME,FILE_OBJ,FILE_NAME,):
    '''
    上传文件到七牛云,并返回 文件hash

    BUCKET_NAME 七牛资源空间名
    FILE_OBJ 文件二进制流
    FILE_NAME 文件保存到七牛云上的文件名
    '''
    q = Auth(AK, SK)
    token = q.upload_token(BUCKET_NAME, FILE_NAME, 3600)
    # ret
    # 一个dict变量,类似 {"hash": "<Hash string>", "key": "<Key string>"}
    # info
    """七牛HTTP请求返回信息类

    该类主要是用于获取和解析对七牛发起各种请求后的响应包的header和body。

    Attributes:
        status_code: 整数变量,响应状态码
        text_body:   字符串变量,响应的body
        req_id:      字符串变量,七牛HTTP扩展字段,参考 http://developer.qiniu.com/docs/v6/api/reference/extended-headers.html
        x_log:       字符串变量,七牛HTTP扩展字段,参考 http://developer.qiniu.com/docs/v6/api/reference/extended-headers.html
        error:       字符串变量,响应的错误内容
    """
    ret, info = put_data(token, FILE_NAME, FILE_OBJ)
    if info.status_code:
        if ret['hash'] == etag_stream(FILE_OBJ):
            return ret['hash']
        return None
    return None
Example #5
0
def upload(origin_file_path):
    # 构建鉴权对象
    q = Auth(config.access_key, config.secret_key)

    # 要上传的空间
    bucket_name = 'md-doc'
    localfile = conwebp.convert(origin_file_path)

    # 上传到七牛后保存的文件名
    dest_prefix = time.strftime("%Y%m%d%H%M%S", time.localtime())
    dest_name = dest_prefix + "_" + os.path.basename(localfile)

    # 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器。
    policy = {
        'callbackBody': 'filename=$(fname)&filesize=$(fsize)'
    }

    token = q.upload_token(bucket_name, dest_name, 3600, policy)

    ret, info = put_file(token, dest_name, localfile)
    if ret is not None:
        print("Upload Success,url=", config.domin + dest_name)
    else:
        print("info=", info)
        print("ret=", ret)
    assert ret['key'] == dest_name
    assert ret['hash'] == etag(localfile)
Example #6
0
def loop_qiniu():
    while True:
        for f in os.listdir(UPLOAD_FOLDER):
            if 'tmp' not in f.split('_')[-1]:
                os.remove(os.path.join(UPLOAD_FOLDER, f))
                # print 'jump out and for one more time'
            elif 'tmp0' == f.split('_')[-1]:
                continue
            elif 'tmp' == f.split('_')[-1]:
                print 'start handle %s' % f
                f_path = os.path.join(UPLOAD_FOLDER, f)
                f_path_new = os.path.join(UPLOAD_FOLDER, f[:-4])
                key = f[:-4]
                q = Auth(access_key, secret_key)
                token = q.upload_token(Bucket, key, 3600)
                local_file = f_path
                ret, _ = put_file(token, key, local_file)
                try:
                    assert ret['key'] == key
                except TypeError:
                    print 'ret has error'
                    continue
                os.rename(f_path, f_path_new)
                print 'end handle %s' % key
                print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
            else:
                print '<----'
                print u'为考虑到的情况'
                print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
                print '---->'
    print 'pass this time while error', time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
def storage(file_data):
    try:
        #构建鉴权对象
        q = Auth(access_key, secret_key)

        #要上传的空间
        bucket_name = 'ihome'

        #上传到七牛后保存的文件名
        # key = 'my-python-logo.png';


        #生成上传 Token,可以指定过期时间等

        token = q.upload_token(bucket_name)

        #要上传文件的本地路径
        # localfile = './sync/bbb.jpg'
        # ret, info = put_file(token, key, localfile)
        ret, info = put_data(token, None, file_data)
    except Exception as e:
        logging.error(e)
        raise e
    print(ret)
    print("*"*16)
    print(info)
    # assert ret['key'] == key
    # assert ret['hash'] == etag(localfile)
    print(type(info))
    print(info.status_code)
    if 200 == info.status_code:
        return ret["key"]
    else:
        raise Exception("上传失败")
Example #8
0
File: upload.py Project: mitcc/gbox
def upload():
    # Get the name of the uploaded file
    file = request.files['file']
    # Check if the file is one of the allowed types/extensions
    if file and allowed_file(file.filename):
        # Make the filename safe, remove unsupported chars
        filename = secure_filename(file.filename)
        # Move the file form the temporal folder to
        # the upload folder we setup
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        # Redirect the user to the uploaded_file route, which
        # will basicaly show on the browser the uploaded file

        path = os.path.join(app.config['UPLOAD_FOLDER'], filename)

        access_key = ''
        secret_key = ''
        bucket_name = ''

        q = Auth(access_key, secret_key)
        token = q.upload_token(bucket_name, None, 3600)

        ret, info = qiniu.put_file(token, filename, path)

        return redirect(url_for('uploaded_file', filename=filename))
Example #9
0
def uploadsong(request):
    q = Auth('ToNLYIGLfHy5tpKSsRcBV2pw18b20LrYuBdvHaA_', 'rrD25c6RoHoMajmLR8lSz9wW4FcGEHvGMDL4l2zV')
    print q
    token = q.upload_token('outshineamazing', '')
    print token
    context = {'uptoken_url':token}
    return render(request,'comments/test.html',context)
Example #10
0
def uploadToQiNiu(filePath,name):
     q = Auth(access_key, secret_key)
     localfile = filePath
     key = name
     mime_type = "image/jpeg"
     token = q.upload_token(bucket_name, key)
     ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True)
Example #11
0
def test_upload():
    q = Auth(access_key, secret_key)
    token = q.upload_token(bucket, mykey.encode('utf-8'))
    file = '/tmp/abc.txt'
    ret, info = put_file(token, mykey.encode('utf-8'), file, mime_type="text/plain", check_crc=True)
    print(info)
    print(ret)
Example #12
0
    def post(self):
        for i in request.forms.keys():
            print i, request.forms[i], type(request.forms[i])

        for i in request.files.keys():
            print i, request.files[i], type(request.files[i])
            print request.files[i].__dict__

        image = request.files['image']
        name = image.raw_filename
        data = image.file.read()
        key = md5('data').hexdigest() + '.' + name.split('.')[-1]

        auth = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
        token = auth.upload_token(
            QINIU_BUCKET_NAME, None, 7200,
            {
                'returnBody': json.dumps({
                    'key': '$(key)',
                    'hash': '$(etag)',
                    'format': '$(imageInfo.format)',
                    'width': '$(imageInfo.width)',
                    'height': '$(imageInfo.height)',
                }),
                'save_key': '$(etag)',
            }
        )
        ret, info = put_data(token, key, data)
Example #13
0
class QiNiuProvider(object):
    def __init__(self, access_key, secret_key, bucket_name, imageServerUrl):
        self.access_key = access_key
        self.secret_key = secret_key
        self.bucket_name = bucket_name
        self.imageServerUrl = imageServerUrl
        self.credentials = Auth(self.access_key, self.secret_key)

    def token(self):
        return self.credentials.upload_token(self.bucket_name)

    def store(self, raw):
        key = hash(str(uuid.uuid1()))
        upload_token = self.credentials.upload_token(self.bucket_name, key)
        ret, err = put_data(upload_token, key, raw)
        if ret is not None:
            return "%s/%s" % (self.imageServerUrl, ret['key'])
        else:
            logging.error('upload error.')

    def store_file(self, file_path, file_name):
        upload_token = self.credentials.upload_token(self.bucket_name, file_name)
        ret, err = put_file(upload_token, file_name, file_path)
        if ret is not None:
            return "%s/%s" % (self.imageServerUrl, ret['key'])
        else:
            logging.error('upload: %s error.' % file_name)
Example #14
0
def tuchuang_index():
    github_user = _get_user()

    if not github_user:
        flash(u'请正确完成牛逼参数设置后上传图片!', category='warning')
        return redirect(url_for('.info'))

    if request.method == 'POST':
        access_key = str(github_user.get('access_key'))
        secret_key = str(github_user.get('secret_key'))
        bucket_name = str(github_user.get('bucket_name'))
        domain_name = str(github_user.get('domain_name'))
        q = Auth(access_key, secret_key)
        token = q.upload_token(bucket_name)

        upload_files = request.files.getlist('file')
        for upload_file in upload_files:
            key = '%s_%s' % (datetime.now().isoformat(), upload_file.filename)
            ret, info = put_data(up_token=token, key=key, data=upload_file)
            url = '%s/%s' % (domain_name, key)
            f = File()
            f.set('url', url)
            f.set('user', github_user)
            f.save()
        flash(u'成功上传%s张照片!' % len(upload_files), category='success')
        return redirect(url_for('.tuchuang_index'))

    image_id = request.args.get('image_id')
    image = Query(File).get(image_id) if image_id else None

    return render_template('tuchuang.html', image=image)
Example #15
0
def upload(filepath):
    qiniu_domain =  'http://7xtq0y.com1.z0.glb.clouddn.com/'
    #需要填写你的 Access Key 和 Secret Key
    access_key = 'NhxOewLDWpAs_THJNvtKN8kZHG3r0_tkWOaJSycc'
    secret_key = 'Cx7AJItEyNWaEG1eLvu85PpCGq0vAaX1xmvJC7c0'

    #构建鉴权对象
    q = Auth(access_key, secret_key)

    #要上传的空间
    bucket_name = 'images'

    #上传到七牛后保存的文件名
    key = os.path.basename(filepath)
    qiniu_url = qiniu_domain + key

    #生成上传 Token,可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    localfile = filepath

    ret, info = put_file(token, key, localfile)
    print(info)
    print('===============')
    print(qiniu_url)

    return qiniu_url
Example #16
0
def upload_file(file_name=None, data=None):
    if not file_name:
        file_name = hashlib.md5(data).hexdigest()
    q = Auth(settings.QINIU_KEY, settings.QINIU_TOKEN)
    token = q.upload_token(settings.QINIU_BUCKET)
    ret, info = put_data(token, file_name, data)
    return ret['key'] == file_name, file_name
Example #17
0
def token(request):
    # wiicome服务器设置七牛上传token
    q = Auth(ACCESS_KEY, SECRET_KEY)
    # 上传策略仅指定空间名和上传后的文件名,其他参数仅为默认值
    token = q.upload_token(BUCKET_KEY)
    data = {"uptoken": token}
    return JsonResponse(data)
Example #18
0
    def create(self):
        # TODO: 表单验证
        image = request.files['image']
        # TODO: 解包检查
        origin_name, suffix = image.raw_filename.split('.')

        data = image.file.read()
        data_hash = md5(data).hexdigest()
        key = data_hash

        model = ImageModel.query.get(data_hash)
        if model:
            return model.as_dict()

        auth = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
        token = auth.upload_token(
            QINIU_BUCKET_NAME, None, 7200,
            {
                'returnBody': json.dumps({
                    'key': '$(key)',
                    'hash': '$(etag)',
                    'format': '$(imageInfo.format)',
                    'width': '$(imageInfo.width)',
                    'height': '$(imageInfo.height)',
                }),
                'save_key': '$(etag)',
            }
        )
        ret, info = put_data(token, key, data)
        model = ImageModel(
            hashkey=data_hash, suffix=ret['format'],
            width=ret['width'], height=ret['height'])
        db.session.add(model)
        db.session.commit()
        return model.as_dict()
Example #19
0
def main():
	q = Auth('FCFQs6B-thjgt30-HEmCS9ZUCGQBxx2Zsg_WO1k5', 'Z8LCTm4gxo_dfX7HT0EhFnXmsFTGwZ8MyCFXmSXF')

	# 上传策略仅指定空间名和上传后的文件名,其他参数仅为默认值
	token = q.upload_token('qile')
	f = open('qiniu_auth_token.txt', 'w')
	f.write(str(token))
Example #20
0
def generate_upload_token(prefix):
    '''
    prefix: 'head' or 'content'
    return: (token, key)
    '''

    # modify default upload zone
    qiniu.config.set_default(default_zone=qiniu.config.zone1)

    # access two keys
    access_key = current_app.config['QINIU_ACCESS_KEY']
    secret_key = current_app.config['QINIU_SECRET_KEY']
    # auth obj
    q = Auth(access_key, secret_key)
    # upload bucket name
    bucket_name = current_app.config['QINIU_BUCKET_NAME']

    # achieve file ext name
    try:
        match = re.finditer(r'.([0-9a-zA-Z]+)$', localfile)
        ext = match[-1].group(1)
    except:
        ext = 'jpg'

    # cloud filename
    # key = generate_upload_filename(ext, prefix=prefix)
    # generate token
    token = q.upload_token(bucket_name, None, 3600)

    return token
Example #21
0
 def get(self, request):
     q = Auth(settings.QNACCESSKEY, settings.QNSECRETKEY)
     filename = str(uuid.uuid1()).replace("-", "") + ".jpg"
     print (filename)
     token = q.upload_token(settings.QNBUKET, filename)
     print (token)
     return Response({"key": filename, "token": token}, status=status.HTTP_202_ACCEPTED)
Example #22
0
class QiNiu(object):

    def __init__(self):
        self.authed = Auth(AccessKey, SecretKey)

    def upload(self, data):
        """
        :param data:
                {
                    "filename":"",
                    "filepath":"",
                    "policy":{}
                }
        :return:
            ret
            info
        """

        filename = data["filename"]
        filepath = data["filepath"]
        policy   = data.get("policy", {})

        if policy:
            token = self.authed.upload_token(BucketName, filename, Expired, policy)
        else:
            token = self.authed.upload_token(BucketName, filename, Expired)

        ret, info = put_file(token, filename, filepath)

        return ret, info
Example #23
0
class Qiniu(object):
    def init_app(self, app):
        access_key = app.config.get('QINIU_ACCESS_KEY')
        secret_key = app.config.get('QINIU_SECRET_KEY')
        self.app = app
        self.bucket = app.config.get('QINIU_BUCKET')
        self.auth = Auth(access_key, secret_key)

    def upload_file(self, filename, filepath):
        """上传本地文件"""
        token = self.auth.upload_token(self.bucket, filename)
        ret, info = put_file(token, filename, filepath)

        if info.exception is not None:
            raise UploadError(info)

    def upload_data(self, filename, data):
        """上传二进制数据"""
        token = self.auth.upload_token(self.bucket, filename)
        ret, info = put_data(token, filename, data)

        if info.exception is not None:
            raise UploadError(info)

    def generate_token(self, filename=None, policy=None):
        """生成上传凭证"""
        return self.auth.upload_token(self.bucket, filename, policy=policy)
Example #24
0
def upload_token():
    q = Auth(ACCESS_KEY, SECRET_KEY)
    # 上传策略仅指定空间名和上传后的文件名,其他参数仅为默认值
    token = q.upload_token('wm-test', None, 7200, {'callbackUrl':"http://298074.cicp.net:59295/upload/callback/", 'callbackBody':"name=$(key)&fname=$(fname)&hash=$(etag)&size=$(imageInfo.width)x$(imageInfo.height)", \
                                                   'saveKey':'aaa$(year)$(mon)$(day)$(hour)$(min)$(sec).jpg', 
                                                   'mimeType':'image/jpg'})
    return token
Example #25
0
def upload_file(localfile):
    # 需要填写你的 Access Key 和 Secret Key
    access_key = 'aRzVj18_VFA_p4EZ9z8ClWkVwYvAOWuoMStYnJhi'
    secret_key = 'bh_hBotfph77wcmHHIgCwExqKEvQN_eyT5m1r9_c'

    # 构建鉴权对象
    q = Auth(access_key, secret_key)

    # 要上传的空间
    bucket_name = 'myselfres'

    # 上传到七牛后保存的文件名

    key = time.strftime("%Y-%m-%d-%H-%M-%S.png", time.localtime())
    print key

    # 生成上传 Token,可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    # 要上传文件的本地路径

    ret, info = put_file(token, key, localfile)
    print (info)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)

    pngOnlinePath = myselfres_dom + key + ')'
    setText(pngOnlinePath)
Example #26
0
def get_token(key):
    q = Auth(qiniu_access_key, qiniu_secret_key)

    # 上传策略仅指定空间名和上传后的文件名,其他参数仅为默认值
    token = q.upload_token(qiniu_bucket_name, key)

    return token
Example #27
0
def putfile(file_url):
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)
    mime_type, file_type = get_mime_type(file_url)
    key = etag(file_url) + "." + file_type  # "test"
    token = q.upload_token(bucket_name, key)
    ret, info = put_file(token, key, file_url, mime_type=mime_type, check_crc=True)
    print ret
Example #28
0
def upload(key):
    access_key = 'Fx9CvaSHqUFVrofBgpGxAdsR3UV0SNB2bRof6ss2'
    secret_key = 'eW8fX00uUZukKcFsZyeIWw3BTyfhyoIhGQ4j5NwN'
    q = Auth(access_key, secret_key)
    bucket_name = 'bing'
    token = q.upload_token(bucket_name, key, 3600)
    localfile = key
    put_file(token, key, localfile)
Example #29
0
def upload_qiniu(path, upload_name):
    ''' upload file to qiniu'''
    q = Auth(config['ak'], config['sk'])
    key = upload_name  # upload to qiniu's markdown dir

    token = q.upload_token(config['bucket'], key)
    ret, info = put_file(token, key, path, check_crc=True)
    return ret != None and ret['key'] == key
Example #30
0
def upload(prefix, key, localpath):

    from qiniu import Auth, put_file
    q = Auth(ACCESS_KEY, SECRET_KEY)

    key = os.path.join(prefix, key)
    token = q.upload_token(BUCKET_NAME, key)
    ret, info = put_file(token, key, localpath)
Example #31
0
class UploaderTestCase(unittest.TestCase):
    mime_type = "text/plain"
    params = {'x:a': 'a'}
    q = Auth(access_key, secret_key)

    def test_put(self):
        key = 'a\\b\\c"hello'
        data = 'hello bubby!'
        token = self.q.upload_token(bucket_name)
        ret, info = put_data(token, key, data)
        print(info)
        assert ret['key'] == key

    def test_put_crc(self):
        key = ''
        data = 'hello bubby!'
        token = self.q.upload_token(bucket_name, key)
        ret, info = put_data(token, key, data, check_crc=True)
        print(info)
        assert ret['key'] == key

    def test_putfile(self):
        localfile = __file__
        key = 'test_file'

        token = self.q.upload_token(bucket_name, key)
        ret, info = put_file(token,
                             key,
                             localfile,
                             mime_type=self.mime_type,
                             check_crc=True)
        print(info)
        assert ret['key'] == key
        assert ret['hash'] == etag(localfile)

    def test_putInvalidCrc(self):
        key = 'test_invalid'
        data = 'hello bubby!'
        crc32 = 'wrong crc32'
        token = self.q.upload_token(bucket_name)
        ret, info = _form_put(token, key, data, None, None, crc=crc32)
        print(info)
        assert ret is None
        assert info.status_code == 400

    def test_putWithoutKey(self):
        key = None
        data = 'hello bubby!'
        token = self.q.upload_token(bucket_name)
        ret, info = put_data(token, key, data)
        print(info)
        assert ret['hash'] == ret['key']

        data = 'hello bubby!'
        token = self.q.upload_token(bucket_name, 'nokey2')
        ret, info = put_data(token, None, data)
        print(info)
        assert ret is None
        assert info.status_code == 403  # key not match

    def test_withoutRead_withoutSeek_retry(self):
        key = 'retry'
        data = 'hello retry!'
        set_default(default_zone=Zone('http://a', 'http://upload.qiniup.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_putData_without_fname(self):
        if is_travis():
            return
        localfile = create_temp_file(30 * 1024 * 1024)
        key = 'test_putData_without_fname'
        with open(localfile, 'rb') as input_stream:
            token = self.q.upload_token(bucket_name)
            ret, info = put_data(token, key, input_stream)
            print(info)
            assert ret is not None

    def test_putData_without_fname1(self):
        if is_travis():
            return
        localfile = create_temp_file(30 * 1024 * 1024)
        key = 'test_putData_without_fname1'
        with open(localfile, 'rb') as input_stream:
            token = self.q.upload_token(bucket_name)
            ret, info = put_data(token, key, input_stream, self.params,
                                 self.mime_type, False, None, "")
            print(info)
            assert ret is not None

    def test_putData_without_fname2(self):
        if is_travis():
            return
        localfile = create_temp_file(30 * 1024 * 1024)
        key = 'test_putData_without_fname2'
        with open(localfile, 'rb') as input_stream:
            token = self.q.upload_token(bucket_name)
            ret, info = put_data(token, key, input_stream, self.params,
                                 self.mime_type, False, None, "  ")
            print(info)
            assert ret is not None
Example #32
0
# -*- encoding: utf-8 -*-
"""
@File    : upload_qiniuyun.py
@Time    : 2020/5/28 10:14
@Author  : chen
上传本地文件到七牛云功能:utils/upload_qiniuyun.py
"""
from qiniu import Auth, put_file, etag
import qiniu.config

# 需要填写你的 Access Key 和 Secret Key
access_key = 'F6TFlLqmX4Jxi_OJ86xLVCB8mQ5KRsyzCjGVWPEh'
secret_key = 'zhCb8cNSR-lifyVCZLPjH3GhD4_W7P5Sgbh9mHah'

# 构建鉴权对象
q = Auth(access_key, secret_key)

# 要上传的空间
bucket_name = 'chen0406'

# 上传后保存的文件名
key = 'my-python-logo.png'

# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)  # 生成token,用于项目上传使用

# 要上传文件的本地路径
localfile = r'E:\ENV\flask项目-cBMOsSmb\Flask项目实战-BBS\static\common\images\logo.png'

ret, info = put_file(token, key, localfile)
# print(info)
Example #33
0
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from . import models
from qiniu import Auth, put_file

AK = 'Be4yzMaDNQenNaMcAHWMkLWTk05uyyRmL9yW-FMp'
SK = 'uROOPs_pDB_gRsCn4OI_2nGDvK2HhrQ0UBdrbUw2'
q = Auth(AK, SK)

# 要上传的空间
bucket_name = 'flower'


@csrf_exempt
def user_login(request):
    if request.method == 'POST':
        print(request.POST)
        try:
            user = models.User.objects.get(
                email=request.POST.get('email'),
                password=request.POST.get('password'))
        except models.User.DoesNotExist:
            user = None
        if user:
            return JsonResponse({
                'success': True,
                'user': {
                    'user_id': user.user_id,
                    'username': user.username,
                    'email': user.email,
                    'phone_number': user.phone_number,
Example #34
0
#!/usr/bin/env python
# coding: utf-8
__author__ = 'scdev1003'

import re
import os
import json
from qiniu import Auth
from qiniu import BucketManager
from flask import Flask, jsonify, request

ak = os.environ["QINIUAK"]
sk = os.environ["QINIUSK"]

q = Auth(ak, sk)

bucket = BucketManager(q)
app = Flask(__name__)


@app.route('/')
def index():
    callback = request.args.get('callback', '')
    bucket_name = 'packtpub-books'
    res = bucket.list(bucket_name)
    books = {}
    for item in res[0].get('items', []):
        key = '.'.join(item.get('key', '').split('.')[0:-1])
        name = re.search(r'^\d{4}\/\d{2}\/\d{2}\/(.*)$', key).group(1)
        date = re.search(r'^(\d{4}\/\d{2}\/\d{2})\/.*$', key).group(1)
        if books.get(date, None) == None:
Example #35
0
 def __init__(self, access_key, secret_key, bucket_name, prefix, download):
     self.bucket_name = bucket_name
     self.prefix = prefix
     self.download = download
     self.q = Auth(access_key, secret_key)
Example #36
0
#!/usr/bin/env python
# -*-coding:utf-8-*-

import uuid
from qiniu import Auth, put_file, etag, urlsafe_base64_encode

QINIU_ACCESS_KEY = '0jAQdT6nr4h5M6jj_kqpg9SHY5L64OU9wQ2NJoNs'
QINIU_SECRET_KEY = 'Qttd5PDhmhrOGYrNBUp0tXMBqHkdyw-S9gdXWkYA'
QINIU_BUCKET_NAME = 'avatar'
QINIU_BUCKET_DOMAIN = '7xsgma.com1.z0.glb.clouddn.com'

q = Auth(access_key=QINIU_ACCESS_KEY, secret_key=QINIU_SECRET_KEY)

bucket_name = 'avatar'

key = 'head'
token = q.upload_token(bucket_name, key, 3600)

localfile = 'head.jpg'

put_file(token, key, localfile)

print "http://7xsgma.com1.z0.glb.clouddn.com/" + key
Example #37
0
    def GetMsg(self):
        """
        获取我们想要的商品信息
        :return:
        """
        ShoeTitle = []

        reql, publishedDate, OldPublishTime = self.GetHtml(), '', ''

        ShoesList = reql['threads']

        country = 'SNKRS中国'        #国家

        sql1 = 'select distinct productid from monitor_result where distributionid=1 and `status`=2'

        try:
            self.Pass_china_cur1.execute(sql1)

        except Exception as e:
            print('查询错误:{}'.format(e))

        SkuList = self.Pass_china_cur1.fetchall()

        OldPublishTime = [i[0] for i in SkuList]


        for i in range(len(ShoesList)):

            seoSlug = ShoesList[i]['seoSlug']

            TheLinkadDress = 'https://www.nike.com/cn/launch/t/'+seoSlug

            if ShoesList[i]['id'] not in OldPublishTime:

                if 'title' not in ShoesList[i]['product']:

                    title = str(ShoesList[i]['name']).replace('\"', '').replace('\'', '')
                    imageUrl = ShoesList[i]['imageUrl']
                    ShoesSku = str(ShoesList[i]['product']['style']) + '-' + str(ShoesList[i]['product']['colorCode'])

                    if ShoesSku not in ['BQ4800-100', 'CD9329-001', 'AV4052-100', 'CK1905-100', 'CK1907-600', 'AO3189-001', 'AO3189-100']:

                        if title != 'SNKRS Pass':
                            Additional_information = '发售'
                        else:
                            Additional_information = 'Pass'

                        startSellDate = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")                        #开始销售时间

                        productId = ShoesList[i]['id']

                        access_key = '92SPonlDvYbu91VZOxvrHUmc9pHi3B8Wy5PUlzQ8'
                        secret_key = 'HZ8SyNcZr7IRRXQkrNwk0v17BsHkCeW9bsriikQZ'
                        q = Auth(access_key, secret_key)
                        bucket_name = 'instagram_img'

                        key = '{}.jpg'.format(productId)

                        token = q.upload_token(bucket_name, key, 3600)

                        r = requests.get(imageUrl, timeout=5)

                        with open('/root/snker_crawler/img/beauty_1.jpg', 'wb') as f:
                            f.write(r.content)

                        localfile = '/root/snker_crawler/img/beauty_1.jpg'
                        ret, info = put_file(token, key, localfile)
                        Img_url = 'http://putu4ibve.bkt.clouddn.com/' + json.loads(info.text_body).get('key')
                        assert ret['key'] == key
                        assert ret['hash'] == etag(localfile)

                        pushid = int(round(time.time() * 1000))

                        sql_1 = """INSERT INTO monitor_result (title, sku, distributionchannels, replenishmenttype, pushtime, picurl, `status`, createtime, distributionid, linkurl, productid) VALUES("{}", "{}", 'SNKRS中国', '{}', '{}', '{}', 2, now(), 1, '{}', '{}')""".format(title, ShoesSku, Additional_information, startSellDate, Img_url, TheLinkadDress, productId)

                        try:
                            self.Pass_china_cur1.execute(sql_1)

                        except Exception as e:
                            print('插入错误:{}'.format(e))

                        self.Pass_china_conn1.commit()

                        Callbacdata = {
                            "title": "{}".format(title),
                            "sku": "{}".format(ShoesSku),
                            "distributionchannels": "{}".format(country),
                            "replenishmenttype": "{}".format(Additional_information),
                            "pushtime": "{}".format(startSellDate),
                            "picurl": "{}".format(Img_url),
                            "size": "",
                            "status": "0",
                            "createtime": "{}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
                            "distributionid": "1",
                            "linkurl": "{}".format(TheLinkadDress),
                            "productid": "{}".format(productId),
                            "sortnum": "1",
                            "pushstatus": "0",
                            "pushid": "{}".format(pushid)
                        }

                        Callbacheader = {'Content-Type': 'application/json'}

                        now_time = datetime.datetime.now()
                        dt_minus1day1 = (now_time + datetime.timedelta(seconds=-2)).strftime('%Y-%m-%d %H:%M:%S')
                        dt_minus1day2 = (now_time + datetime.timedelta(seconds=+2)).strftime('%Y-%m-%d %H:%M:%S')

                        sql_7 = "SELECT * FROM monitor_result WHERE linkurl='{}' AND createtime BETWEEN '{}' AND '{}'".format(TheLinkadDress, dt_minus1day1, dt_minus1day2)

                        try:
                            self.Pass_china_cur.execute(sql_7)
                        except Exception as E:
                            print(E)

                        Grab_judgment = self.Pass_china_cur.fetchall()

                        if len(Grab_judgment) == 0:

                            reqls = requests.post('http://mapi.eyee.com/capi/community/monitor/open/push', data=json.dumps(Callbacdata), headers=Callbacheader, timeout=5)
                            # reqls = requests.post('http://stest.eyee.com/capi/community/monitor/open/push', data=json.dumps(Callbacdata), headers=Callbacheader, timeout=5)

                            with open('/root/push/passChina.log', 'a') as d:
                                d.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + str(reqls.text) + str(pushid))
                                d.write('\n')


        if len(ShoeTitle) > 0:

            self.Pass_china_cur.close()
            self.Pass_china_conn.close()

            self.Pass_china_cur1.close()
            self.Pass_china_conn1.close()
            print('发送保存成功')
Example #38
0
def create_token(bucket_name, key, policy=None):
    access_key = qn_access_key
    secret_key = qn_secret_key
    q = Auth(access_key, secret_key)
    token = q.upload_token(bucket_name, key, 3600, policy)
    return token
Example #39
0
from django.conf import settings
from qiniu import Auth

q = Auth(settings.ENV_QINIU_AK, settings.ENV_QINIU_SK)


def get_qiniu_token(prefix: str = '',
                    bucket_name: str = settings.ENV_QINIU_DEFAULT_BUCKET):
    """获取七牛TOKEN

    Doc:
        http://developer.qiniu.com/docs/v6/api/reference/security/put-policy.html

    :param prefix:
    :param bucket_name: 要上传的空间
    :return:
    """
    prefix = prefix.strip('/')
    if prefix:
        save_key = f"{prefix}/$(etag)$(ext)"
    else:
        save_key = '$(etag)$(ext)'
    base_url = settings.QINIU_BUCKET_URL_MAP.get(bucket_name, '')

    policy = {
        'saveKey':
        save_key,
        'returnBody':
        """{{
            "hash": $(hash),
            "key": $(key),
Example #40
0
 def post(self, request, **kwargs):
     """七牛图片上传Token获取"""
     q = Auth(access_key, secret_key)
     token = q.upload_token(bucket_name, expires=3600)
     return Response(token)
Example #41
0
 def test_noKey(self):
     with pytest.raises(ValueError):
         Auth(None, None).token('nokey')
     with pytest.raises(ValueError):
         Auth('', '').token('nokey')
Example #42
0
    urlopen = urllib.urlopen
elif is_py3:
    import io
    import urllib

    StringIO = io.StringIO
    urlopen = urllib.request.urlopen

access_key = os.getenv('QINIU_ACCESS_KEY')
secret_key = os.getenv('QINIU_SECRET_KEY')
bucket_name = os.getenv('QINIU_TEST_BUCKET')
hostscache_dir = None

dummy_access_key = 'abcdefghklmnopq'
dummy_secret_key = '1234567890'
dummy_auth = Auth(dummy_access_key, dummy_secret_key)


def rand_string(length):
    lib = string.ascii_uppercase
    return ''.join([random.choice(lib) for i in range(0, length)])


def create_temp_file(size):
    t = tempfile.mktemp()
    f = open(t, 'wb')
    f.seek(size - 1)
    f.write(b('0'))
    f.close()
    return t
Example #43
0
def upload_Token():
    access_key = "UP4lyUo3aBJPr2YBIv7x-BmV83mTd6hczJS0bbkl"
    secret_key = "W7MacbJiXJPsSXn-H12aqN-WsZEokxBAW8ZaXDDD"
    q = Auth(access_key, secret_key)
    token = q.upload_token('sitp')
    return token
Example #44
0
# -*- coding: utf-8 -*-
# flake8: noqa

import requests
from qiniu import Auth

access_key = '...'
secret_key = '...'

q = Auth(access_key, secret_key)
bucket_domain = "..."
key = "..."

#有两种方式构造base_url的形式
base_url = 'http://%s/%s' % (bucket_domain, key)

#或者直接输入url的方式下载
base_url = 'http://domain/key'

#可以设置token过期时间
private_url = q.private_download_url(base_url, expires=3600)

print(private_url)
r = requests.get(private_url)
assert r.status_code == 200
Example #45
0
# -*- coding: utf-8 -*-

from instagram import app
from qiniu import Auth, put_stream, put_data, put_file, etag
import qiniu.config
import os

# 需要填写你的 Access Key 和 Secret Key
access_key = app.config['QINIU_ACCESS_KEY']
secret_key = app.config['QINIU_SECRET_KEY']
# 构建鉴权对象
q = Auth(access_key, secret_key)
# 要上传的空间
bucket_name = app.config['QINIU_BUCKET_NAME']
domain_prefix = app.config['QINIU_DOMAIN']


def qiniu_upload_file(source_file, save_file_name):
    # 生成上传 Token,可以指定过期时间等
    token = q.upload_token(bucket_name, save_file_name)

    ret, info = put_data(token, save_file_name, source_file.stream)

    print(type(info.status_code), info)
    if info.status_code == 200:
        return domain_prefix + save_file_name
    return None
Example #46
0
class QiniuClient(object):
    def __init__(self, access_key, secret_key, bucket_name):
        self.client = Auth(access_key, secret_key)
        self.bucket_name = bucket_name
        self.bucket = BucketManager(self.client)

    def set_bucket_name(self, bucket_name):
        """重设bucketname"""
        self.bucket_name = bucket_name

    def upload_file(self, filename, file_io=None):
        """上传文件
        接受文件名或者IO形式的数据

        :filename:
            可以是文件系统中的文件路径,也可以直接使用文件名称,
            最后会根据是否带`file_io`参数来决定是否需要读取文件。
        :file_io:
            文件IO对象,如果带有这个参数则直接将其上传。
        :return:
            - error: 是否有错误
            - exception: 错误原因
            - hash: 上传文件的hash
            - key: 上传文件的key
        """
        if file_io is None:
            file_io = BytesIO()
            with open(filename, 'rb') as f:
                file_io.write(f.read())
        filename = filename.rsplit("/")[-1]

        try:
            token = self.client.upload_token(self.bucket_name, filename)
            result, info = put_data(token, filename, file_io.getvalue())
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        else:
            return {
                "error": info.status_code != 200,
                "exception": info.exception,
                "hash": result.get('hash', None),
                "key": result.get('key', None)
            }

    def file_delete(self, filename):
        """删除指定的文件"""
        try:
            result, info = self.bucket.delete(self.bucket_name, filename)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        else:
            return {
                'error': info.status_code != 200,
                'exception': info.exception
            }

    def file_list(self, prefix=None, delimiter=None, marker=None, limit=None):
        """获取文件列表,提供了若干选项供筛选文件

        :param prefix: 前缀
        :param delimiter: 分隔符
        :param marker: 标记
        :param limit: 条目数量
        :return:
            - error: 是否有错误
            - exception: 错误的原因
            - items: 文件数据列表
                - key
                - hash
                - fsize
                - mimeType
                - putTime
                - type
                - status
        """
        try:
            result, info = self.bucket.list(self.bucket_name, prefix, marker,
                                            limit, delimiter)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        else:
            return {
                'error': info.status_code != 200,
                'exception': info.exception,
                'items': result.get('items', None)
            }

    def stat_info(self, filename):
        """获取文件信息

        :return: 同上
        """
        try:
            result, info = self.bucket.stat(self.bucket_name, filename)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        return {
            'error': info.status_code != 200,
            'exception': info.exception,
            'fsize': result.get('fsize', None),
            'hash': result.get('hash', None),
            'mimeType': result.get("mimeType", None),
            'putTime': result.get("putTime", None),
            'type': result.get('type', None)
        }

    def batch_stat(self, filenames):
        """批量获取文件信息

        :return: 同上
        """
        try:
            ops = build_batch_stat(self.bucket_name, filenames)
            result, info = self.bucket.batch(ops)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        else:
            return {
                'error': info.status_code != 200,
                'exception': info.exception,
                'items': result.get('items', None)
            }

    def batch_delete(self, filenames):
        """批量删除文件
        """
        try:
            ops = build_batch_delete(self.bucket_name, filenames)
            result, info = self.bucket.batch(ops)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        else:
            return {
                'error': info.status_code != 200,
                'exception': info.exception
            }

    def fetch(self, url, filename):
        """抓去网络资源到空间"""
        try:
            result, info = self.bucket.fetch(url, self.bucket_name, filename)
        except Exception as exc:
            return {'error': True, 'exception': str(exc)}
        return {'error': info.status_code != 200, 'exception': info.exception}
Example #47
0
class QiniuWrap(object):
    def __init__(self):

        configer = Configer('')

        access_key = configer.get_configer('QINIU', 'access_key')
        secret_key = configer.get_configer('QINIU', 'secret_key')

        self.q = Auth(access_key, secret_key)
        self.bucket = BucketManager(self.q)

    def transcode_h264(self, bucket_name, key):

        file_tail = ['ori.mp4', '.mp4']

        for type in file_tail:

            des_filename = key.replace(type, 'h264.mp4')

            if des_filename is not None:
                break

        op = op_save(
            'avthumb/mp4/ab/32k/aq/10/ar/44100/acodec/libfaac/r/25/vb/260k/vcodec/libx264/s/640x360/autoscale/1/stripmeta/0',
            bucket_name, des_filename)

        ops = []
        ops.append(op)
        self.pfop = PersistentFop(self.q, bucket_name, 'videoconverth264')
        ret, info = self.pfop.execute(key, ops, 1)

        if 200 == info.status_code:
            return 0
        else:

            return info.text_body[10:-2]

    def download_pub_file(self, bucket_name, key, path='.'):

        base_url = 'http://%s.okjiaoyu.cn/%s' % (bucket_name, key)
        request = urllib2.Request(base_url)
        response = urllib2.urlopen(request)
        pic = response.read()

        with open(path + '/' + key, 'wb') as fd:
            fd.write(pic)

        return response.code

    def download_pri_file(self, bucket_name, key):

        base_url = 'http://%s.okjiaoyu.cn/%s' % (bucket_name, key)
        private_url = q.private_download_url(base_url, expires=3600)
        request = urllib2.Request(base_url)
        response = urllib2.urlopen(request)
        pic = response.read()

        with open(path + '/' + key, 'wb') as fd:
            fd.write(pic)

        return response.code

    def upload_file(self, bucket_name, key, localfile):

        if bucket_name is None or key is None or localfile is None:
            return 'sys error'

        token = self.q.upload_token(bucket_name, key)

        ret, info = put_file(token, key, localfile)

        if 200 == info.status_code:
            return None
        else:
            return info.text_body[10:-2]

    def upload_data(self, bucket_name, key, data):

        if bucket_name is None or key is None or data is None:
            return 'sys error'

        token = self.q.upload_token(bucket_name, key)

        ret, info = put_data(token, key, data)

        if 200 == info.status_code:
            return None
        else:
            return info.text_body[10:-2]

    def get_file_info(self, bucket_name, key):
        bucket_name = bucket_prex + bucket_name
        ret, info = bucket.stat(bucket_name, key)
        return info

    def copy_file(self, frm_bucket_name, frm_key, to_bucket_name, to_key):
        frm_bucket_name = bucket_prex + frm_bucket_name
        to_bucket_name = bucket_prex + to_bucket_name
        ret, info = bucket.copy(frm_bucket_name, frm_key, to_bucket_name,
                                to_key)
        return ret

    def move_file(self, frm_bucket_name, frm_key, to_bucket_name, to_key):
        frm_bucket_name = bucket_prex + frm_bucket_name
        to_bucket_name = bucket_prex + to_bucket_name
        ret, info = bucket.move(frm_bucket_name, frm_key, to_bucket_name,
                                to_key2)
        return ret

    def del_file(self, bucket_name, key):
        bucket_name = bucket_prex + bucket_name
        ret, info = bucket.delete(bucket_name, key)
        return ret

    def get_uptoken(self, bucket_name, key):

        return self.q.upload_token(bucket_name, key)
Example #48
0
 def __init__(self, access_key, secret_key, bucket_name):
     self.client = Auth(access_key, secret_key)
     self.bucket_name = bucket_name
     self.bucket = BucketManager(self.client)
Example #49
0
import math
import os

from flask import Flask, request, jsonify
import json
import uuid

from werkzeug.utils import secure_filename
from sqlalchemy import or_
from model import User, db, Pet, Collection, Admin
from flask.ext.cors import CORS
# from flask_cors import CORS
from settings import *
from qiniu import Auth, put_file, etag, urlsafe_base64_encode

q = Auth(access_key=QINIU_ACCESS_KEY, secret_key=QINIU_SECRET_KEY)
app = Flask(__name__)

CORS(app)


@app.route('/')
def hello_world():
    info = {'result': 0, 'msg': "成功", 'data': None}
    return jsonify(info)


"""
    用户注册接口
    {
        "phone":"15705213522",
from qiniu import Auth, put_file, etag

from config import QINIU_ACCESS_KEY, QINIU_SECRET_KEY

# 构建鉴权对象
q = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
# 要上传的空间
bucket_name = 'corley-images'
# 上传后保存的文件名
key = 'logo.png'
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
# 要上传文件的本地路径
localfile = 'E:\Test\logo.gif'
ret, info = put_file(token, key, localfile)
print('ret :', ret)
print('info:', info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
Example #51
0
#coding:utf8
from qiniu import Auth, put_file, etag, urlsafe_base64_encode, put_data
import qiniu.config
#需要填写你的 Access Key 和 Secret Key
access_key = 'uamqojf_BxYBinVjndycNhWYRXgBukLbMtdNEUXZ'
secret_key = 'I4f473dX6m3r-mTpHUKtHv1_Khy7WH1VE3Vg1SHN'

#构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'image1'

#生成上传 Token,空间名称,名字不传由七牛云维护, 可以指定过期时间等
token = q.upload_token(bucket_name, None, 3600)


def image_storage(image_data):

    # 上传图片
    ret, info = put_data(token, None, image_data)

    #判断图片是否有上传成功
    if info.status_code == 200:
        return ret.get("key")
    else:
        return "上传失败"


# if __name__ == '__main__':
#
Example #52
0
# -*-coding:utf-8-*-
from qiniu import Auth, BucketManager
from apps.core.plug_in.config_process import get_plugin_config, import_plugin_config
from apps.plugins.qiniu_cloud_plugin.config import PLUGIN_NAME, CONFIG
from apps.plugins.qiniu_cloud_plugin.upfile_cloud import qiniu_upload, qiniu_file_del, qiniu_file_rename, get_file_url,\
    qiniu_copy

__author__ = "Allen Woo"

# 初始化
import_plugin_config(PLUGIN_NAME, CONFIG)
qiniu = Auth(get_plugin_config(PLUGIN_NAME, "ACCESS_KEY"),
             get_plugin_config(PLUGIN_NAME, "SECRET_KEY"))


def main(**kwargs):
    '''
    主函数
    :param kwargs:
        action: 动作
    :return:
    '''
    bucket = BucketManager(qiniu)

    if kwargs.get("action") == "upload":
        data = qiniu_upload(qiniu, **kwargs)

    elif kwargs.get("action") == "copy_file":
        data = qiniu_copy(bucket, **kwargs)

    elif kwargs.get("action") == "delete":
Example #53
0
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from .models import db
from qiniu import Auth
import os

access_key = 'qHyZn42sB0ZU2X6rYwugx3rgjDnG1SZD-5c01F5y'
secret_key = 'YERUm0zdzKul3mPq-u_8GHzjye98HiD7eeDygpi7'
bucket_name = 'c2cweb'
bucket_domain = 'pbn2nc8d5.bkt.clouddn.com'

qiniu_handle = Auth(access_key, secret_key)
policy = {}

app = Flask(__name__, static_url_path='')
app.config.from_object('config')
app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024
db.init_app(app)

from app import views
Example #54
0
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag

access_key = '...'
secret_key = '...'

q = Auth(access_key, secret_key)

bucket_name = 'Bucket_Name'

key = 'my-python-logo.png'

#上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器。
policy = {
    'callbackUrl': 'http://your.domain.com/callback.php',
    'callbackBody': 'filename=$(fname)&filesize=$(fsize)'
}

token = q.upload_token(bucket_name, key, 3600, policy)

localfile = './sync/bbb.jpg'

ret, info = put_file(token, key, localfile)
print(info)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
Example #55
0
 def __init__(self, bucket_name, base_url):
     self.bucket_name = bucket_name
     self.base_url = base_url
     self.q = Auth(settings.QINIU_AK, settings.QINIU_SK)
     self.base_url = base_url
Example #56
0
    resp = requests.post(url='http://sms-api.luosimao.com/v1/send.json',
                         auth=('api', 'key-524049379b633f7a4344494e95b09f89'),
                         data={
                             'mobile': tel,
                             'message': f'您的验证码为{mobile_code}。【铁壳测试】'
                         },
                         timeout=10,
                         verify=False)
    return resp.content


QINIU_ACCESS_KEY = 'KarvlHfUdoG1mZNSfDVS5Vh3nae2jUZumTBHK-PR'
QINIU_SECRET_KEY = 'SFPFkAn5NENhdCMqMe9wd_lxGHAeFR5caXxPTtt7'
QINIU_BUCKET_NAME = 'teamproject'

auth = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)


@app.task
def upload_filepath_to_qiniu(file_path, filename):
    """将文件上传到七牛云存储"""
    token = auth.upload_token(QINIU_BUCKET_NAME, filename)
    put_file(token, filename, file_path)


@app.task
def upload_stream_to_qiniu(file_stream, filename, size):
    """将文件上传到七牛云存储"""
    token = auth.upload_token(QINIU_BUCKET_NAME, filename)
    put_stream(token, filename, file_stream, None, size)
Example #57
0
def get_img(url, key):
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)
    ret, info = bucket.fetch(url, bucket_name, key)
    return urljoin(qiniu_yuming, key)
Example #58
0
class BucketTestCase(unittest.TestCase):
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)

    def test_list(self):
        ret, eof, info = self.bucket.list(bucket_name, limit=4)
        assert eof is False
        assert len(ret.get('items')) == 4
        ret, eof, info = self.bucket.list(bucket_name, limit=1000)
        print(ret, eof, info)
        assert eof is False

    def test_buckets(self):
        ret, info = self.bucket.buckets()
        print(info)
        assert bucket_name in ret

    def test_prefetch(self):
        ret, info = self.bucket.prefetch(bucket_name,
                                         'python-sdk.html',
                                         hostscache_dir=hostscache_dir)
        print(info)
        assert ret['key'] == 'python-sdk.html'

    def test_fetch(self):
        ret, info = self.bucket.fetch(
            'http://developer.qiniu.com/docs/v6/sdk/python-sdk.html',
            bucket_name,
            'fetch.html',
            hostscache_dir=hostscache_dir)
        print(info)
        assert ret['key'] == 'fetch.html'
        assert 'hash' in ret

    def test_fetch_without_key(self):
        ret, info = self.bucket.fetch(
            'http://developer.qiniu.com/docs/v6/sdk/python-sdk.html',
            bucket_name,
            hostscache_dir=hostscache_dir)
        print(info)
        assert ret['key'] == ret['hash']
        assert 'hash' in ret

    def test_stat(self):
        ret, info = self.bucket.stat(bucket_name, 'python-sdk.html')
        print(info)
        assert 'hash' in ret

    def test_delete(self):
        ret, info = self.bucket.delete(bucket_name, 'del')
        print(info)
        assert ret is None
        assert info.status_code == 612

    def test_rename(self):
        key = 'renameto' + rand_string(8)
        self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
        key2 = key + 'move'
        ret, info = self.bucket.rename(bucket_name, key, key2)
        print(info)
        assert ret == {}
        ret, info = self.bucket.delete(bucket_name, key2)
        print(info)
        assert ret == {}

    def test_copy(self):
        key = 'copyto' + rand_string(8)
        ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
        print(info)
        assert ret == {}
        ret, info = self.bucket.delete(bucket_name, key)
        print(info)
        assert ret == {}

    def test_change_mime(self):
        ret, info = self.bucket.change_mime(bucket_name, 'python-sdk.html',
                                            'text/html')
        print(info)
        assert ret == {}

    def test_change_type(self):
        target_key = 'copyto' + rand_string(8)
        self.bucket.copy(bucket_name, 'copyfrom', bucket_name, target_key)
        ret, info = self.bucket.change_type(bucket_name, target_key, 1)
        print(info)
        assert ret == {}
        ret, info = self.bucket.stat(bucket_name, target_key)
        print(info)
        assert 'type' in ret
        self.bucket.delete(bucket_name, target_key)

    def test_copy_force(self):
        ret, info = self.bucket.copy(bucket_name,
                                     'copyfrom',
                                     bucket_name,
                                     'copyfrom',
                                     force='true')
        print(info)
        assert info.status_code == 200

    def test_batch_copy(self):
        key = 'copyto' + rand_string(8)
        ops = build_batch_copy(bucket_name, {'copyfrom': key}, bucket_name)
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200
        ops = build_batch_delete(bucket_name, [key])
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200

    def test_batch_copy_force(self):
        ops = build_batch_copy(bucket_name, {'copyfrom': 'copyfrom'},
                               bucket_name,
                               force='true')
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200

    def test_batch_move(self):
        key = 'moveto' + rand_string(8)
        self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
        key2 = key + 'move'
        ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200
        ret, info = self.bucket.delete(bucket_name, key2)
        print(info)
        assert ret == {}

    def test_batch_move_force(self):
        ret, info = self.bucket.copy(bucket_name,
                                     'copyfrom',
                                     bucket_name,
                                     'copyfrom',
                                     force='true')
        print(info)
        assert info.status_code == 200
        ops = build_batch_move(bucket_name, {'copyfrom': 'copyfrom'},
                               bucket_name,
                               force='true')
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200

    def test_batch_rename(self):
        key = 'rename' + rand_string(8)
        self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
        key2 = key + 'rename'
        ops = build_batch_move(bucket_name, {key: key2}, bucket_name)
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200
        ret, info = self.bucket.delete(bucket_name, key2)
        print(info)
        assert ret == {}

    def test_batch_rename_force(self):
        ret, info = self.bucket.rename(bucket_name,
                                       'copyfrom',
                                       'copyfrom',
                                       force='true')
        print(info)
        assert info.status_code == 200
        ops = build_batch_rename(bucket_name, {'copyfrom': 'copyfrom'},
                                 force='true')
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200

    def test_batch_stat(self):
        ops = build_batch_stat(bucket_name, ['python-sdk.html'])
        ret, info = self.bucket.batch(ops)
        print(info)
        assert ret[0]['code'] == 200

    def test_delete_after_days(self):
        days = '5'
        ret, info = self.bucket.delete_after_days(bucket_name, 'invaild.html',
                                                  days)
        assert info.status_code == 612
        key = 'copyto' + rand_string(8)
        ret, info = self.bucket.copy(bucket_name, 'copyfrom', bucket_name, key)
        ret, info = self.bucket.delete_after_days(bucket_name, key, days)
        assert info.status_code == 200
Example #59
0
class ResumableUploaderTestCase(unittest.TestCase):
    mime_type = "text/plain"
    params = {'x:a': 'a'}
    q = Auth(access_key, secret_key)

    def test_put_stream(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=None,
                                   version=None,
                                   bucket_name=None)
            assert ret['key'] == key

    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_2m_stream_v2(self):
        localfile = create_temp_file(2 * 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 test_put_4m_stream_v2(self):
        localfile = create_temp_file(4 * 1024 * 1024)
        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 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 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.qiniup.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_retry(self):
        localfile = __file__
        key = 'test_file_r_retry'
        qiniu.set_default(
            default_zone=Zone('http://a', 'http://upload.qiniup.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)
Example #60
0
# -*- coding: utf-8 -*-
import os
import uuid
from qiniu import Auth, put_data, BucketManager
from key import ACCESS_KEY, SECRET_KEY, BUCKET_NAME

q = Auth(ACCESS_KEY, SECRET_KEY)


class Qiniu(object):
    # def save_file_to_qiniu(self,
    #                        upload_file,
    #                        filename=str(uuid.uuid1()).replace('-', ''),
    #                        path='attach'):
    #     try:
    #         key = '%s/%s' % (path, filename)
    #         token = self.q.upload_token(BUCKET_NAME, key)
    #         ret, info = put_data(token, key, upload_file)
    #         if ret.get('key', None) == None:
    #             raise Exception('upload error')
    #         else:
    #             return u'%s' % key
    #     except Exception, e:
    #         print str(e)
    #         return str(e)

    def fetch_file_to_qiniu(self,
                            url,
                            filename=str(uuid.uuid1()).replace('-', ''),
                            path='attach'):
        try: