def flush_resource(url): cdn_manager = CdnManager(q) # 需要刷新的文件链接 urls = [url] # 刷新链接 refresh_url_result = cdn_manager.refresh_urls(urls) print(refresh_url_result)
def update(auth): print('刷新') cdn_manager = CdnManager(auth) # 需要刷新的文件链接 urls = ['{}/{}'.format(Config.link, Config.file)] # 刷新链接 refresh_url_result = cdn_manager.refresh_urls(urls) print(refresh_url_result)
def generate(card): try: msstream = BytesIO() data = { 'copyright': 'open', 'id': card.id, 'title': card.get('name'), 'content': card.get('content'), 'author': card.get('author'), 'url': card.get('img_url') } Movie.movie(data, msstream) url = os.environ["QINIU_ACCESS_URL"] access_key = os.environ["QINIU_ACCESS_KEY"] secret_key = os.environ["QINIU_SECRET_KEY"] #构建鉴权对象 q = Auth(access_key, secret_key) #要上传的空间 bucket_name = 'card' key = card.get('objectId') if card.get('photo') is None: token = q.upload_token(bucket_name) ret, info = put_data(token, key, msstream.getvalue()) else: cdn_manager = CdnManager(q) urls = ['http://oppyrwj3t.bkt.clouddn.com/' + key] token = q.upload_token(bucket_name, key) ret, info = put_data(token, key, msstream.getvalue()) refresh_url_result = cdn_manager.refresh_urls(urls) if (info.ok()): metaData = {'owner': card.get('username')} photo = Photo() photo.set('mine_type', 'image/jpeg') photo.set('key', key) photo.set('name', key) photo.set('url', url + '/' + key) photo.set('provider', 'qiniu') photo.set('metaData', metaData) photo.set('bucket', bucket_name) photo.save() update = Card.create_without_data(key) update.set('photo', photo) update.save() return 'ok' else: return 'failed' return 'already' except LeanCloudError as e: if e.code == 101: # 服务端对应的 Class 还没创建 card = '' return HttpResponse(e, content_type="text/plain") else: raise e return HttpResponse(e, content_type="text/plain")
def __init__(self, access_key, secret_key, bucket_name, bucket_domain): self.bucket_name = bucket_name self.bucket_domain = bucket_domain # 构建鉴权对象 q = Auth(access_key, secret_key) self.q = q self.cdn_manager = CdnManager(q) #初始化BucketManager self.bucket = BucketManager(q) self.file2refresh = []
def refresh_data(q, url): cdn_manager = CdnManager(q) # 需要刷新的文件链接 urls = [ url, # 'https://tmn07.com/rank_v1911/events_list.json', ] # 刷新链接 refresh_url_result = cdn_manager.refresh_urls(urls) return refresh_url_result
def clear_qiniu(url): # 账户ak,sk access_key = 'MeUQwQAvlOJ7C-wVasdfcCsJIvq0PppRT3VrYKog34xv' secret_key = 'wAwXfUY12YD3vkHa_Qqi9S3F31RF6F8reIP7cWj-jj' auth = qiniu.Auth(access_key=access_key, secret_key=secret_key) cdn_manager = CdnManager(auth) # 需要刷新的文件链接 urls = [url] # 刷新链接 refresh_url_result = cdn_manager.refresh_urls(urls) return refresh_url_result[0]['requestId']
def upload(local_file, bucket_name, ak, sk, bucket_host=None, prefix=None, key=None): """ 上传指定文件到指定bucket Args: ---- local_file: 待上传文件路径 bucket_name: 指定上传的bucket ak: bucket 所在账号ak sk: bucket 所在账号sk bucket_host: bucket 对应的外链域名, 若不指定, 则动态获取 prefix: 上传前缀, 若不指定, 则无前缀 key: 上传后保存的文件名, 若不指定, 则与本地文件同名 Return: ------ url: 上传文件的访问外链 """ auth = Auth(ak, sk) if bucket_host is None: # 获取bucket外链 bucket_host = get_bucket_host(bucket_name, auth) if key is None: key = os.path.split(local_file)[-1] if prefix is not None: key = os.path.join(prefix, key) token = auth.upload_token(bucket_name, key, 3600) ret, info = put_file(token, key, local_file) assert ret['key'] == key assert ret['hash'] == etag(local_file) url = bucket_host + '/' + key # 强制刷新CDN cdn_manager = CdnManager(auth) cdn_manager.refresh_urls([url]) # refresh_cdn(url, auth) #rd = int(round(time.time()*1000)) #url = url + '?v=%d'%rd return url
def publish(self, target_url, credentials=None, **extra): # read auth keys from config file which they name shoudl be "qiniu.ini" config = self.get_config('qiniu.ini') access_key = config.get('auth.Access_Key') secret_key = config.get('auth.Secret_Key') bucket_name, key_prefix = self.split_bucket_uri(target_url) # prepare local files and upload status variables local = self.list_local() qn = Auth(access_key, secret_key) start_time = datetime.now() yield "### start to upload to bucket %s %s " % (bucket_name, key_prefix) counter = 0 num_files = len(local) ### upload files for filename in local: counter = counter+1 abs_path = os.path.join(self.output_path, filename) yield '### uploading %s ###' % filename key_name = key_prefix+filename token = qn.upload_token(bucket_name,key_name,120) ret,info=put_file(token,key_name,abs_path) if info.status_code ==200: yield '-- Successfully uploaded %s of %s files --' % (str(counter),num_files) else: yield 'something wrong %s' % info.status_code yield 'Full error message: %s' % info last_time = datetime.now()-start_time yield "$$$ Upload Done in %s seconds!" % last_time.seconds ##start to refresh cdn dir if config.get('cdn.refresh_enable') == "yes": yield "start to refresh" cdn_namager = CdnManager(qn) refresh_url = config.get('cdn.refresh_url') if refresh_url[-1]!='/': refresh_url = refresh_url+"/" dir = [refresh_url] refresh_dir_result = cdn_namager.refresh_dirs(dir) print(refresh_dir_result) if refresh_dir_result[0]['code'] ==200: yield "refresh complete!" else: yield "Error refreshing: %s" % refresh_dir_result[0] yield "ALL TASK ARE COMPLETED!"
def qiniu_flux_data(urls): q = Auth(qiniu_ak, qiniu_sk) cdn_manager = CdnManager(q) startDate = time.strftime('%Y-%m-01', time.localtime()) endDate = time.strftime('%Y-%m-%d', time.localtime()) granularity = 'day' ret, info = cdn_manager.get_flux_data(urls, startDate, endDate, granularity) if info.status_code == 200: data = ret['data'] flux_byte_num = 0 for url in data.values(): for area in url.values(): flux_byte_num += sum(area) flux_g_num = flux_byte_num / 1024 / 1024 / 1024 return flux_g_num else: logger.error(f"流量查询失败:{info}")
def sync(self, request, queryset): access_key = settings.QINIU_ACCESS_KEY secret_key = settings.QINIU_SECRET_KEY bucket = settings.QINIU_BUCKET host = settings.QINIU_HOST auth = Auth(access_key, secret_key) filename = __file__.replace(os.path.basename(__file__), '') + '/templates/index.html' uploads = [ 'mirrors/index.html', 'mirrors.json', ] token = auth.upload_token(bucket, uploads[0], 3600) put_file(token, uploads[0], filename) mirrors = Mirror.objects.all() serializer = MirrorSerializer(instance=mirrors, many=True) token = auth.upload_token(bucket, uploads[1], 3600) put_data( token, uploads[1], json.dumps({ 'title': settings.APP_NAME, 'url': settings.MEDIA_URL, 'data': serializer.data, })) cdnManager = CdnManager(auth) for i in range(len(uploads)): uploads[i] = host + '/' + uploads[i] uploads.append(host + '/mirrors/') cdnManager.refresh_urls(uploads) return JsonResponse({'status': 'success', 'msg': '处理成功!'})
def del_fig(ak, sk, png_files, fig_urls, delete=True): BASEURL = "http://p7d7ismcm.bkt.clouddn.com" QINIU = Auth(ak, sk) BUCKET = 'report' bucket = BucketManager(QINIU) if delete: for k in png_files: key = os.path.basename(k) ret, info = bucket.delete(BUCKET, key) if ret == {}: rep = 'Figure has been deleted.' else: rep = 'Error when deleting figure.' print(k, rep) # refresh cdn cache cdn_manager = CdnManager(QINIU) refresh_url_result = cdn_manager.refresh_urls(fig_urls) if refresh_url_result[0]['error'] == u'success': print('CDN has been refreshed') return
# -*- coding: utf-8 -*- # flake8: noqa """ 查询指定域名指定时间段内的流量 """ import qiniu from qiniu import CdnManager # 账户ak,sk access_key = '' secret_key = '' auth = qiniu.Auth(access_key=access_key, secret_key=secret_key) cdn_manager = CdnManager(auth) startDate = '2017-07-20' endDate = '2017-08-20' granularity = 'day' urls = ['a.example.com', 'b.example.com'] # 获得指定域名流量 ret, info = cdn_manager.get_flux_data(urls, startDate, endDate, granularity) print(ret) print(info)
def __init__(self, **kwargs): CDN.__init__(self, **kwargs) self.auth = Auth(self.ak, self.sk) self.bucket_mgr = BucketManager(self.auth) self.cdn_mgr = CdnManager(self.auth) self.old_etags = dict()
if res.status_code != 200: raise Exception("upload failed") return ret, res if __name__ == "__main__": # 不要开代理 access_key = sys.argv[1] secret_key = sys.argv[2] sync = Sync( access_key=access_key, # access_key secret_key=secret_key, # secret_key bucket_name="bi-she", # bucket_name sync_dir="./", # 静态文件目录(后面必须有斜杠/) exclude=[".DS_Store"], cover=True, remove_redundant=True, ) # 刷新缓存 cdn_manager = CdnManager(sync.q) # 需要刷新的文件链接 urls = ['http://aaa.example.com/a.gif', 'http://bbb.example.com/b.jpg'] # URL刷新链接 refresh_url_result = cdn_manager.refresh_urls(urls) # 目录刷新链接 refresh_dir_result = cdn_manager.refresh_dirs(['xxx'])
from qiniu import Auth, CdnManager access_key = 'v_s4L4kwQ-er524cv0ByjdiU7KtwzcaTgb7-y_nU' secret_key = 'e-m9s9sDqfVhFFblA8Xq9eQaMdBPoPJ9AMtrfFLm' q = Auth(access_key, secret_key) cdn = CdnManager(q) urls = ['http://phd6u0gel.bkt.clouddn.com/Geekbench-4.3.0-Linux.tar.gz', 'http://phd6u0gel.bkt.clouddn.com/super_pi.tgz'] refresh_urls_response = cdn.refresh_urls(urls=urls) print(refresh_urls_response)
#需要填写你的 Access Key 和 Secret Key #export QINIU_ACCESS_KEY=xxx access_key = os.environ["QINIU_ACCESS_KEY"] #export QINIU_SECRET_KEY=xxx secret_key = os.environ["QINIU_SECRET_KEY"] #要上传的空间 #export QINIU_BUCKET=xxx bucket_name = os.environ["QINIU_BUCKET"] bucket_domain = os.environ["QINIU_BUCKET_DOMAIN"] # 构建鉴权对象 q = Auth(access_key, secret_key) cdn_manager = CdnManager(q) #初始化BucketManager bucket = BucketManager(q) file2refresh = [] def is_diff(key, localfile): ''' 比较本地文件与文件文件是否一致 ''' ret, info = bucket.stat(bucket_name, key) #print(ret,info) hash = ret if hash: diff = ret['hash'] != etag(localfile) # diff==False,说明文件相同,这个有点绕
def post(self, request): if not request.user.is_authenticated: return JsonResponse({'code': 7, 'msg': '请先登录'}) avatar_file = request.FILES.get('avatar') if not avatar_file: return JsonResponse({'code': 2, 'msg': '请选择需要上传的图片'}) allow_types = ['image/jpeg', 'image/png', 'image/gif'] if avatar_file.content_type not in allow_types: return JsonResponse({'code': 3, 'msg': '上传失败,文件类型错误'}) file_name = '{}_avatar_{}_{}'.format(request.user.username, time.time(), avatar_file.name) fs = FileSystemStorage() fs.save(file_name, avatar_file) file_path = os.path.join(MEDIA_ROOT, file_name) # compress the image i = Image.open(file_path) i.thumbnail((300, 300)) i.save(file_path) access_key = 'M2TrolxfManTFNP4Clr3M12JW0tvAaCV0xIbrZk5' secret_key = 'Llh0byt0KDHwiFlcNVvPiTpQSrH8IrZSt5puu1zS' q = qiniu_auth(access_key, secret_key) bucket_name = 'avatar' redis = get_redis() try: token = q.upload_token(bucket_name, file_name, 3600) ret, info = put_file(token, file_name, file_path) assert ret['key'] == file_name assert ret['hash'] == etag(file_path) except Exception as e: return JsonResponse({'code': 4, 'msg': '上传文件出错'}) finally: # 删除本地 fs.delete(file_name) # 删除以前头像地址 used_avatar = redis.hget( 'user:{}:detail'.format(request.user.username), 'avatar').decode() if used_avatar != 'http://avatar.cdn.henji.xyz/default.jpg': try: bucket = BucketManager(q) key = os.path.basename(used_avatar) ret, info = bucket.delete(bucket_name, key) assert ret == {} except Exception as e: pass # 更新头像地址 url = 'http://avatar.cdn.henji.xyz/{}'.format(file_name) redis.hset('user:{}:detail'.format(request.user.username), 'avatar', "{}-avatar".format(url)) # 刷新缓存 cdn_manager = CdnManager(q) urls = [url] cdn_manager.refresh_urls(urls) return JsonResponse({'code': 1, 'msg': url})
def __init__(self): self.manager = CdnManager( Auth(access_key=settings.QINIU_ACCESSKEY, secret_key=settings.QINIU_ACCESSSECRET))
def refresh(): cdn_manager = CdnManager(Config.q) refresh_url_result = cdn_manager.refresh_urls(Config.urls) print(refresh_url_result) return refresh_url_result
def post(self, request): # 判断是否已经登陆 if not request.user.is_authenticated: return JsonResponse({'code': 4, 'msg': '上传图片请先登录'}) photo = request.FILES.get('photo') if not photo: return JsonResponse({'code': 2, 'msg': '请选择需要上传的图片'}) allow_types = ['image/jpeg', 'image/png', 'image/gif'] if photo.content_type not in allow_types: return JsonResponse({'code': 3, 'msg': '上传失败,文件类型错误'}) file_name = '{}_photo_{}_{}'.format(request.user.username, time.time(), photo.name.replace(' ', '')) fs = FileSystemStorage() fs.save(file_name, photo) file_path = os.path.join(MEDIA_ROOT, file_name) # compress the image i = Image.open(file_path) i.thumbnail((1280, 1280)) i.save(file_path) access_key = 'M2TrolxfManTFNP4Clr3M12JW0tvAaCV0xIbrZk5' secret_key = 'Llh0byt0KDHwiFlcNVvPiTpQSrH8IrZSt5puu1zS' q = qiniu_auth(access_key, secret_key) bucket_name = 'photo' try: token = q.upload_token(bucket_name, file_name, 3600) ret, info = put_file(token, file_name, file_path) assert ret['key'] == file_name assert ret['hash'] == etag(file_path) except Exception as e: return JsonResponse({'code': 4, 'msg': '上传文件出错'}) finally: # 删除本地 fs.delete(file_name) # 删除以前头像地址 used_photo = request.GET['used_photo'] if used_photo: try: bucket = BucketManager(q) key = os.path.basename(used_photo) ret, info = bucket.delete(bucket_name, key) assert ret == {} except Exception as e: pass # 更新图片地址 url = 'http://photo.cdn.henji.xyz/{}'.format(file_name) # 刷新缓存 cdn_manager = CdnManager(q) urls = [url] cdn_manager.refresh_urls(urls) return JsonResponse({'code': 1, 'msg': url})