def delete(self, filename): """ 删除 """ auth = qiniu.Auth(self._access_key, self._secret_key) bucket = qiniu.BucketManager(auth) return bucket.delete(self._bucket_name, filename)
def list(bucket, prefix, output): auth = qiniu.Auth(accessKey, secretKey) bkm = qiniu.BucketManager(auth) eof = False marker = None fp = open(output, "w") while not eof: ret, eof, info = bkm.list(bucket, prefix=prefix, marker=marker, limit=None, delimiter=None) if ret and not eof: marker = ret.get("marker") if ret: items = ret.get("items") for item in items: key = item["key"] putTime = item["putTime"] hash = item["hash"] fsize = item["fsize"] mimeType = item["mimeType"] data = "{0}\t{1}\t{2}\t{3}\t{4}".format( key.encode("utf-8"), fsize, putTime, mimeType, hash) fp.write(data) fp.write("\r\n") fp.flush() fp.close()
def delete(self, name): bucket = qiniu.BucketManager(self.q) ret, info = bucket.delete(self.bucket_name, name) if ret: return True else: raise Exception('delete qiniu file error, detail: {}'.format(info))
def __init__(self, acces_key, secret_key, bucket): # 1-1: 构建权限对象 self.q = qiniu.Auth(acces_key, secret_key) # 1-2: 生成token: 300秒有效 self.token = self.q.upload_token(bucket, None, 300) self.bucket = bucket self.bucket_api = qiniu.BucketManager(self.q)
def delete_fonts(self, keyword='', pref='', prefix=config.qiniu_fonts_prefix): fonts_list = self.get_fonts(keyword=keyword, pref=pref, prefix=prefix) delete_fonts_object = list( filter( lambda fonts_file: keyword in fonts_file['key'] and fonts_file[ 'key'].split(prefix)[-1].startswith(pref), fonts_list)) delete_fonts = list( map(lambda font_object: font_object['key'], delete_fonts_object)) if not delete_fonts: print('No fonts matched for delete in qiniu fonts bucket') else: print('Fonts to be deleted %s' % delete_fonts) self.logger.info('Fonts to be deleted %s' % delete_fonts) bucket = qiniu.BucketManager(self.auth) ops = qiniu.build_batch_delete(config.qiniu_fonts_bucket, delete_fonts) res, info = bucket.batch(ops) print(res, info) assert info.status_code == 200 print('Fonts %s delete success' % delete_fonts) self.logger.info('Fonts %s delete success' % delete_fonts)
def delete(): key = request.form.get('key', '') if not key: return jsonify({'result': 'fail'}) real_key = (g.user.account + key).encode('utf8') bucket = qiniu.BucketManager(qn) if real_key[-1] != '/': ret, info = bucket.delete(qiniu_bucket, real_key) if ret: return jsonify({'result': 'fail'}) else: items = [] marker = None while True: ret, eof, _ = bucket.list(qiniu_bucket, prefix=real_key, marker=marker, limit=1000) for item in ret['items']: items.append(item['key'].encode('utf8')) if eof: break marker = ret['marker'] ops = qiniu.build_batch_delete(qiniu_bucket, items) ret, info = bucket.batch(ops) if ret[0]['code'] != 200: return jsonify({'result': 'fail'}) return jsonify({'result': 'success'})
def __init__(self, access, secret): self.access_key = access self.secret_key = secret self._q = qiniu.Auth(self.access_key, self.secret_key) self._bucket = qiniu.BucketManager(self._q) self.mcu_url = None self.file_list_size = 10
def __init__(self, parent, auth): wx.Panel.__init__(self, parent) self.__frame = self.GetParent().GetParent() self.__auth = auth self.__bucketManger = qiniu.BucketManager(self.__auth) self.__conf = Config() self.__bucketName = "" self.__download_url = None self.__prefix = "" self.__marker = None self.__limit = LIMIT self.__initImages() self.__boxSizer = wx.BoxSizer() self.__dataList = self.__initList() self.__boxSizer.Add(self.__dataList, 1, wx.EXPAND) self.__initPopMenu() self.SetSizer(self.__boxSizer) # init column sorter wx.lib.mixins.listctrl.ColumnSorterMixin.__init__(self, LIMIT)
def list(self, path, **kwargs): path = self._wrap_path(path) token = self._get_token() bucket = qiniu.BucketManager(self.q) ret, eof, info = bucket.list(self.bucket_name, 'sshr-', None, None) return [item['key'][5:] for item in ret.get('items')]
def delete_file(bucket_name, key): q = get_quniu_auth() bucket = qiniu.BucketManager(q) ops = qiniu.build_batch_delete(bucket_name, [key]) ret, info = bucket.batch(ops) return ret, info
def init_extra(self): storage = self.storage access_key = storage.meta['access-key'] secret_key = storage.meta['secret-key'] self.domain = storage.meta['domain'] self.auth = qiniu.Auth(access_key, secret_key) self.bucket_manager = qiniu.BucketManager(self.auth) self.bucket_name = storage.meta['bucket']
def delete(self, key): # try: #初始化Auth状态 q = qiniu.Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY) #初始化BucketManager bucket = qiniu.BucketManager(q) #删除bucket_name 中的文件 key #你要测试的空间, 并且这个key在你空间中存在 ret, info = bucket.delete(QINIU_BUCKET_NAME, key) print(info)
def __init__(self): qiniu_args = extract_args(( 'QINIU_AK', 'QINIU_SK', 'QINIU_BUDGET', )) self._q = qiniu_args self._t = qiniu.Auth(qiniu_args['QINIU_AK'], qiniu_args['QINIU_SK']) self.bucket = qiniu.BucketManager(self._t)
def delete(self, key): # try: #初始化Auth状态 q = qiniu.Auth(qiniu_access_key, qiniu_secret_key) #初始化BucketManager bucket = qiniu.BucketManager(q) #删除bucket_name 中的文件 key #你要测试的空间, 并且这个key在你空间中存在 ret, info = bucket.delete(qiniu_bucket_name, key) print(info)
def fetch(remoteUrl, bucket, key): auth = qiniu.Auth(accessKey, secretKey) bm = qiniu.BucketManager(auth) retData, respInfo = bm.fetch(remoteUrl, bucket, key) if respInfo.status_code == 200: print("Fetch success!") elif respInfo.status_code == 401: print("Unauthorized error, please set your access key and secret key") else: print("Error: " + respInfo.error)
def move(srcBucket, srcKey, destBucket, destKey): auth = qiniu.Auth(accessKey, secretKey) bm = qiniu.BucketManager(auth) retData, respInfo = bm.move(srcBucket, srcKey, destBucket, destKey) if respInfo.status_code == 200: print("Move success!") elif respInfo.status_code == 401: print("Unauthorized error, please set your access key and secret key") else: print("Error: " + respInfo.error)
def change_mimetype(bucket, key, newMimeType): auth = qiniu.Auth(accessKey, secretKey) bm = qiniu.BucketManager(auth) retData, respInfo = bm.change_mime(bucket, key, newMimeType) if respInfo.status_code == 200: print("Change mime type success!") elif respInfo.status_code == 401: print("Unauthorized error, please set your access key and secret key") else: print("Error: " + respInfo.error)
def onAuth(self): ak = str(self.accessKey.GetLabelText()) sk = str(self.secretKey.GetLabelText()) buckets, info = qiniu.BucketManager(qiniu.Auth(ak, sk)).buckets() if info.ok(): if self.rememberMe.IsChecked(): self.conf.setKeys((ak, sk)) self.onLoginSuccess(buckets) else: self.onAuthError(info.error)
def qiniu_fetch(bucket_name, resource_url, filename=None): ''' 由七牛抓取网络资源到空间 Args: bucket_name: 空间名 resource_url: 网络资源地址 filename: 上传后的文件名 ''' bucket = qiniu.BucketManager(authorization) ret, info = bucket.fetch(resource_url, bucket_name, filename) return ret, info
def __init__(self, access_key_id, access_key_secret, bucket_name, endpoint='', cdn_endpoint=''): super(QiniuBucket, self).__init__(access_key_id, access_key_secret, bucket_name, endpoint, cdn_endpoint) self.bucket_name = bucket_name self.auth = qiniu.Auth(access_key_id, access_key_secret) self.bucket = qiniu.BucketManager(self.auth)
def qiniu_fetch_img(img_url, img_name): q = qiniu.Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY) token = q.upload_token(QINIU_BUCKET_NAME) bucket = qiniu.BucketManager(q) ret, info = bucket.fetch(img_url, QINIU_BUCKET_NAME, img_name) if not ret: return None key = ret.get('key') return QINIU_URL + key
def mergeAllDayDataIntoLocalDailyFile(qiniuLocation, qiniuDate, localFile): ''' Get all the files from one day folder and merge them into one file ''' try: logger.debug('''>>> Enter mergeAllDayDataIntoOneFile, check location {qiniuLocation}, and save to {localFile}'''.format(**locals())) logger.debug('Start get auth') q = qiniu.Auth(constants.QINIU_AK(), constants.QINIU_SK()) logger.debug('finish get auth') count = 0 fileCount = 0 create_file(localFile) marker = None eof = False while not eof: ret, eof, info = qiniu.BucketManager(q).list(qiniuLocation, qiniuDate, marker=marker) marker = ret.get('marker', None) allDayTexts = json.loads(info.text_body).get('items') logger.debug('find %s files' % str(len(allDayTexts))) fileCount += len(allDayTexts) with open(localFile, "a") as myfile: finished = 0 for dayText in allDayTexts: key = dayText.get('key') url = 'http://{domain}/{key}'.format( domain=constants.QINIU_PUBLIC_DOMAIN_NAME(), key=key) logger.debug('----------finised :' + str(finished)) finished += 1 if url.endswith('txt'): userid = url.split('_')[0].split('-')[-1] private_url = q.private_download_url(url, expires=3600) r = requests.get(private_url) r.encoding = 'utf-8' for row in r.text.split('\n')[1:]: if len(row) > 3: count += 1 #only focus on the last 24 columns newRow = ','.join( row.split(',')[-24:]).decode('utf-8') #folder.split('/')[0] is the ds myfile.write(qiniuDate + ',' + userid + ',' + newRow + '\n') logger.debug('done one iteration') logger.debug('''<<< Exit mergeAllDayDataIntoLocalDailyFile, find %s item in the folder''' % count) except Exception, e: logger.error('!!!!!! Error in checkFolderExistInQiniu' + str(e)) raise e
def listBucket(request): bucket_name = request.GET.get('bucket','') if AK=='' or SK=='': return HttpResponse('Empty AK or SK!') else: if bucket_name == '': return HttpResponse('Empty bucket name!') else: q = qiniu.Auth(AK, SK) bucket = qiniu.BucketManager(q) ret, eof, info = bucket.list(bucket_name) print type(ret) data = json.dumps(ret,ensure_ascii=False) return HttpResponse(data)
def rename(): key = request.form.get('key', '') new_name = request.form.get('name', '') if not key or not new_name: return jsonify({'result': 'fail'}) old_name = key.split('/')[-1] if old_name == new_name: return jsonify({'result': 'success'}) old_key = (g.user.account + key).encode('utf8') new_key = (g.user.account + key[:-len(old_name)] + new_name).encode('utf8') bucket = qiniu.BucketManager(qn) ret, info = bucket.move(qiniu_bucket, old_key, qiniu_bucket, new_key) if ret: return jsonify({'result': 'fail'}) return jsonify({'result': 'success'})
def get_fonts(self, keyword='', pref='', prefix=config.qiniu_fonts_prefix): bucket = qiniu.BucketManager(self.auth) ret, eof, info = bucket.list(config.qiniu_fonts_bucket, prefix) fonts_object_list = list( filter( lambda font_info: font_info['key'].startswith(prefix) and keyword in font_info['key'] and ''.join(font_info['key'].split( prefix)[-1]).startswith(pref), ret['items'])) # fonts_list = list(map(lambda font_info: font_info['key'].split(config.qiniu_fonts_prefix)[1], ret['items'])) self.logger.info('There was %s fonts matched in qiniu' % len(fonts_object_list)) print('There was %s fonts matched in qiniu' % len(fonts_object_list)) # print(fonts_object_list) return fonts_object_list
def stat(bucket, key): auth = qiniu.Auth(accessKey, secretKey) bm = qiniu.BucketManager(auth) retData, respInfo = bm.stat(bucket, key) if retData != None: print("Stat Result:") print(" File Size:\t" + str(retData["fsize"]) + " Bytes") print(" Hash:\t\t" + retData["hash"]) print(" MimeType:\t" + retData["mimeType"]) print(" PutTime:\t" + time.ctime(retData["putTime"] / 10000000)) else: if respInfo.status_code == 401: print( "Unauthorized error, please set your access key and secret key" ) else: print("Error: " + respInfo.error)
def disk(prefix=None): if prefix: if prefix[-1] != '/': prefix = prefix + '/' else: prefix = '' real_prefix = g.user.account + '/' + prefix parents = [] parents.append({'name': 'Home', 'url': url_for('disk')}) current_path = '' for folder in prefix.split('/')[:-1]: if folder: parents.append({'name': folder, 'url': url_for('disk') + current_path + folder + '/'}) current_path = current_path + folder + '/' bucket = qiniu.BucketManager(qn) marker = None items = [] folders = [] while True: ret, eof, _ = bucket.list(qiniu_bucket, prefix=real_prefix, marker=marker, limit=1000) for item in ret['items']: name = item['key'][len(real_prefix):] if not name: continue if '/' in name: folder = name.split('/')[0] if folder and folder not in folders: folders.append(folder) else: items.append({'key': item['key'][len(g.user.account):], 'name': name, 'modified': datetime.datetime.fromtimestamp(int(item['putTime'] / 1e7)).strftime('%Y-%m-%d %H:%M:%S'), 'size': human_size(item['fsize'])}) if eof: break marker = ret['marker'] folders.sort() items.sort(key=lambda item: item['name']) return render_template('disk.html', prefix=prefix, parents=parents, folders=folders, items=items)
def batch_stat(paramFile): batchOps = [] fp = open(paramFile, mode="r") for line in fp: items = line.split("\t") if len(items) == 2: bucketPart = items[0].strip() keyPart = items[1].strip() encodedEntry = qiniu.entry(bucketPart, keyPart) batchOps.append("/stat/" + encodedEntry) else: print("Invalid stat line `" + line.strip() + "'") fp.close() auth = qiniu.Auth(accessKey, secretKey) bm = qiniu.BucketManager(auth) _, respInfo = bm.batch(batchOps) if respInfo.status_code == 401: print("Unauthorized error, please set your access key and secret key") elif respInfo.status_code == 200 or respInfo.status_code == 298: textBody = respInfo.text_body statDataList = json.loads(textBody) for statData in statDataList: print("Code: " + str(statData["code"])) fileInfo = statData["data"] if fileInfo.__contains__("error"): print("Error: " + fileInfo["error"]) else: print("Stat Result:") print(" File Size:\t" + str(fileInfo["fsize"]) + " Bytes") print(" Hash:\t\t" + fileInfo["hash"]) print(" MimeType:\t" + fileInfo["mimeType"]) print(" PutTime:\t" + time.ctime(fileInfo["putTime"] / 10000000)) print( "---------------------------------------------------------------" ) else: print(respInfo)
def qiniu_get_file_list(a_key, s_key, prefix, bucket_name): q = qiniu.Auth(a_key, s_key) bucket = qiniu.BucketManager(q) limit = 255 delimiter = None marker = None format_time = [] ret, eof, info = bucket.list(bucket_name, prefix, marker, limit, delimiter) info_body = info.text_body if ret is None: return None, None, None file_name = re.findall(r'"key":(.+?),', str(info_body)) file_fsize = re.findall(r'"fsize":(.+?),', str(info_body)) file_puttime = re.findall(r'"putTime":(.+?),', str(info_body)) for seconds in file_puttime: t = datetime.datetime.fromtimestamp( int(seconds) / 10000000).strftime('%Y-%m-%d %H:%M:%S') format_time.append(t) return file_name, file_fsize, format_time
def delete(self, key): # try: #初始化Auth状态 q = qiniu.Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY) #初始化BucketManager bucket = qiniu.BucketManager(q) #删除bucket_name 中的文件 key #你要测试的空间, 并且这个key在你空间中存在 ret, info = bucket.delete(QINIU_BUCKET_NAME, key) print(info) # return True # except Exception as e: # print 'error',e # return False # policy = { # # "callbackUrl":"http://120.27.97.33/upload/token/", # "callbackUrl":"https://www.12xiong.top/upload/token/", # "callbackBody":"key=$(key)&hash=$(etag)&w=$(imageInfo.width)&h=$(imageInfo.height)&duration=$(avinfo.video.duration)&fsize=$(fsize)&vw=$(avinfo.video.width)&vh=$(avinfo.video.height)", # "callbackHost":"120.27.97.33", # "fsizeLimit": 6815744, # # "mimeLimit": "image/png" # }