Skip to content

Commit

Permalink
Merge pull request #11 from fengluo/master
Browse files Browse the repository at this point in the history
fix qiniu support
  • Loading branch information
tonyseek committed Jan 9, 2015
2 parents 44b6d94 + 09263d3 commit e2011e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
22 changes: 9 additions & 13 deletions flask_storage/qiniu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
from __future__ import absolute_import

from werkzeug import cached_property
import qiniu.rs
import qiniu.io
import qiniu.conf
import qiniu

from ._base import BaseStorage
from ._compat import urljoin
Expand All @@ -32,15 +30,14 @@ class QiniuStorage(BaseStorage):

def __init__(self, name, extensions, config):
super(QiniuStorage, self).__init__(name, extensions, config)
self.init_config()

def init_config(self):
qiniu.conf.ACCESS_KEY = self.access_key
qiniu.conf.SECRET_KEY = self.secret_key
@cached_property
def auth(self):
return qiniu.Auth(self.access_key, self.secret_key)

@cached_property
def _client(self):
return qiniu.rs.Client()
return qiniu.BucketManager(self.auth)

def url(self, filename):
"""Generate the url for a filename.
Expand All @@ -64,18 +61,17 @@ def generate_upload_token(self, filename=None):
Otherwise, client can modify the file.
"""
if filename:
scope = '%s:%s' % (self.bucket, filename)
token = self.auth.upload_token(self.bucket, filename)
else:
scope = self.bucket
policy = qiniu.rs.PutPolicy(scope)
return policy.token()
token = self.auth.upload_token(self.bucket)
return token

def save(self, storage, filename, token=None, extra=None):
self.check(storage)
if token is None:
token = self.generate_upload_token()
stream = storage.stream
ret, err = qiniu.io.put(token, filename, stream, extra=extra)
ret, err = qiniu.put_data(token, filename, stream)
if err:
raise QiniuException(err)
return ret
Expand Down
10 changes: 5 additions & 5 deletions tests/test_qiniu.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class TestQiniuStorage(BaseCase):

def setUp(self):
super(TestQiniuStorage, self).setUp()
self.io = self.patch('qiniu.io')
self.upload_token = self.patch('qiniu.rs.PutPolicy.token')
self.put_data = self.patch('qiniu.put_data')
# self.upload_token = self.patch('qiniu.rs.PutPolicy.token')

def test_upload(self):
ret, err = {'key': 'flask.png'}, None
self.io.put.return_value = ret, err
self.put_data.return_value = ret, err
response = self.upload()
assert self.upload_token.call_count == 1
assert self.io.put.call_count == 1
# assert self.upload_token.call_count == 1
assert self.put_data.call_count == 1

0 comments on commit e2011e8

Please sign in to comment.