Repository: emaste-r/pyqiniu Branch: master Commit: 6b46daadb616 Files: 4 Total size: 4.4 KB Directory structure: gitextract_4wn2zvwg/ ├── .gitignore ├── LICENSE ├── README.md └── pyqiniu.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # dotenv .env # virtualenv .venv venv/ ENV/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2018 ouyangbro Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # pyqiniu 对七牛云api封装了一些项目中比较实用的工具函数 # 安装环境 ``` python 2.7.10 pip install qiniu==7.0.8 ``` # 使用方法 配置好access_key、secret_key和bucket_name. ``` import pyqiniu # 文件 file_path = u"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg" # 上传, 返回key key = pyqiniu.upload(file_path) print key # 获取文件key key = pyqiniu.get_key(file_path) print key # 判断文件是否存在 is_exist = pyqiniu.check_file_exist(file_path) print is_exist ``` ================================================ FILE: pyqiniu.py ================================================ # coding=utf-8 from qiniu import Auth, put_file, etag from qiniu import BucketManager # 构建鉴权对象 access_key = "your-access-key" secret_key = "your-secret-key" bucket_name = 'your-bucket-name' def upload(file_path): """ 上传文件 :param file_path: 文件路径 :return: key """ # 构建鉴权对象 q = Auth(access_key, secret_key) # 上传到七牛后保存的文件名 key = None # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 ret, info = put_file(token, key, file_path) assert ret['hash'] == etag(file_path) return ret['key'] def get_img_info(img_url): """ 获取七牛上图片的基本信息:长宽、后缀 :param img_url: :return: tuple(长、宽、后缀) """ info_url = img_url + '?imageInfo' # 基本信息 import requests r = requests.get(info_url) body = r.content r.close() import json info_dic = json.loads(body) height = info_dic['height'] width = info_dic['width'] suffix = info_dic['format'] return height, width, suffix def get_key(file_path): """ 获取一个文件的key,七牛的算法是获取文件的hash值,使用的是 qiniu.etag() :param file_path: :return: key (str) """ key = etag(file_path) return key def check_file_exist(file_path): """ 判断key在你空间中存在 :param file_path: :return: True False """ key = get_key(file_path) # 初始化Auth状态 q = Auth(access_key, secret_key) # 初始化BucketManager bucket = BucketManager(q) ret, info = bucket.stat(bucket_name, key) text_body = info.text_body if 'error' in text_body: return False return True if __name__ == '__main__': # 文件 file_path = u"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg" # 上传, 返回key key = upload(file_path) print key # 获取文件key key = get_key(file_path) print key # 判断文件是否存在 is_exist = check_file_exist(file_path) print is_exist