├── .gitignore ├── LICENSE ├── README.md └── pyqiniu.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 ouyangbro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pyqiniu 2 | 对七牛云api封装了一些项目中比较实用的工具函数 3 | 4 | # 安装环境 5 | ``` 6 | python 2.7.10 7 | pip install qiniu==7.0.8 8 | ``` 9 | # 使用方法 10 | 配置好access_key、secret_key和bucket_name. 11 | ``` 12 | import pyqiniu 13 | 14 | # 文件 15 | file_path = u"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg" 16 | 17 | # 上传, 返回key 18 | key = pyqiniu.upload(file_path) 19 | print key 20 | 21 | # 获取文件key 22 | key = pyqiniu.get_key(file_path) 23 | print key 24 | 25 | # 判断文件是否存在 26 | is_exist = pyqiniu.check_file_exist(file_path) 27 | print is_exist 28 | 29 | ``` 30 | -------------------------------------------------------------------------------- /pyqiniu.py: -------------------------------------------------------------------------------- 1 | # coding=utf-8 2 | from qiniu import Auth, put_file, etag 3 | from qiniu import BucketManager # 构建鉴权对象 4 | 5 | access_key = "your-access-key" 6 | secret_key = "your-secret-key" 7 | bucket_name = 'your-bucket-name' 8 | 9 | 10 | def upload(file_path): 11 | """ 12 | 上传文件 13 | :param file_path: 文件路径 14 | :return: key 15 | """ 16 | # 构建鉴权对象 17 | q = Auth(access_key, secret_key) 18 | 19 | # 上传到七牛后保存的文件名 20 | key = None 21 | 22 | # 生成上传 Token,可以指定过期时间等 23 | token = q.upload_token(bucket_name, key, 3600) 24 | 25 | # 要上传文件的本地路径 26 | ret, info = put_file(token, key, file_path) 27 | 28 | assert ret['hash'] == etag(file_path) 29 | return ret['key'] 30 | 31 | 32 | def get_img_info(img_url): 33 | """ 34 | 获取七牛上图片的基本信息:长宽、后缀 35 | :param img_url: 36 | :return: tuple(长、宽、后缀) 37 | """ 38 | info_url = img_url + '?imageInfo' # 基本信息 39 | import requests 40 | r = requests.get(info_url) 41 | body = r.content 42 | r.close() 43 | import json 44 | info_dic = json.loads(body) 45 | height = info_dic['height'] 46 | width = info_dic['width'] 47 | suffix = info_dic['format'] 48 | return height, width, suffix 49 | 50 | 51 | def get_key(file_path): 52 | """ 53 | 获取一个文件的key,七牛的算法是获取文件的hash值,使用的是 qiniu.etag() 54 | :param file_path: 55 | :return: key (str) 56 | """ 57 | key = etag(file_path) 58 | return key 59 | 60 | 61 | def check_file_exist(file_path): 62 | """ 63 | 判断key在你空间中存在 64 | :param file_path: 65 | :return: True False 66 | """ 67 | key = get_key(file_path) 68 | 69 | # 初始化Auth状态 70 | q = Auth(access_key, secret_key) 71 | 72 | # 初始化BucketManager 73 | bucket = BucketManager(q) 74 | 75 | ret, info = bucket.stat(bucket_name, key) 76 | 77 | text_body = info.text_body 78 | if 'error' in text_body: 79 | return False 80 | return True 81 | 82 | 83 | if __name__ == '__main__': 84 | # 文件 85 | file_path = u"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg" 86 | 87 | # 上传, 返回key 88 | key = upload(file_path) 89 | print key 90 | 91 | # 获取文件key 92 | key = get_key(file_path) 93 | print key 94 | 95 | # 判断文件是否存在 96 | is_exist = check_file_exist(file_path) 97 | print is_exist 98 | --------------------------------------------------------------------------------