Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions examples/upload.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# -*- coding: utf-8 -*-
# flake8: noqa

from qiniu import Auth, put_file, etag
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
import qiniu.config
from qiniu.compat import is_py2, is_py3

#需要填写你的 Access Key 和 Secret Key
# 需要填写你的 Access Key 和 Secret Key
access_key = '...'
secret_key = '...'

#构建鉴权对象
# 构建鉴权对象
q = Auth(access_key, secret_key)

#要上传的空间
bucket_name = 'Bucket_Name'
# 要上传的空间
bucket_name = 'if-bc'

#上传到七牛后保存的文件名
key = 'my-python-logo.png';
# 上传到七牛后保存的文件名
key = 'my-python-七牛.png'

#生成上传 Token,可以指定过期时间等
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)

#要上传文件的本地路径
localfile = './sync/bbb.jpg'
# 要上传文件的本地路径
localfile = '/Users/jemy/Documents/qiniu.png'

ret, info = put_file(token, key, localfile)
print(ret)
print(info)
assert ret['key'] == key

if is_py2:
assert ret['key'].encode('utf-8') == key
elif is_py3:
assert ret['key'] == key

assert ret['hash'] == etag(localfile)
2 changes: 1 addition & 1 deletion qiniu/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def upload_token(self, bucket, key=None, expires=3600, policy=None, strict_polic

scope = bucket
if key is not None:
scope = u'{0}:{1}'.format(bucket, key)
scope = '{0}:{1}'.format(bucket, key)

args = dict(
scope=scope,
Expand Down
8 changes: 6 additions & 2 deletions qiniu/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import requests
from requests.auth import AuthBase

from qiniu.compat import is_py2, is_py3
from qiniu import config
import qiniu.auth
from . import __version__
Expand All @@ -21,7 +22,7 @@ def __return_wrapper(resp):
if resp.status_code != 200 or resp.headers.get('X-Reqid') is None:
return None, ResponseInfo(resp)
resp.encoding = 'utf-8'
ret = resp.json() if resp.text != '' else {}
ret = resp.json(encoding='utf-8') if resp.text != '' else {}
return ret, ResponseInfo(resp)


Expand Down Expand Up @@ -169,7 +170,10 @@ def connect_failed(self):
return self.__response is None or self.req_id is None

def __str__(self):
return ', '.join(['%s:%s' % item for item in self.__dict__.items()])
if is_py2:
return ', '.join(['%s:%s' % item for item in self.__dict__.items()]).encode('utf-8')
elif is_py3:
return ', '.join(['%s:%s' % item for item in self.__dict__.items()])

def __repr__(self):
return self.__str__()