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
 def list(self, bucket_name, prefix, limit=5):
     bucket = BucketManager(self.mauth)
     delimiter = None
     marker = None
     ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
     for item in ret.get('items'):
         print "{Name: %s Size:%s Hash:%s}" % (item['key'], item['fsize'], item['hash'])
Exemple #3
0
    def _list_remote_bucket(self):
        """
        traverse the buckt through the Qiniu API and return a list of keys
        and timestamp
        :except no connection is established
        :return: a dict mapping key to upload time
        """
        key_list = {}
        big_file = {}
        done = False
        marker = None

        bucket = BucketManager(self.auth)

        while not done:
            res, done, _ = bucket.list(self.bucketname, marker=marker,
                                       limit=self.BATCH_LIMIT)
            if not res:
                self.logger('ERROR',
                            'could not establish connection with cloud. Exit.')
                sys.exit(1)
            marker = res.get('marker')
            for resource in res['items']:
                key_list[resource['key']] = resource['putTime']
                big_file[resource['key']] = resource['fsize'] \
                    if resource['fsize'] > self.download_size_threshold * 2 \
                    else 0
        return key_list, big_file
Exemple #4
0
class QiniuUpload():
    def __init__(self, bucket = config.PIC_BUCKET, domain = config.PIC_DOMAIN):
        self.qiniuer = qiniu.Auth(config.QINIU_ACCESS_KEY, config.QINIU_SECRET_KEY)
        self.bucket_manager = BucketManager(self.qiniuer)
        self.bucket = bucket
        self.domain = domain

    def get_token(self):
        return self.qiniuer.upload_token(self.bucket)

    def del_file(self, key):
        ret, info = self.bucket_manager.delete(self.bucket, key)
        # 错误处理
        assert ret is None
        assert info.status_code == 612
        return ret, info

    def get_upload_info(self, key):
        ret, info = self.bucket_manager.stat(self.bucket, key)
        assert 'hash' in ret
        return info

    def get_file_info(self, key):
        url = self.domain + key + '?imageInfo'
        try:
            response = urllib2.urlopen(url, timeout=5)
            resp = response.read()
            return resp
        except Exception, e:
            print e
            return None
class QiniuWrapper():

    policy = {'returnBody': '{"key": $(key), "type": $(mimeType), "name": $(fname), "size": $(fsize), "hash": $(etag)}'}
    bucket_name = QINIU_BUCKET_NAME
    domain = 'http://%s.qiniudn.com/' % bucket_name

    def __init__(self):
        self.q = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
        self.bucket_manager = BucketManager(self.q)

    def get_upload_token(self, key, expires=3600):
        return self.q.upload_token(self.bucket_name, key, expires, self.policy)

    def upload_file(self, key, filename, mime_type="application/octet-stream"):
        '''
        上传文件到七牛,如果指定的key对应的文件在七牛上已经存在, 会覆盖原来七牛上的文件
        '''
        ret, info = put_file(self.get_upload_token(key), key, filename, mime_type=mime_type, check_crc=True)
        if info.status_code != 200:
            return (False, info)
        return (True, info)

    def upload_stream(self, key, input_stream, data_size, mime_type="application/octet-stream"):
        '''
        上传文件到七牛,如果指定的key对应的文件在七牛上已经存在, 会覆盖原来七牛上的文件
        '''
        ret, info = put_stream(self.get_upload_token(key), key, input_stream, data_size, mime_type=mime_type, check_crc=True)
        if info.status_code != 200:
            return (False, info)
        return (True, info)

    def move(self, old_key, new_key):
        ret, info = self.bucket_manager.move(self.bucket_name, old_key, self.bucket_name, new_key)
        if info.status_code != 200:
            return (False, info)
        return (True, info)

    def delete(self, key):
        ret, info = self.bucket_manager.delete(self.bucket_name, key)
        if info.status_code != 200:
            return (False, info)
        return (True, info)

    def batch_delete(self, keys):
        '''
        keys = ['key1', 'key2', 'key3']
        '''
        ops = build_batch_delete(self.bucket_name, keys)
        ret, info = self.bucket_manager.batch(ops)
        if info.status_code != 200:
            return (False, info)
        return (True, info)

    def list(self, prefix=None, limit=1000, marker=None):
        return self.bucket_manager.list(self.bucket_name, prefix=prefix, marker=marker, limit=limit)

    @classmethod
    def get_url(cls, key):
        return cls.domain + key
Exemple #6
0
def bucket_list():
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)
    ret, eof, info = bucket.list(bucket_name, limit=4)
    for item in ret.get('items', []):
        print item
    ret, eof, info = bucket.list(bucket_name, limit=100)
    print(info)
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 #8
0
def qiniu_file(pre_text, limit):
    q = qiniu_q()
    bucket = BucketManager(q)
    bucket_name = 'love-travel'
    ret, eof, info = bucket.list(bucket_name, prefix=pre_text, delimiter="/", limit=limit)
    if len(ret["items"]) > 0 :
        return {'result' : True, 'files' : ret["items"]}
    else:
        return {'result' : False}
Exemple #9
0
def move_file(username,path,filename,newpath,newfilename,bucket=None):  # bucket即bucketmanager path='/dir/dir' 可用于重命名文件,移动文件,移动目录,重命名目录 # ok
    user = User.objects.filter(username=username)
    if bool(user):
        user = user[0]
    else:
        return False
    if bucket == None:
        q = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY)  # 授权
        bucket = BucketManager(q)
        if  not check_str(filename) or not check_str(path,ispath=True) or  not check_str(newfilename) or not check_str(newpath,ispath=True):
            return False

        file1 = FileInfo.objects.filter(owner=user,file_name=filename,file_path=path)
        filecheck = FileInfo.objects.filter(owner=user,file_path=newpath)  # filepath 可以不存在
        if bool(file1) and bool(filecheck):
            update_dir_size(username,path,0-file1[0].size)
            update_dir_size(username,newpath,file1[0].size)
        else:
            return False
    # 数据库操作
    file1 = FileInfo.objects.filter(owner=user,file_name=filename,file_path=path)
    if bool(file1):
        file1 = file1[0]
    else:
        return False  # 文件名或者文件夹名不存在
    filecheck = FileInfo.objects.filter(owner=user,file_name=newfilename,file_path=newpath)
    if bool(filecheck):
        return False
    file1.file_path = newpath
    file1.file_name = newfilename
    file1.save()
    if path == '/':
        path = ''
    if newpath == '/':
        newpath = ''
    if file1.file_type == 'dir': 
        subpath = ''.join([path,'/',filename])
        files = FileInfo.objects.filter(owner=user,file_path=subpath)
        subpath2 = ''.join([newpath,'/',newfilename])
        for f in files:
            if f.file_type != 'dir':
                f.file_path = subpath2
                f.save()
                key = ''.join([username, subpath , '/', f.file_name])
                key2 = ''.join([username, subpath2, '/', f.file_name])
                ret, info = bucket.move(settings.QINIU_BUCKET_NAME, key, settings.QINIU_BUCKET_NAME, key2)
                print info
                # assert ret=={}
            else :
                move_file(username,subpath,f.file_name,subpath2,f.file_name,bucket)
    else:
        key = ''.join([username, path, '/', filename])
        key2 = ''.join([username, newpath, '/', newfilename])
        ret, info = bucket.move(settings.QINIU_BUCKET_NAME, key, settings.QINIU_BUCKET_NAME, key2)
        print info
        # assert ret == {}
    return True
Exemple #10
0
 def get_file_info(self,bucket_name,keys=[]):
     """Args:
     bucket_name:'bucket_name'
     keys:  ['fileName1','fileName2']
     """
     bucket = BucketManager(self.__auth)
     ops = build_batch_stat(bucket_name, keys)
     ret, info = bucket.batch(ops)
     return ret,info
Exemple #11
0
 def delete_files(self,source_bucket,pathlist=[]):
     """Args:
     source_bucket: 'source_bucket'
     pathlist: ['source_file_name',...]
     """
     bucket = BucketManager(self.__auth)
     ops = build_batch_delete(source_bucket, pathlist)
     ret, info = bucket.batch(ops)
     return ret,info
Exemple #12
0
 def get(self, request):
     mediaid = request.query_params["mediaid"]
     filename = str(uuid.uuid1()).replace("-", "") + ".jpg"
     q = Auth(settings.QNACCESSKEY, settings.QNSECRETKEY)
     bucket = BucketManager(q)
     token = wxutils.get_accesstoken()
     mediaurl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + token + "&media_id=" + mediaid
     ret, info = bucket.fetch(mediaurl, settings.QNBUKET, filename)
     return Response({"pic": filename})
Exemple #13
0
def save_to_qiniu(url, store_name):
    """放到七牛云存储上
    """
    bucket_name = config.QINIU['bucket']
    g = Auth(config.QINIU['accesskey'], config.QINIU['secretkey'])
    bucket = BucketManager(g)
    ret, info = bucket.fetch(url, bucket_name, store_name)
    if ret:
        return False
    return config.QINIU['cdn'] + '/' + store_name
Exemple #14
0
 def move_files(self,source_bucket,target_bucket,pathdict={}):
     """Args:
     source_bucket: 'source_bucket'
     target_bucket:  'target_bucket'
     pathdict: {'source_file_name':'target_file_name',...}
     """
     bucket = BucketManager(self.__auth)
     ops = build_batch_move(source_bucket, pathdict, target_bucket)
     ret, info = bucket.batch(ops)
     return ret,info
Exemple #15
0
def isFileExist(file_name):
    q = getAuth()
    # check if file already exist
    bucket_name = os.environ['server']
    bucket = BucketManager(getAuth())
    ret, info = bucket.stat(bucket_name, file_name)
    if ret != None:
        return True
    else:
        return False
 def upload_by_fetch(self, remote_file_path, key):
     """
     通过远程url上传文件,但是可能报错,请使用upload()方法
     :param remote_file_path:
     :param key:
     :return:
     """
     bucket = BucketManager(self.auth)
     ret, info = bucket.fetch(remote_file_path, QINIU_DEFAULT_BUCKET, key)
     print('ret: %s' % ret)
     print('info: %s' % info)
Exemple #17
0
def bucket_list():
    bm = BucketManager(qa())
    ret = bm.list('image', None, None, 10, None)
    if ret and ret[0] and ret[0].get('items') and len(ret[0].get('items')) > 0:
        imgs = ret[0].get('items')
        marker = ret[0].get('marker')
        print(marker)
        if imgs:
            for x in imgs:
                print('ll.append("%s")' % x['key'])
    print(ret)
Exemple #18
0
def delete(auth):
    print('删除')
    #初始化BucketManager
    bucket = BucketManager(auth)

    #删除bucket_name 中的文件 key
    ret, info = bucket.delete(Config.bucket_name, Config.file)
    print(info)
    assert ret == {}

    update(auth)
Exemple #19
0
def save_image(url, bookname):
    """抓取图片放到七牛云存储上
    """
    bucket_name = config.QINIU['bucket']
    g = Auth(config.QINIU['accesskey'], config.QINIU['secretkey'])
    bucket = BucketManager(g)
    save_to = 'IMAGE|{}'.format(bookname)
    ret, info = bucket.fetch(url, bucket_name, save_to)
    if ret:
        return False
    return config.QINIU['cdn'] + '/' + save_to
Exemple #20
0
 def __init__(self, dbname, usr, pwd):
     self.m = MongoDBPipeline(dbname, usr, pwd)
     self.m_auth = self.m.auth
     self.keys = self.m.setCol(conf.QiniuDBUser, conf.QiniuProfileCol).col.find()[0].keys
     self.access_key = self.keys.access_key
     self.secret_key = self.keys.secret_key
 
     # 构建鉴权对象
     self.auth = self.Auth(self.access_key, self.secret_key)
 
     # bucket
     self.bucket = BucketManager(self.auth)
def del_qiqiu(filename):
    access_key = 'Y2epBqeCBXUkG6_Li_8fkpL2PgqQ6RuPFx74N-Ei'
    secret_key = 'miw2OtJI3-K-7q7LtpDWr3xnCOcPfp7dfzDhuWor'
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'chenihphoto'
    # 初始化BucketManager(q)
    bucket = BucketManager(q)
    key = filename  # key就是文件名
    ret, info = bucket.delete(bucket_name, key)
    return info
Exemple #22
0
    def multi_delete(self, key_list):
        # key_list: [key1, key2, key3, ...]
        from qiniu import build_batch_delete
        bucket = BucketManager(self.q)
        ops = build_batch_delete(self.bucket_name, key_list)
        ret, info = bucket.batch(ops)
        # print('QiniuStorage - multi_delete: %s' % info)

        json_info = json.loads(info.text_body)
        for m_info in json_info:
            # "code":612,"data":{"error":"no such file or directory"
            assert m_info[u'code'] == 200 or m_info[u'code'] == 612
 def __init__(self, access_key, secret_key, domain='domain'):
     """
     :param access_key: 七牛 AK
     :param secret_key: 七牛 SK
     :param domain: 七牛 空间 域名
     """
     self.access_key = access_key
     self.secret_key = secret_key
     self.auth = self.get_auth()
     self.token = None
     self.domain = domain
     self.bucket = BucketManager(self.auth)
Exemple #24
0
def img_del(url):

    #初始化BucketManager
    bucket = BucketManager(q)
    #删除bucket_name 中的文件 key
    ret, info = bucket.delete(url['bucket'], url['key'])
    try:
        assert ret == {}
    except:
        print "error-del"
        return -1
    return 0
Exemple #25
0
 def upload(self, picture_path_list, link_only=False):
     if self.upload_handler:
         success_uploaded_list = []
         for picture_path in picture_path_list:
             picture_name = os.path.basename(picture_path)
             token = self.upload_handler.upload_token(
                 self.container_name, picture_name, 3600)
             ret, info = put_file(token, picture_name, picture_path)
             bucket = BucketManager(self.upload_handler)
             success = bucket.stat(self.container_name, picture_name)
             print(success)
         self.write_markdown_picture_url(picture_path_list, link_only)
Exemple #26
0
def url2qiniu(url):
    key = hashlib.md5(url).hexdigest()
    try:
        q = Auth(current_app.config.get('QINIU_ACCESS_KEY'),
                 current_app.config.get('QINIU_SECRET_KEY'))
        bucket = BucketManager(q)
        isok = bucket.fetch(url, current_app.config.get('QINIU_BUCKET_NAME'),
                            key)
    except:
        return None

    return "%s%s" % (current_app.config.get('QINIU_HOST'), key)
    def __init__(self,
                 access_key=QINIU_ACCESS_KEY,
                 secret_key=QINIU_SECRET_KEY,
                 bucket_name=QINIU_BUCKET_NAME,
                 bucket_domain=QINIU_BUCKET_DOMAIN,
                 secure_url=QINIU_SECURE_URL):

        self.auth = Auth(access_key, secret_key)
        self.bucket_name = bucket_name
        self.bucket_domain = bucket_domain
        self.bucket_manager = BucketManager(self.auth)
        self.secure_url = secure_url
Exemple #28
0
    def __init__(self, access_key: str, secret_key: str, bucket_name: str,
                 sync_dir: str, exclude: List, cover: bool,
                 remove_redundant: bool):
        self.bucket_name = bucket_name
        self.q = Auth(access_key, secret_key)
        self.bucket = BucketManager(self.q)
        self.sync_dir = sync_dir
        self.exclude = exclude
        self.cover = cover
        self.remove_redundant = remove_redundant

        self.sync()
Exemple #29
0
def delete_qiniu(filename):
    """删除相片"""
    # 构建鉴权对象,接收两个参数,Access Key 和 Secret Key
    q = Auth(Config.access_key,Config.secret_key)
    # 要上传的空间
    bucket_name = 'lisa530'
    # 初始化BucketManager
    bucket = BucketManager(q)
    # key就是要删除的文件的名字
    key = filename
    ret, info = bucket.delete(bucket_name, key)
    return info
Exemple #30
0
def remove_file(username, path, filename, bucket=None):  # 删除文件 删除目录  ok
    user = User.objects.filter(username=username)
    if bool(user):
        user = user[0]
    else:
        return False
    if bucket == None:
        q = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY)  # 授权
        bucket = BucketManager(q)
        if not check_str(filename) or not check_str(path, ispath=True):
            return False
        file1 = FileInfo.objects.filter(owner=user,
                                        file_name=filename,
                                        file_path=path)
        if bool(file1):
            print "i'm in'"
            print username, path, -file1[0].size
            update_dir_size(username, path, -file1[0].size)
    file1 = FileInfo.objects.filter(owner=user,
                                    file_name=filename,
                                    file_path=path)
    if bool(file1):
        file1 = file1[0]
    else:
        return False
    file_type = file1.file_type
    file1.delete()
    if path == '/':
        path = ''
    if file_type == 'dir':
        subpath = ''.join([path, '/', filename])
        print subpath
        files = FileInfo.objects.filter(owner=user, file_path=subpath)
        for f in files:
            print f.file_name, f.file_type
            if f.file_type != 'dir':
                key = ''.join([username, subpath, '/', f.file_name])
                ret, info = bucket.delete(settings.QINIU_BUCKET_NAME, key)
                f.delete()
                print info
            # assert ret is None
            # assert info.status_code == 612
            else:
                filename = f.file_name
                remove_file(username, subpath, filename, bucket)

    else:
        key = ''.join([username, path, '/', filename])
        ret, info = bucket.delete(settings.QINIU_BUCKET_NAME, key)
        print info
    # assert ret is None
    # assert info.status_code == 612
    return True
Exemple #31
0
def delete_qiniu_file(filename):
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)
    # 要上传的空间
    bucket_name = 'demo'
    reform, fo = bucket.delete(bucket_name, filename)
    print(reform)
    print(fo)
    if reform != None:
        print('已经成功地将{}->>X'.format(filename))
    else:
        raise Exception('七牛云删除文件失败')
Exemple #32
0
def qiniu_file(pre_text, limit):
    q = qiniu_q()
    bucket = BucketManager(q)
    bucket_name = 'love-travel'
    ret, eof, info = bucket.list(bucket_name,
                                 prefix=pre_text,
                                 delimiter="/",
                                 limit=limit)
    if len(ret["items"]) > 0:
        return {'result': True, 'files': ret["items"]}
    else:
        return {'result': False}
Exemple #33
0
 def list_file_from_qiniu(self):
     info = None
     prefix = 'images'
     limit = None
     delimiter = None
     marker = None
     try:
         bucket = BucketManager(q)
         ret, eof, info = bucket.list(BUCKET_NAME, prefix, marker, limit,
                                      delimiter)
     except Exception, e:
         pass
def get_file_from_qiniu():
    print("[*] Try to get file from qiniu bucket...")
    qiniu_bucket = BucketManager(auth=Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY))
    ret, eof, info = qiniu_bucket.list(settings.QINIU_BUCKET_NAME)

    items = ret.get("items")
    if not items:
        print(ret)
        exit(1)

    # print(items)
    print("[*] Get file success, count: {}".format(len(items)))
    return items
Exemple #35
0
def delete(fileName):
    access_key = 'access_key'
    secret_key = 'secret_key'
    #初始化Auth状态
    q = Auth(access_key, secret_key)
    #初始化BucketManager
    bucket = BucketManager(q)
    #你要测试的空间, 并且这个key在你空间中存在
    bucket_name = 'cesai'
    key = fileName
    #删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, key)
    return info
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
Exemple #37
0
def delete_file(bucket_name, key):
    global access_key
    global secret_key
    global q
    bucket = BucketManager(q)
    ret, info = bucket.stat(bucket_name, key)
    if ret and ('hash' in ret):
        ret, info = bucket.delete(bucket_name, key)
        #print(ret)
        #print(info)
        print("%s delete in qiniu"%basename(key))
    else:
        print("%s not exist in qiniu"%basename(key))
Exemple #38
0
 def __init__(self, config, *args, **kw):
     assert isinstance(config, dict)
     assert isinstance(config.get("bucket"), str)
     assert isinstance(config.get("access_key"), str)
     assert isinstance(config.get("secret_key"), str)
     assert isinstance(config.get("domain"), str)
     self.__access_key = config.get("access_key")
     self.__secret_key = config.get("secret_key")
     self.__bucket = config.get("bucket")
     self.__domain = config.get("domain")
     self.__auth = Auth(self.__access_key, self.__secret_key)
     self.__bucket_manager = BucketManager(self.__auth)
     self.__file_info_cache = {}
Exemple #39
0
def delete_data(urls, bucket='lucky-web', key_prefix=''):
    keys = []
    for url in urls:
        if not url.endswith('/'):
            url += '/'
        key = url.split('/')[-2]
        if key_prefix:
            key = '%s/%s' % (key_prefix, key)
        keys.append(key)

    ops = build_batch_delete(bucket, keys)
    b = BucketManager(Q)
    ret, info = b.batch(ops)
def list_wrong_files(bucket_name, bucket=None, prefix=None, limit=None):
    if bucket is None:
        bucket = BucketManager(q)
    marker = None
    eof = False
    files_key = []
    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']:
            if item['fsize'] == 85:
                files_key.append(item['key'])
    return files_key
Exemple #41
0
 def fetch_files_from_net_to_qiniu(self, bucket_name, pathdict={}):
     """Args:
     bucket_name: 'bucket_name'
     pathdict: {'source_file_name':'target_file_name',...}
     """
     bucket = BucketManager(self.__auth)
     rets = []
     infos = []
     for p in pathdict.keys():
         ret, info = bucket.fetch(pathdict[p], bucket_name, p)
         rets.append(ret)
         infos.append(info)
     return rets, infos
def delete_img():
    filename = request.args.get('filename', '')
    if filename == '':
        return jsonify({'code': 0, 'err': '请求类型不正确'})
    else:
        access_key = current_app.config['QINIU_ACCESS_KEY']
        secret_key = current_app.config['QINIU_SECRET_KEY']
        q = Auth(access_key, secret_key)
        bucket_name = 'blogimage'
        bucket = BucketManager(q)
        delete_key = filename
        bucket.delete(bucket_name, delete_key)
        return jsonify({'code': 1})
Exemple #43
0
    def upload_url(self,s_url,f_name):
        bucket_name = 'knightcomment'
        q = Auth(self.access_key, self.secret_key)

        bucket = BucketManager(q)
        ret,info = bucket.fetch(s_url, bucket_name, f_name)
        print("response:", ret)
        print("response:", info)
        # ret_dic = json.loads(info)
        status = False
        if info.status_code == 200:
            status = True
        return status
Exemple #44
0
def delete_data(urls, bucket='lucky-web', key_prefix=''):
    keys = []
    for url in urls:
        if not url.endswith('/'):
            url += '/'
        key = url.split('/')[-2]
        if key_prefix:
            key = '%s/%s' % (key_prefix, key)
        keys.append(key)

    ops = build_batch_delete(bucket, keys)
    b = BucketManager(Q)
    ret, info = b.batch(ops)
Exemple #45
0
 def uploadPhotoToQiniu(self, articleObj):
     access_key = "cYb6oCjYozsv4kl3V5SAkpxkzpcjD_Ib8UJ2zFwE"
     secret_key = "1qcjHRvUhrfuI9-0q8A2VruJlsongo_FHEeJKxMS"
     bucket_name = "meinv"
     q = qiniu.Auth(access_key, secret_key)
     bucket = BucketManager(q)
     ret, info = bucket.fetch(articleObj["coverUrl"], bucket_name)
     if ret is not None:
         print u"成功保存", ret["hash"]
         self.uploadToDatabase(ret["hash"], articleObj["articleName"])
     else:
         print (info)
         raise Exception("上传到7牛可能超时了")
Exemple #46
0
 def __init__(self, type):
     ## private variable begin with '__'
     # qiniu配置初始化
     self.bucket_name = BUCKET
     self.upload_pre = BUCKET_PRE
     self.__auth = Auth(ACCESS_KEY, SECRET_KEY)
     self.__bucket = BucketManager(self.__auth)
     # redis连接
     self.redis = redis.StrictRedis(host='localhost', port=6379, db=0)
     # 数据库orm连接
     self.session = Sy_Session.get_session()
     self.req = Sy_Request()
     self.type = type
Exemple #47
0
def _list(access_key, secret_key, bucket_name, file_path, prefix, start, end, fileType, suffix, limit=1000,
          delimiter=None,
          marker=None, fsize=False, stype=-1):
    q = Auth(access_key, secret_key)
    bucket = BucketManager(q)
    ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter)
    if eof:
        marker = None
        _filter_listinfo(ret, file_path, start, end, fileType, suffix, fsize, stype)
    else:
        if info.status_code == 200:
            marker = ret.get("marker")
    return ret, marker, info
Exemple #48
0
 def fetch_files_from_net_to_qiniu(self,bucket_name,pathdict={}):
     """Args:
     bucket_name: 'bucket_name'
     pathdict: {'source_file_name':'target_file_name',...}
     """
     bucket = BucketManager(self.__auth)
     rets=[]
     infos=[]
     for p in pathdict.keys():
         ret, info = bucket.fetch(pathdict[p], bucket_name,p)
         rets.append(ret)
         infos.append(info)
     return rets,infos
def delete_files(bucket_name, files_key, bucket=None):
    if bucket is None:
        bucket = BucketManager(q)
    if len(files_key) == 0:
        logging.info('nothing to delete')
        return
    ops = build_batch_delete(bucket_name,files_key)

    ret,info = bucket.batch(ops)

    if ret[0]['code'] == 200:
        logging.info('delete all success!')
    else:
        logging.error('delete failed!')
Exemple #50
0
 def delete_one_object(self,bucketName,keyName):
     times = 0
     while times <= self.retryTimes:
         times += 1
         #初始化BucketManager
         bucket = BucketManager(self.q)
         try:
             ret, info = bucket.delete(bucketName, keyName)
             if info.status_code == 200:
                 return True
             else:
                 return None
         except Exception,e:
             log.error(e)
Exemple #51
0
def delete_file():
    access_key = 'AgalBXSUPi_LPe9E4nmQrMkVSD67ttNiu707HRjD'
    secret_key = 'A_PtalvHhaUSovjSMDq9h7FgGIZclvRle3M90KSg'
    #初始化Auth状态
    q = Auth(access_key, secret_key)
    #初始化BucketManager
    bucket = BucketManager(q)
    #你要测试的空间, 并且这个key在你空间中存在
    bucket_name = 'flybear'
    key = 'test.xlsx'
    #删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, key)
    print(info)
    assert ret == {}
Exemple #52
0
def delete_qiniu(filename):
    # 需要填写你的 Access Key 和 Secret Key
    access_key = 'r5pmxxVGpZPYqKjOvuYRo2gWWF0ejLq5O3i66e9A'
    secret_key = 'Gl8VuDSdRS0yop2l7xVEAk8uv7PIw-eQQ1_r4lm4'
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'myareazxy'
    # 初始化BucketManager
    bucket = BucketManager(q)
    # key就是要删除的文件的名字
    key = filename
    ret, info = bucket.delete(bucket_name, key)
    return info
Exemple #53
0
def save_img(img_url, file_name):
    '''
    将url上的图片文件保存到七牛云
    :param img_url: 图片地址
    :param file_name: 保存文件名称
    :return: 保存图片信息
    '''
    bucket_name = BUCKET
    q = Auth(QINIU_AK, QINIU_SK)
    bucket = BucketManager(q)
    url = img_url
    key = file_name
    ret, info = bucket.fetch(url, bucket_name, key)
    return info
Exemple #54
0
    def delete(self, filepath):
        """
        删除文件
        :param filename: 文件路径,数据库中取出链接直接传过来就行
        :return: info信息
                _ResponseInfo__response:<Response [200]>, exception:None, status_code:200, text_body:, req_id:VX8AAAAFqYEUYxAW, VX8AAAAFqYEUYxAW, x_log:-

        """
        filename = filepath[33:]
        print(filename)
        q = Auth(self.__access_key, self.__secret_key)
        bucket = BucketManager(q)
        ret, info = bucket.delete(self.__bucket_name, filename)
        return info
Exemple #55
0
def delete(file_key):

    #初始化Auth状态
    q = Auth(access_key, secret_key)

    #初始化BucketManager
    bucket = BucketManager(q)

    #你要测试的空间, 并且这个key在你空

    #删除bucket_name 中的文件 key
    ret, info = bucket.delete(bucket_name, file_key)

    return ret
Exemple #56
0
def delete_qiniu(filename):
    # 需要填写你的 Access Key 和 Secret Key
    access_key = '1fXvG9wkbN7AgRUG6usHDcRP5Bb85apcovRAIITP'
    secret_key = 'Aqf1lPAmUG72EdZJ7PxKtWHfWDYNdUycZP1TaAIN'
    # 构建鉴权对象
    q = Auth(access_key, secret_key)
    # 要上传的空间
    bucket_name = 'myblog202006'
    # 初始化BucketManager
    bucket = BucketManager(q)
    # key就是要删除的文件的名字
    key = filename
    ret, info = bucket.delete(bucket_name, key)
    return info
Exemple #57
0
def remove_file(username,path,filename,bucket=None):  # 删除文件 删除目录  ok
    user = User.objects.filter(username=username)
    if bool(user):
        user = user[0]
    else :
        return False
    if bucket == None:
        q = Auth(settings.QINIU_ACCESS_KEY, settings.QINIU_SECRET_KEY)  # 授权
        bucket = BucketManager(q)
        if  not check_str(filename) or not check_str(path,ispath=True):
            return False
        file1 =FileInfo.objects.filter(owner=user,file_name=filename,file_path=path)
        if bool(file1):
            print "i'm in'"
            print username,path,-file1[0].size
            update_dir_size(username,path,-file1[0].size)
    file1 =FileInfo.objects.filter(owner=user,file_name=filename,file_path=path)
    if bool(file1):
        file1 = file1[0]
    else:
        return False
    file_type = file1.file_type
    file1.delete()
    if path == '/':
        path = ''
    if file_type == 'dir':
        subpath = ''.join([path,'/',filename])
        print subpath
        files = FileInfo.objects.filter(owner=user,file_path=subpath)
        for f in files:
            print f.file_name,f.file_type
            if f.file_type != 'dir':
                key = ''.join([username, subpath , '/', f.file_name])
                ret, info = bucket.delete(settings.QINIU_BUCKET_NAME, key)
                f.delete()
                print info
               # assert ret is None
               # assert info.status_code == 612
            else:
                filename =f.file_name
                remove_file(username,subpath,filename,bucket)

    else:
        key = ''.join([username, path, '/', filename])
        ret, info = bucket.delete(settings.QINIU_BUCKET_NAME, key)
        print info
       # assert ret is None
       # assert info.status_code == 612
    return True
Exemple #58
0
def updateFile(filePath, uri):
    q = Auth(access_key, secret_key)
    bucket_name = defaultBucketName
    bucket = BucketManager(q)
    key = getKeyFromUri(uri)
    # delete
    ret, info = bucket.delete(bucket_name, key)
    # upload
    token = q.upload_token(bucket_name, key)
    ret, info = put_file(token, key, filePath)
    if 'key' not in ret:
        return None
    retFileUri = ('%s/%s') % (defaultBucketUriBase, key)
    logging.debug('upload qiniu file uri:[%r]' % (retFileUri))
    return retFileUri
Exemple #59
0
    def update_image_source(self,bucket_name,pathlist=[]):
        """Args:
        bucket_name: 'bucket_name'
        pathlist: ['file_name',...]
        !需要提前对仓库设置镜像源!
        """

        bucket = BucketManager(self.__auth)
        rets=[]
        infos=[]
        for p in pathlist:
            ret, info = bucket.prefetch(bucket_name, p)
            rets.append(ret)
            infos.append(info)
        return rets,infos
	def _upload_zhihu_user_img_qiniu(self, img_name, img_place):
		if UPLOAD_FLAG == True:
			q = Auth(ACCESS_KEY, SECRET_KEY)
			bucket = BucketManager(q)
			mime_type = 'image/jpeg'
			#delete the same file
			del_ret, del_info = bucket.delete(BUCKET_NAME, img_name)
			#insert the new file
			token = q.upload_token(BUCKET_NAME, img_name)
			insert_ret, insert_info = put_file(token, img_name, img_place+'/'+img_name+'.jpg', mime_type=mime_type, check_crc=True)
			if insert_info is not None and str(insert_info).split(',')[1].strip()=="status_code:200":
				print '['+self._now_time()+']'+"upload img:"+img_name+' to bucket:'+BUCKET_NAME+'successfully...'
				return True
			else:
				return False