[
  {
    "path": ".gitignore",
    "content": "# Byte-compiled / optimized / DLL files\n__pycache__/\n*.py[cod]\n*$py.class\n\n# C extensions\n*.so\n\n# Distribution / packaging\n.Python\nenv/\nbuild/\ndevelop-eggs/\ndist/\ndownloads/\neggs/\n.eggs/\nlib/\nlib64/\nparts/\nsdist/\nvar/\nwheels/\n*.egg-info/\n.installed.cfg\n*.egg\n\n# PyInstaller\n#  Usually these files are written by a python script from a template\n#  before PyInstaller builds the exe, so as to inject date/other infos into it.\n*.manifest\n*.spec\n\n# Installer logs\npip-log.txt\npip-delete-this-directory.txt\n\n# Unit test / coverage reports\nhtmlcov/\n.tox/\n.coverage\n.coverage.*\n.cache\nnosetests.xml\ncoverage.xml\n*.cover\n.hypothesis/\n\n# Translations\n*.mo\n*.pot\n\n# Django stuff:\n*.log\nlocal_settings.py\n\n# Flask stuff:\ninstance/\n.webassets-cache\n\n# Scrapy stuff:\n.scrapy\n\n# Sphinx documentation\ndocs/_build/\n\n# PyBuilder\ntarget/\n\n# Jupyter Notebook\n.ipynb_checkpoints\n\n# pyenv\n.python-version\n\n# celery beat schedule file\ncelerybeat-schedule\n\n# SageMath parsed files\n*.sage.py\n\n# dotenv\n.env\n\n# virtualenv\n.venv\nvenv/\nENV/\n\n# Spyder project settings\n.spyderproject\n.spyproject\n\n# Rope project settings\n.ropeproject\n\n# mkdocs documentation\n/site\n\n# mypy\n.mypy_cache/\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2018 ouyangbro\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# pyqiniu\n对七牛云api封装了一些项目中比较实用的工具函数\n\n# 安装环境\n```\npython 2.7.10\npip install qiniu==7.0.8\n```\n# 使用方法\n配置好access_key、secret_key和bucket_name.\n```\nimport pyqiniu\n\n# 文件\nfile_path = u\"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg\"\n\n# 上传, 返回key\nkey = pyqiniu.upload(file_path)\nprint key\n\n# 获取文件key\nkey = pyqiniu.get_key(file_path)\nprint key\n\n# 判断文件是否存在\nis_exist = pyqiniu.check_file_exist(file_path)\nprint is_exist\n\n```\n"
  },
  {
    "path": "pyqiniu.py",
    "content": "# coding=utf-8\nfrom qiniu import Auth, put_file, etag\nfrom qiniu import BucketManager  # 构建鉴权对象\n\naccess_key = \"your-access-key\"\nsecret_key = \"your-secret-key\"\nbucket_name = 'your-bucket-name'\n\n\ndef upload(file_path):\n    \"\"\"\n    上传文件\n    :param file_path: 文件路径\n    :return: key\n    \"\"\"\n    # 构建鉴权对象\n    q = Auth(access_key, secret_key)\n\n    # 上传到七牛后保存的文件名\n    key = None\n\n    # 生成上传 Token，可以指定过期时间等\n    token = q.upload_token(bucket_name, key, 3600)\n\n    # 要上传文件的本地路径\n    ret, info = put_file(token, key, file_path)\n\n    assert ret['hash'] == etag(file_path)\n    return ret['key']\n\n\ndef get_img_info(img_url):\n    \"\"\"\n    获取七牛上图片的基本信息：长宽、后缀\n    :param img_url:\n    :return: tuple(长、宽、后缀)\n    \"\"\"\n    info_url = img_url + '?imageInfo'  # 基本信息\n    import requests\n    r = requests.get(info_url)\n    body = r.content\n    r.close()\n    import json\n    info_dic = json.loads(body)\n    height = info_dic['height']\n    width = info_dic['width']\n    suffix = info_dic['format']\n    return height, width, suffix\n\n\ndef get_key(file_path):\n    \"\"\"\n    获取一个文件的key，七牛的算法是获取文件的hash值，使用的是 qiniu.etag()\n    :param file_path:\n    :return: key (str)\n    \"\"\"\n    key = etag(file_path)\n    return key\n\n\ndef check_file_exist(file_path):\n    \"\"\"\n    判断key在你空间中存在\n    :param file_path:\n    :return: True False\n    \"\"\"\n    key = get_key(file_path)\n\n    # 初始化Auth状态\n    q = Auth(access_key, secret_key)\n\n    # 初始化BucketManager\n    bucket = BucketManager(q)\n\n    ret, info = bucket.stat(bucket_name, key)\n\n    text_body = info.text_body\n    if 'error' in text_body:\n        return False\n    return True\n\n\nif __name__ == '__main__':\n    # 文件\n    file_path = u\"/Users/ouyang/Desktop/新增测试/净空法师/封面.jpg\"\n\n    # 上传, 返回key\n    key = upload(file_path)\n    print key\n\n    # 获取文件key\n    key = get_key(file_path)\n    print key\n\n    # 判断文件是否存在\n    is_exist = check_file_exist(file_path)\n    print is_exist\n"
  }
]