Exemple #1
0
    def del_qiniu_pic(self):
        path_url = self.GP('path_url', '')
        dR = {'code': '', 'MSG': ''}
        sql = "select id,cname from images where pic =%s and COALESCE(del_flag,0)=0"
        l, n = self.db.select(sql, path_url)
        if n == 0:
            dR['code'] = '1'
            dR['MSG'] = '删除图片失败!'
            return dR
        aid, cname = l[0]

        q = Auth(self.oss_access_key, self.oss_secret_key)
        bucket_name = self.oss_bucket_name

        bucket = BucketManager(q)
        ret, info = bucket.stat(bucket_name, cname)
        if ret == None:
            q = Auth(self.oss_access_key_all, self.oss_secret_key_all)
            bucket_name = self.oss_bucket_name_all
            bucket = BucketManager(q)
            ret, info = bucket.stat(bucket_name, cname)
            if ret == None:
                dR['code'] = '1'
                dR['MSG'] = '数据有误!'
                return dR
        ret_d, info_d = bucket.delete(bucket_name, cname)

        self.db.query("update images set del_flag=1  where id= %s", aid)
        dR['MSG'] = '删除图片成功!'
        dR['code'] = '0'
        return dR
Exemple #2
0
 def _file_stat(self, name):
     bucket = BucketManager(self.auth)
     ret, info = bucket.stat(self.bucket_name, name)
     if ret is None:
         raise QiNiuError(info)
     self.file_stat = ret
     return ret
Exemple #3
0
def get_file_fron_qiniu():
    one_pic_lst = []
    host = 'http://ptn9z2j0m.bkt.clouddn.com'
    # bucket_domain = host[7:]
    access_key = 'Amsb0VBTxOJBlCAhcSmRxA4O8gPLde6MBHoC_UdM'
    secret_key = 'Jl-F0fqFTSzzHWLRa4rTkxnuQw4mZNAPWXtSZ5Pz'
    # 初始化Auth状态
    q = Auth(access_key, secret_key)
    # 初始化BucketManager
    bucket = BucketManager(q)
    # 你要测试的空间, 并且这个key在你空间中存在
    bucket_name = '0614consumer'
    # 前缀
    prefix = None
    # 列举条目
    limit = 2549
    delimiter = None
    # 标记
    marker = None
    while True:
        ret, eof, info = bucket.list(bucket_name, prefix, marker, limit,
                                     delimiter)
        time.sleep(0.1)
        marker = ret.get('marker')
        one_pic_lst.extend(ret.get('items'))
        if marker is None:
            break
        else:
            continue
    one_pic_lst = ['{}/{}'.format(host, i.get('key')) for i in one_pic_lst]
    one_pic_lst.extend(one_pic_lst)
    one_pic_lst = sorted(one_pic_lst, key=lambda x: x.split('_')[-1][:-4])
    return one_pic_lst
Exemple #4
0
def uploadQiniu(src_file, dist_file):
    try:
        bucket_name = CONFIG['qiniu']['bucket']
        ak = CONFIG['qiniu']['access_key']
        sk = CONFIG['qiniu']['secret_key']
        domain = CONFIG['qiniu']['domain']
        token_timeout = 3600
        qiniu = Auth(ak, sk)
        token = qiniu.upload_token(bucket_name, dist_file, token_timeout)
        ret, info = put_file(token, dist_file, src_file)
        if ret == None:
            raise Exception(info)
        #assert ret['key'] == dist_file
        #assert ret['hash'] == etag(src_file)

        days = 30
        url = domain + '/' + dist_file
        down_url = qiniu.private_download_url(url, 86400 * days)
        msg = '上传七牛成功:' + json.dumps(ret) + "\r\n"
        msg = msg + "下载链接(" + str(days) + "天有效):" + down_url
        log(msg, title='上传七牛成功')

        if days > 0:
            bucket = BucketManager(qiniu)
            bucket.delete_after_days(bucket_name, dist_file, str(days))
        return True
    except Exception as e:
        log('上传七牛失败:' + str(e), title='上传七牛失败')
        return False
Exemple #5
0
def go():
    result_url_dic = {}
    result_name_list = []
    base_url = u'http://pkdsmwim0.bkt.clouddn.com/'
    q = Auth(QINIU['AK'], QINIU['SK'])
    bucket = BucketManager(q)
    bucket_name = 'small'
    # 前缀
    prefix = ''
    # 列举条目
    limit = 1000
    # 列举出除'/'的所有文件以及以'/'为分隔的所有前缀
    delimiter = '.'
    # 标记
    marker = None
    ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
    for data in c_utils.deserialize(info.text_body)['commonPrefixes']:
        result_url_dic[data + 'pdf'] = base_url + data + 'pdf'
        result_name_list.append(data + 'pdf')
    for s in result_name_list:
        code = redis_cli.sadd(PDF_URL, s)
        print(code)
        if not code:
            print(s)
    redis_cli.hmset(PDF_DIC_URL, result_url_dic)

    print c_utils.sort_serialize(result_url_dic)
    print c_utils.sort_serialize(result_name_list)

    print(len(result_name_list))
Exemple #6
0
 def __GetBucket(self):
     self.q = Auth(self.access_key, self.secret_key)
     bucket = BucketManager(self.q)
     if not bucket:
         raise(NameError,"连接七牛失败")
     else:
         return bucket
Exemple #7
0
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":
        data = qiniu_file_del(bucket, **kwargs)

    elif kwargs.get("action") == "rename":
        data = qiniu_file_rename(bucket, **kwargs)

    elif kwargs.get("action") == "get_file_url":
        data = get_file_url(**kwargs)
    else:
        assert False
    return data
Exemple #8
0
 def delete(self, filename):
     bucket = BucketManager(self.auth)
     reform, fo = bucket.delete(self.bucketname, filename)
     if reform != None:
         print('已经成功地将删除'.format(filename))
     else:
         print('这里出现了一个小错误.(可能是空间并没有这个文件)')
Exemple #9
0
def upload_pic(filename):
    # parser = OptionParser()
    # parser.add_option("", "--filename", dest="filename", help="filename")
    # (opts, args) = parser.parse_args()

    #需要填写你的 Access Key 和 Secret Key
    access_key = 'key'
    secret_key = 'secret'

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

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

    #上传到七牛后保存的文件名
    arr = filename.split("/")

    key = arr[len(arr) - 1]

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

    #要上传文件的本地路径

    localfile = filename

    ret, info = put_file(token, key, localfile)
    bucket = BucketManager(q)

    print("http://image.blueskykong.com/" + key)
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)
    return info
Exemple #10
0
def del_cloud_file(file_name):
    bucket = BucketManager(q)
    try:
        bucket.delete(bucket_name, file_name)
        print "删除{0}成功!".format(file_name)
    except Exception:
        print "删除{0}失败!".format(file_name)
Exemple #11
0
def qiniuOP():
    q = Auth(access_key, secret_key)

    # 创建bucket实例
    bucket = BucketManager(q)
    # 读取空间内容的list
    bucket_list = BucketManager.list(bucket, 'xcxdatacab2')
    # 将list的内容转成字典
    list_dic = bucket_list[0]
    # 将字典中items的内容提取出来
    list_items = list_dic['items']
    # print(list_items)
    for i in list_items:
        print(i['key'])
        appname = i['key']
        version = i['key'][0:5]
        downloadurl = BaseURL + appname
        # 组合写入数据库的值
        valuse = '"' + appname + '"' + ',' + '"' + version + '"' + ',' + '"' + downloadurl + '"'
        table_list_contnt = compare_table_qiniulist(table)
        #
        table_appname = []
        for i in table_list_contnt:
            table_appname = table_appname + [i[1]]
        if appname in table_appname:
            print("内容重复无需写入")
        else:
            try:

                MysqlOP.writeMysqlonerow(table, fieldname, valuse)
                print('数据写入成功')
            except:
                print('数据写入失败')
def multi_copy(private_key_list: List[str]) -> List:
    access_key = Facade.config["qiniu"]["access_key"]
    secret_key = Facade.config["qiniu"]["secret_key"]
    # original code
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)

    # 1. what i do
    target_copy_dict = {}
    copy_key_list = []

    # 2. prepare copy name
    for private_key in private_key_list:
        target_copy_dict[private_key] = rename(private_key)
        copy_key_list.append(rename(private_key))

    public_bucket_name = Facade.config["qiniu"]["category"]["public"]["bucket"]
    private_bucket_name = Facade.config["qiniu"]["category"]["private"][
        "bucket"]

    logworker.warning('------multi_copy---------start')
    logworker.warning(public_bucket_name)
    logworker.warning(private_bucket_name)
    logworker.warning(copy_key_list)
    logworker.warning('end------multi_copy---------')

    # force为true时强制同名覆盖, 字典的键为原文件,值为目标文件
    ops = build_batch_copy(private_bucket_name,
                           target_copy_dict,
                           public_bucket_name,
                           force='false')
    ret, info = bucket.batch(ops)

    return ret, info, copy_key_list
Exemple #13
0
    def get(self, request, id):
        if not request.user.is_authenticated:
            return JsonResponse({'code': 4, 'msg': '修改图片请先登录'})
        try:
            photo = Photo.objects.get(pk=id)
            redis = get_redis()
            if photo.author_id == request.user.username:
                access_key = 'M2TrolxfManTFNP4Clr3M12JW0tvAaCV0xIbrZk5'
                secret_key = 'Llh0byt0KDHwiFlcNVvPiTpQSrH8IrZSt5puu1zS'

                q = qiniu_auth(access_key, secret_key)
                bucket_name = 'photo'
                try:
                    bucket = BucketManager(q)
                    key = os.path.basename(photo.url)
                    ret, info = bucket.delete(bucket_name, key)
                    assert ret == {}
                except Exception as e:
                    pass

                # 删除该博客的所有评论点赞数
                for r in photo.replies.all():
                    redis.delete('preply:{}:likes'.format(r.id))
                for r in photo.sub_replies.all():
                    redis.delete('psreply:{}:likes'.format(r.id))

                # 删除博客点赞
                redis.delete('photo:{}:likes'.format(id))

                # 删除博客
                photo.delete()
                return JsonResponse({'code': 1, 'msg': '删除成功'})
            return JsonResponse({'code': 3, 'msg': '删除失败,无权删除'})
        except exceptions.ObjectDoesNotExist:
            return JsonResponse({'code': 2, 'msg': '删除失败,没有该图片'})
Exemple #14
0
def get_remote_category():
    def filter_file(file_item):
        file_key = file_item["key"]
        return file_key.find(u"__ARCHIVE__") == -1 \
            and file_key.find(u"_log") == -1 \
            and file_key.find(u"申请-") == -1 \
            and file_key.find(u".DS_Store") == -1

    auth = Auth(CONFIG["AK"], CONFIG["SK"])
    bucket = BucketManager(auth)
    files = []
    marker = None
    while True:
        response, eof, info = bucket.list(CONFIG["BUCKET_NAME"], None, marker,
                                          1000, None)
        if info.status_code == 200:
            files = files + response["items"]
            if eof:
                break
            if "marker" in response:
                marker = response["marker"]
        else:
            return False

    category_remote = {}
    for file_list_item in filter(filter_file, files):
        size = file_list_item["fsize"]
        file_id = file_list_item["hash"]
        key = file_list_item["key"]
        time = file_list_item["putTime"]
        category_remote[file_id] = {"key": key, "size": size, "time": time}
    return category_remote
Exemple #15
0
    def teardown_class(cls):
        """Delete all files in the test bucket.
        """
        storage = QiniuPrivateStorage(
            bucket_name=get_qiniu_config('QINIU_PRIVATE_BUCKET_NAME'),
            bucket_domain=get_qiniu_config('QINIU_PRIVATE_BUCKET_DOMAIN'),
        )
        auth = storage.auth
        bucket = BucketManager(auth)

        while True:
            ret, eof, info = bucket.list(storage.bucket_name, limit=100)

            if ret is None:
                print(info)
                break

            for item in ret['items']:
                name = item['key']
                if six.PY2:
                    name = name.encode('utf-8')
                ret, info = bucket.delete(storage.bucket_name, name)
                if ret is None:
                    print(info)
            if eof:
                break
Exemple #16
0
def main():

    qiniuConfig = getQiniuConfig("9ong")
    q = Auth(qiniuConfig["access_key"], qiniuConfig["secret_key"])
    bucket = BucketManager(q)

    # url = 'https://mmbiz.qpic.cn/mmbiz_png/icHOSb47jqpUuNrM7oELEjVfVQozavBjj0m8gBpxXx7kr2n6xox3aV9WMwMKJD23VekwpRETF3s2UicDibaVIIRLw/0?wx_fmt=png'
    # url = 'https://mmbiz.qpic.cn/mmbiz_jpg/3OEpTPib0kVib6SYAfuB5uty5ma9DBU1r4icuhFk0mQOSAcFT6ibqaXchIN0K94tln5gcLu8v3eQ1GMEK9fUNhAGbg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1'
    # fetchImg(url,bucket,qiniuConfig)
    # sys.exit()

    theFile = 'I:\\src\\github-page\\docs\\产品设计\\企业应用架构的演变史-杨堃-bak.md'
    theContent = ''
    theList = []
    with open(theFile, 'r', encoding='utf-8') as rf:
        for line in rf:
            matchList = []
            matchList = re.findall(r'!\[.*\]\((.+)\)', line)
            if matchList:
                for originUrl in matchList:
                    print(originUrl)
                    # 七牛抓取网络图片
                    newUrl = fetchImg(originUrl, bucket, qiniuConfig)
                    print(newUrl)
                    # 替换line
                    line = line.replace(originUrl, newUrl)

            theList.append(line)
    with open(theFile, 'w', encoding='utf-8') as wf:
        for oneLine in theList:
            theContent += oneLine
        if theContent:
            wf.write(theContent)
            print("===========================\n")
            print(theContent)
Exemple #17
0
    def get(self):
        #files = os.listdir(os.path.join("static", "upload"))
        #files = [os.path.join("upload", x) for x in files]
        files = []

        # list all resourses
        # bucket=<UrlEncodedBucket>&marker=<Marker>&limit=<Limit>&prefix=<UrlEncodedPrefix>&delimiter=<UrlEncodedDelimiter>
        access_key = '6LG96YZtw8bacYtviITUQIFqdK67qZ3SqXE8Lhaw'
        secret_key = 'Kc1aI18VwuDyVNdid8UfdPrAPiWXOb09UxmlFfQd'
        bucket_name = 'nossiac'

        q = Auth(access_key, secret_key)
        bucket = BucketManager(q)
        prefix = None
        limit = 1000
        delimiter = None
        marker = None  # offset

        ret, eof, info = bucket.list(bucket_name, prefix, marker, limit,
                                     delimiter)

        items = ret["items"]
        for item in items:
            files.append("http://7xthf5.com1.z0.glb.clouddn.com/" +
                         item["key"])

        self.render("upload.html", files=files)
Exemple #18
0
    def mRight(self):

        if self.qiniu_access_key == '' or self.qiniu_secret_key == '' or self.qiniu_bucket_name == '':
            return [], []

        q = Auth(self.qiniu_access_key, self.qiniu_secret_key)
        bucket_name = self.qiniu_bucket_name
        bucket = BucketManager(q)
        # 前缀
        prefix = None
        # 列举条目
        limit = 10000
        # 列举出除'/'的所有文件以及以'/'为分隔的所有前缀
        delimiter = None
        # 标记
        marker = None
        ret, eof, info = bucket.list(bucket_name, prefix, marker, limit,
                                     delimiter)

        iTotal_length = len(ret.get('items'))
        if iTotal_length == 0:
            return [], []
        List = ret.get('items')

        pageNo = self.GP('pageNo', '')
        if pageNo == '':
            pageNo = '1'
        pageNo = int(pageNo)

        L, iTotal_length, iTotal_Page, pageNo, select_size = self.list_for_grid(
            List, iTotal_length, pageNo=pageNo, select_size=10)
        #L,iTotal_length,iTotal_Page,pageNo,select_size=self.db.select_for_grid(sql,self.pageNo)
        PL = [pageNo, iTotal_Page, iTotal_length, select_size]
        return PL, L
Exemple #19
0
def storage_delete(merid, shopkey, filename):
    """七牛云存储删除文件接口
    """
    if not filename:
        return False

    try:
        q = Auth(current_app.config.get('QINIU_ACCESS_KEY'),
                 current_app.config.get('QINIU_SECRET_KEY'))

        bucket = BucketManager(q)

        ret, info = bucket.stat(current_app.config.get('QINIU_BUCKET_NAME'),
                                filename)
        if info and info.status_code != 200:
            return False

        if not ret or not ret['hash']:
            return True

        ret, info = bucket.delete(current_app.config.get('QINIU_BUCKET_NAME'),
                                  filename)
        if info and info.status_code != 200:
            return False
    except:
        return False

    return True
Exemple #20
0
 def __init__(self):
     self.auth = Auth(settings.QFILE_QINIU_ACCESS_KEY,
                      settings.QFILE_SECRET_KEY)
     self.bucket_name = settings.QFILE_QINIU_BUCKET_NAME
     self.bucket_domain = settings.QFILE_QINIU_BUCKET_DOMAIN
     self.bucket_manager = BucketManager(self.auth)
     self.secure_url = settings.QFILE_QINIU_SECURE_URL
Exemple #21
0
    def get(self):
        # list all resourses
        # bucket=<UrlEncodedBucket>&marker=<Marker>&limit=<Limit>&prefix=<UrlEncodedPrefix>&delimiter=<UrlEncodedDelimiter>
        self.access_key = '6LG96YZtw8bacYtviITUQIFqdK67qZ3SqXE8Lhaw'
        self.secret_key = 'Kc1aI18VwuDyVNdid8UfdPrAPiWXOb09UxmlFfQd'
        self.bucket_name = 'nossiac'

        q = Auth(self.access_key, self.secret_key)
        bucket = BucketManager(q)
        prefix = None
        limit = 10
        delimiter = None
        marker = None  # offset

        ret, eof, info = bucket.list(self.bucket_name, prefix, marker, limit,
                                     delimiter)
        self.write(str(ret))

        self.write("<hr>")
        #import json
        #json_ret = json.loads(ret)
        items = ret["items"]
        for item in items:
            self.write(
                '<img style=\"margin=10px; max-width:100px; max-height: 100px;\" src=\"http://7xthf5.com1.z0.glb.clouddn.com/'
                + item["key"] + '\"><br>')

        self.write("<hr>")
        self.write(str(eof))
        self.write("<hr>")
        self.write(str(info))
Exemple #22
0
 def list_file_names(self,
                     bucket_name,
                     prefix=None,
                     marker=None,
                     limit=None,
                     delimiter=None):
     """
     Args:
         bucket:     空间名
         prefix:     列举前缀
         marker:     列举标识符(首次为None)
         limit:      单次列举个数限制(默认列举全部)
         delimiter:  指定目录分隔符
         
     Returns:
         pathlist: ['file_name',...]
     """
     file_name_list = []
     bucket = BucketManager(self.__auth)
     marker = None
     eof = False
     while eof is False:
         ret, eof, info = bucket.list(bucket_name,
                                      prefix=prefix,
                                      marker=marker,
                                      limit=limit)
         marker = ret.get('marker', None)
         for item in ret['items']:
             file_name_list.append(item['key'])
     return file_name_list, eof
Exemple #23
0
def media_copy(key, from_bucket, to_bucket):
    auth = qiniu_auth()
    bucket = BucketManager(auth)
    ret, info = bucket.stat(from_bucket, key)
    if ret:
        ret, info = bucket.copy(from_bucket, key, to_bucket, key)
    return ret, info
Exemple #24
0
def list_all(bucket_name, bucket=None, prefix="", limit=100):

    if bucket is None:
        bucket = BucketManager(q)

    marker = None
    eof = False

    while eof is False:

        try:
            ret, eof, info = bucket.list(bucket_name,
                                         prefix=prefix,
                                         marker=marker,
                                         limit=limit)
            # print ret, eof, info
        except Exception as e:
            print repr(e) + ' while list from marker={m}'.format(m=marker)
            time.sleep(1)
            continue

        marker = ret.get('marker', None)

        for item in ret['items']:
            # print 'got:', item
            yield item
Exemple #25
0
    def fetch(self):
        self.__qiniuConfig = qiniuConfig =  self.__getQiniuConfig('9ong')
        qiniu = Auth(qiniuConfig["access_key"], qiniuConfig["secret_key"])
        self.__bucket = BucketManager(qiniu)            
        
        theList = []
        theContent = ''
        with open(self.__theFile,'r',encoding='utf-8') as rf:
            for line in rf:
                matchList = []
                matchList = re.findall(r'!\[.*\]\((.+)\)',line)            
                if matchList:
                    for originUrl in matchList:
                        print(originUrl)                    
                        # 七牛抓取网络图片
                        newUrl = self.__fetchImg(originUrl)
                        print(newUrl)
                        # 替换line
                        line = line.replace(originUrl,newUrl)

                theList.append(line)
        with open(self.__theFile,'w',encoding='utf-8') as wf:
            for oneLine in theList:
                theContent += oneLine
            if theContent:
                wf.write(theContent)
                print("\n"+self.__theFile+"\n===========================\n")
Exemple #26
0
    def delete_qiniu_pic_data(self):
        pk = self.GP('id', '')
        dR = {'code': '', 'MSG': ''}

        sql = "select cname,pic from images where id =%s"
        l, n = self.db.select(sql, pk)
        if n == 0:
            dR['code'] = '1'
            dR['MSG'] = '删除图片失败,请刷新页面重试'
            return dR
        filename, file_link = l[0]

        q = Auth(self.qiniu_access_key, self.qiniu_secret_key)
        bucket_name = self.qiniu_bucket_name

        bucket = BucketManager(q)
        ret, info = bucket.stat(bucket_name, filename)
        if ret == None:
            self.db.query("update images set del_flag=1  where id= %s", pk)
            dR['MSG'] = '七牛没有这张图片,在数据库删除记录成功!'
            dR['code'] = '0'
            return dR

        ret_d, info_d = bucket.delete(bucket_name, filename)
        if ret_d == {}:
            self.db.query("update images set del_flag=1  where id= %s", pk)
            dR['MSG'] = '删除图片成功!'
        return dR
Exemple #27
0
def removeFile(key):
    q = Auth(access_key, secret_key)
    bucket_name = defaultBucketName
    bucket = BucketManager(q)

    ret, info = bucket.delete(bucket_name, key)
    logging.debug('remove qiniu file uri:[%r]' % (key))
    return key
Exemple #28
0
    def __init__(self, *args, **kwargs):

        accesskeyid = kwargs['accesskeyid']
        accesskeysecret = kwargs['accesskeysecret']
        self._bucket = kwargs['bucket']
        self._auth = Auth(accesskeyid, accesskeysecret)
        self._domain = kwargs['domain_url']
        self._qiniu_api = BucketManager(self._auth)
Exemple #29
0
    def get_bucket_mgr(self):
        if not self._bucket_mgr:
            ak = self.access_key
            sk = self.secret_key
            q = Auth(ak, sk)
            self._bucket_mgr = BucketManager(q)

        return self._bucket_mgr
Exemple #30
0
 def stat_file(self, path, info):
     bucket = BucketManager(self.q)
     ret, qninfo = bucket.stat(self.QINIU_BUCKET_NAME, path)
     if 'hash' in ret:
         modified_tuple = parsedate_tz(ret['putTime'])
         modified_stamp = int(mktime_tz(modified_tuple))
         return {'checksum': path, 'last_modified': modified_stamp}
     return {}