向七牛云上传二进制数据流 Args: bucket_name: 空间名 filedata: 二进制数据流 filename: 上传后的文件名 ''' token = authorization.upload_token(bucket_name, filename, 3600) # 生成上传 Token ret, info = qiniu.put_data(token, filename, filedata) return ret, info 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 async_qiniu_upload = call_by_worker(qiniu_upload) async_qiniu_upload_data = call_by_worker(qiniu_upload_data) async_qiniu_fetch = call_by_worker(qiniu_fetch)
import requests from swiper import platform_config from worker import call_by_worker def gen_verify_code(length=4): '''生成验证码''' if length <= 0: length = 1 code = random.randrange(10**(length - 1), 10**(length)) return str(code) def send_sms(phone_num, text): '''发送短信''' params = platform_config.HY_SMS_PARAMS.copy() params['mobile'] = phone_num params['content'] = params['content'] % text headers = { "Accept": "text/plain", "Content-type": "application/x-www-form-urlencoded" } response = requests.post(platform_config.HY_SMS_URL, data=params, headers=headers) return response async_send_sms = call_by_worker(send_sms) # 为方便调试,将异步调用单独定义一次
from qiniu import Auth from qiniu import put_file from swiper import config from worker import call_by_worker qn = Auth(config.QN_ACCESS_KEY, config.QN_SECRET_KEY) def upload_to_qiniu(localfile, key): ''' 将本地文件上传到七牛云 Args: key: 上传到七牛后保存的文件名 localfile: 要上传文件的本地路径 ''' bucket_name = config.QN_BUCKET_NAME token = qn.upload_token(bucket_name, key, 3600) #生成上传 Token,可以指定过期时间等 ret, info = put_file(token, key, localfile) print(info) return ret, info async_upload_to_qiniu = call_by_worker(upload_to_qiniu) # 手动装饰,定义出异步上传七牛云
from worker import call_by_worker QN_Auth = Auth(config.QN_ACCESS_KEY, config.QN_SECRET_KEY) def get_qn_url(filename): return urljoin(config.QN_BASE_URL, filename) def upload_to_qiniu(localfile, key): '''将本地文件上传到七牛云 Args: localfile: 本地文件位置 key: 上传到云服务器后的文件名 ''' # 生成上传 Token 可以指定过期时间等 token = QN_Auth.upload_token(config.QN_BUCKET, key, 3600) # 要上传文件的本地路径 ret, info = put_file(token, key, localfile) assert ret['key'] == key assert ret['hash'] == etag(localfile) url = get_qn_url(filename) return ret, info, url async_upload_to_qiniu = call_by_worker(upload_to_qiniu)
from django.core.mail import send_mail, send_mass_mail, mail_admins from worker import call_by_worker async_send_mail = call_by_worker(send_mail) async_send_mass_mail = call_by_worker(send_mass_mail) async_mail_admins = call_by_worker(mail_admins)