Skip to content

Commit

Permalink
modify the usage and module name
Browse files Browse the repository at this point in the history
  • Loading branch information
chenwenquan committed Aug 4, 2016
1 parent d160ae4 commit 10bf394
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ Qiniu file uploader for Flask


## Install

```
$ pip install qiniufs
```

## Usage

```python
bucket = "your-qiniu-bucket-name"
prefix_urls = "your-qiniu-url"
fs = QiniuFS(bucket, access_key, secret_key, prefix_urls)

def upload_picture(picture):
mime = picture.mimetype
data = picture.read()
if data:
r, d = fs.upload(data, mime)
if r and d:
key = d.get('key')
url = fs.get_url(d.get('key'))
return key, url
bucket_name = "your-qiniu-bucket-name"
prefix_url = "your-qiniu-domain"
fs = QiniuFS(bucket_name, access_key, secret_key, prefix_url)

# upload
mime = data.mimetype
r, d = fs.upload_data(data, mime=mime)
if r and d:
key = d.get('key')
url = fs.get_url(d.get('key'))

# delete
r = fs.delete_file(key)
if r:
print "success"
```

33 changes: 16 additions & 17 deletions qiniu.py → qiniufs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class QiniuFS(object):
Qiniu file storage
Attributes:
bucket: 存储空间
bucket: bucket name
access_key: access_key
secret_key: secret_key
prefix_urls: 存储空间URL前缀
policy: 存储空间上传策略
prefix_url: domain
policy: policy for upload
'''
def __init__(self, bucket, access_key, secret_key, prefix_url, policy=None):
self.bucket = bucket
Expand All @@ -36,7 +36,7 @@ def _make_auth(self):

def _token(self, key=None, expires=3600):
"""
上传凭证
token
"""
key = key or randbytes2(16)
auth = self._make_auth()
Expand All @@ -45,14 +45,14 @@ def _token(self, key=None, expires=3600):

def upload_data(self, data, mime_type=None, key=None):
"""
上传二进制流
upload data stream
Args:
data: 二进制流对象
key: 文件名
mime_type: 文件mimeType
data: data stream
key: data stream name
mime_type: data stream mimeType
Returns:
True or False {"hash": "<Hash string>", "key": "<Key string>"}
True or False and {"hash": "<Hash string>", "key": "<Key string>"}
"""
token, key = self._token(key=key)
mime_type = mime_type or 'application/octet-stream'
Expand All @@ -63,11 +63,11 @@ def upload_data(self, data, mime_type=None, key=None):

def upload_file(self, file, mime_type=None, key=None):
"""
断点续传上传文件
upload file object
Args:
file: 文件对象
key: 文件名
mime_type: 文件mimeType
file: file object
key: file name
mime_type: file mimeType
Returns:
True or False and {"hash": "<Hash string>", "key": "<Key string>"}
Expand All @@ -83,7 +83,7 @@ def upload_file(self, file, mime_type=None, key=None):

def delete_file(self, key):
"""
文件删除, 谨慎使用
delete the file form qiniu
"""
auth = self._make_auth()
bucket = qiniu.BucketManager(auth)
Expand All @@ -92,7 +92,7 @@ def delete_file(self, key):

def asyn_file_process(self, key, fops, pipeline=None):
"""
文件异步持久化处理
asynchronous file persistence
"""
auth = self._make_auth()
pfop = qiniu.PersistentFop(auth, self.bucket, pipeline=pipeline)
Expand All @@ -103,7 +103,7 @@ def asyn_file_process(self, key, fops, pipeline=None):

def get_url(self, key, scheme='http', style=None, is_private=False):
"""
下载地址
url for the uploaded file
"""
url = ''
if self.prefix_url:
Expand All @@ -124,7 +124,6 @@ def get_url(self, key, scheme='http', style=None, is_private=False):

class QiniuPolicy(object):
'''
上传策略,字段含义请参考七牛文档
http://developer.qiniu.com/article/developer/security/put-policy.html
'''
def __init__(self,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
keywords='qiniu for flask',
description='qiniu file uploader for flask!',
long_description=__doc__,
py_modules=['qiniu'],
py_modules=['qiniufs'],
platforms='any',
install_requires=[
'qiniu==7.0.6'
Expand Down

0 comments on commit 10bf394

Please sign in to comment.