本文整理汇总了Python中qiniu.put_file函数的典型用法代码示例。如果您正苦于以下问题:Python put_file函数的具体用法?Python put_file怎么用?Python put_file使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了put_file函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: upload
def upload(self, upload_dir, upload_bucket):
if os.path.isdir(upload_dir):
if os.path.isdir(upload_dir):
up_file_list = []
for root, dirs, files in os.walk(upload_dir,topdown=True):
for v_file in files:
up_file_list.append(os.path.join(root, v_file))
for file_name in up_file_list:
token = self.mauth.upload_token(upload_bucket, file_name)
ret, info = put_file(token, file_name, file_name)
# print(info)
assert ret['key'] == file_name
assert ret['hash'] == etag(file_name)
print ret
elif os.path.isfile(upload_dir):
token = self.mauth.upload_token(upload_bucket, file_name)
ret, info = put_file(token, file_name, file_name)
assert ret['key'] == file_name
assert ret['hash'] == etag(file_name)
print ret
elif os.path.isfile(upload_dir):
file_name = upload_dir
token = self.mauth.upload_token(upload_bucket, file_name)
ret, info = put_file(token, file_name, file_name)
assert ret['key'] == file_name
assert ret['hash'] == etag(file_name)
print ret
示例2: upload
def upload(key):
access_key = 'Fx9CvaSHqUFVrofBgpGxAdsR3UV0SNB2bRof6ss2'
secret_key = 'eW8fX00uUZukKcFsZyeIWw3BTyfhyoIhGQ4j5NwN'
q = Auth(access_key, secret_key)
bucket_name = 'bing'
token = q.upload_token(bucket_name, key, 3600)
localfile = key
put_file(token, key, localfile)
示例3: user_home
def user_home(request):
user = get_user(request)
if request.method == 'GET':
data = {'user': user}
return render(request, 'home/user/user_home.html', data)
if request.method == 'POST':
username = request.POST.get('username')
icon = request.FILES['icon']
email = request.POST.get('email')
sex = request.POST.get('sex')
tel = request.POST.get('tel')
if username and username != user.username:
if User.objects.filter(username=username).first():
msg = '用户名已存在'
return render(request, 'home/user/user_home.html', {'msg': msg})
user.username = username
if email and email != user.email:
if User.objects.filter(email=email).first():
msg = '该邮箱已注册'
return render(request, 'home/user/user_home.html', {'msg': msg})
if not re.match("^[a-zA-Z0-9_.-][email protected][a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$", str(email)):
msg = '请输入有效邮箱号'
return render(request, 'home/user/user_home.html', {'msg': msg})
user.email = email
if icon:
with open(BASE_DIR + AVATAR_DIR, 'wb') as pic:
for c in icon.chunks():
pic.write(c)
# 构建鉴权对象
q = qiniu.Auth(ACCESS_KEY, QINIU_SECRET_KEY)
# 上传到七牛后保存的文件名
key = 'static/icon/' + user.username + '/'+str(time.time())+'.jpg'
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(BUCKET_NAME, key)
localfile = BASE_DIR + '/static/images/icon.jpg'
qiniu.put_file(token, key, localfile)
user.icon = 'http://pbu0s2z3p.bkt.clouddn.com/' + key
if tel and tel != user.tel:
if User.objects.filter(tel=tel).first():
msg = '该手机已注册'
return render(request, 'home/user/user_home.html', {'msg': msg})
if not re.match("^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$", str(tel)):
msg = '请输入有效手机号'
return render(request, 'home/user/user_home.html', {'msg': msg})
user.tel = tel
if sex and sex != user.sex:
user.sex = 1 if sex == '男' else 0
user.save()
return HttpResponseRedirect('/user/user_home/')
示例4: __upload_file
def __upload_file(self,localphoto):
localfile = localphoto.getpath()
key = self.pre + localfile.split('\\')[-1]
inf_key = key + '&&' + str(localphoto.getmood())\
+'&&'+localphoto.getcomment()+'&&.inf'
self.inf[key] = [localphoto.getmood(),localphoto.getcomment]
token1 = self.q.upload_token(self.BN,inf_key)
token = self.q.upload_token(self.BN, key)
mime_type = self.__get_mime_type(localfile)
params = {'x:a': 'a'}
progress_handler = lambda progress, total: progress
ret1, info1 = qiniu.put_file(token1, inf_key, self.static_file,params,\
mime_type, progress_handler=progress_handler)
ret, info = qiniu.put_file(token, key, localfile, params,\
mime_type, progress_handler=progress_handler)
示例5: to_upload_file
def to_upload_file(self, f_file, f_md5, f_type):
'''
to upload file into qiniu bucket
'''
try:
# f_name = os.path.basename(f_file)
q = qiniu.Auth(self.access_key, self.secret_key)
# key means filename in qiniu bucket
key = "%s%s.%s" % (self.image_prefix, f_md5, f_type)
token = q.upload_token(self.bucket_name, key)
qiniu.put_file(token, key, f_file)
return True
except:
return False
示例6: upload
def upload(args):
""" Upload images to qiniu cloud
"""
assert (args.bucket_name != None)
assert (args.remote_dir != None)
assert (args.input_file != None)
assert (args.local_dir != None)
bucket_name = args.bucket_name
# with open(args.input_file) as fh:
# paths = filter(None, fh.readlines())
img_num = 0
for video in os.listdir(args.input_file):
img_num += 1
with open(os.path.join(args.input_file, video)) as fh:
paths = filter(None, fh.readlines())
mime_type = "image/jpg"
for path in paths:
key = (os.path.join(args.remote_dir, path)).strip('\n')
print 'upload ', path, ' as ', key
# path = path.split()[0]
localfile = (os.path.join(args.local_dir, '%s' % (path))).strip('\n')
print localfile
token = q.upload_token(bucket_name, key)
ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True)
assert info.status_code == 200
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
print '%d images uploaded' % (img_num)
示例7: upload_without_key
def upload_without_key(bucket, filePath, uploadname):
'''上传文件'''
auth = qiniu.Auth(accessKey, secretKey)
upToken = auth.upload_token(bucket, key=None)
key = uploadname
retData, respInfo = qiniu.put_file(upToken, key, filePath, mime_type=mime.guess_type(filePath)[0])
parseRet(retData, respInfo)
示例8: store_file
def store_file(self, file_path, file_name):
upload_token = self.credentials.upload_token(self.bucket_name, file_name)
ret, err = put_file(upload_token, file_name, file_path)
if ret is not None:
return "%s/%s" % (self.imageServerUrl, ret['key'])
else:
logging.error('upload: %s error.' % file_name)
示例9: upload_photo
def upload_photo():
"""上传照片到七牛,并返回私有链接地址"""
from qiniu import Auth
from qiniu import put_file
global config
progress_handler = lambda progress, total: progress
photo_path = http_get("http://127.0.0.1:9876/photo/shot").content
# Upload to qiniu
mime_type = "image/jpeg"
auth = Auth(str(config["qiniu"]["api_key"]), str(config["qiniu"]["secret"]))
print auth
filename = os.path.basename(photo_path)
print "filename: ", filename, type(filename)
token = auth.upload_token(str(config["qiniu"]["bucket"]))
print token
ret, info = put_file(token, filename, photo_path, {}, mime_type, progress_handler=progress_handler)
print "uploaded: ", ret, info
try:
os.remove(photo_path)
except Exception:
pass
# Return URL
base_url = "{}/{}".format(str(config["qiniu"]["domain"]), filename)
return auth.private_download_url(base_url, expires=3600)
示例10: upload
def upload(origin_file_path):
# 构建鉴权对象
q = Auth(config.access_key, config.secret_key)
# 要上传的空间
bucket_name = 'md-doc'
localfile = conwebp.convert(origin_file_path)
# 上传到七牛后保存的文件名
dest_prefix = time.strftime("%Y%m%d%H%M%S", time.localtime())
dest_name = dest_prefix + "_" + os.path.basename(localfile)
# 上传文件到七牛后, 七牛将文件名和文件大小回调给业务服务器。
policy = {
'callbackBody': 'filename=$(fname)&filesize=$(fsize)'
}
token = q.upload_token(bucket_name, dest_name, 3600, policy)
ret, info = put_file(token, dest_name, localfile)
if ret is not None:
print("Upload Success,url=", config.domin + dest_name)
else:
print("info=", info)
print("ret=", ret)
assert ret['key'] == dest_name
assert ret['hash'] == etag(localfile)
示例11: loop_qiniu
def loop_qiniu():
while True:
for f in os.listdir(UPLOAD_FOLDER):
if 'tmp' not in f.split('_')[-1]:
os.remove(os.path.join(UPLOAD_FOLDER, f))
# print 'jump out and for one more time'
elif 'tmp0' == f.split('_')[-1]:
continue
elif 'tmp' == f.split('_')[-1]:
print 'start handle %s' % f
f_path = os.path.join(UPLOAD_FOLDER, f)
f_path_new = os.path.join(UPLOAD_FOLDER, f[:-4])
key = f[:-4]
q = Auth(access_key, secret_key)
token = q.upload_token(Bucket, key, 3600)
local_file = f_path
ret, _ = put_file(token, key, local_file)
try:
assert ret['key'] == key
except TypeError:
print 'ret has error'
continue
os.rename(f_path, f_path_new)
print 'end handle %s' % key
print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
else:
print '<----'
print u'为考虑到的情况'
print time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
print '---->'
print 'pass this time while error', time.strftime("%Y/%y/%d %H:%M:%S", time.localtime(time.time()))
示例12: uploadToQiNiu
def uploadToQiNiu(filePath,name):
q = Auth(access_key, secret_key)
localfile = filePath
key = name
mime_type = "image/jpeg"
token = q.upload_token(bucket_name, key)
ret, info = put_file(token, key, localfile, mime_type=mime_type, check_crc=True)
示例13: upload_file
def upload_file(self, bucket_name, key, file_path):
#生成上传 Token,可以指定过期时间等
token = self._q.upload_token(bucket_name, key, 3600)
ret, info = put_file(token, key, file_path)
print(info)
pic_url = self.get_pic_url(bucket_name, file_path)
add_to_clipboard(pic_url, file_path)
示例14: test_upload
def test_upload():
q = Auth(access_key, secret_key)
token = q.upload_token(bucket, mykey.encode('utf-8'))
file = '/tmp/abc.txt'
ret, info = put_file(token, mykey.encode('utf-8'), file, mime_type="text/plain", check_crc=True)
print(info)
print(ret)
示例15: user_icon
def user_icon(request):
if request.method == 'GET':
return render(request, 'home/user/icon.html')
if request.method == 'POST':
file = request.FILES['avatar']
with open(BASE_DIR+'/static/images/icon.jpg', 'wb') as pic:
for c in file.chunks():
pic.write(c)
user = get_user(request)
# 要上传的空间
bucket_name = 'goodbuy'
# 构建鉴权对象
q = qiniu.Auth(ACCESS_KEY, QINIU_SECRET_KEY)
# 上传到七牛后保存的文件名
key = 'static/icon/'+user.username+'3.jpg'
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key)
localfile = BASE_DIR+'/static/images/icon.jpg'
ret, info = qiniu.put_file(token, key=key, file_path=localfile)
print(info, ret)
assert ret['key'] == key
assert ret['hash'] == etag(localfile)
user.icon = 'http://pbu0s2z3p.bkt.clouddn.com/'+key
user.save()
return JsonResponse({'key':key,'token':token})