V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
honmaple
V2EX  ›  Python

用 Python 写了一个简单的图片上传存储服务

  •  
  •   honmaple · 2017-03-14 14:08:57 +08:00 · 1950 次点击
    这是一个创建于 2610 天前的主题,其中的信息可能已经有所发展或是发生改变。

    七牛什么的自定义域名竟然还需要备案,所以花两天时间写了一个简单的图片上传存储服务(基于 flask)

    示例,使用 requests上传图片

    import requests
    
    def images():
        url = 'http://127.0.0.1:8000/api/images'
        files = {'images': open('desktop.png', 'rb')}
        multiple_files = [
            ('images', ('11.png', open('11.png', 'rb'), 'image/png')),
            ('images', ('desktop.png', open('desktop.png', 'rb'), 'image/png'))
        ]
        headers = {
            'Api-Key':
            'InhpeWFuZzA4MDdJBtx4AWlPpI_Oxx1Ki8',
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'
        }
        # r = requests.post(url, files=multiple_files, headers=headers)
        r = requests.post(url, files=files, headers=headers)
        print(r.text)
    

    GitHub 地址: https://github.com/honmaple/maple-file

    6 条回复    2017-03-16 15:10:41 +08:00
    ipconfiger
        1
    ipconfiger  
       2017-03-14 14:30:04 +08:00
    支持自动多尺寸 thumbnail 不?
    honmaple
        2
    honmaple  
    OP
       2017-03-14 14:34:55 +08:00
    @ipconfiger 目前只是保存原图及生成 width=300 的缩略图,后续可能会加上
    honmaple
        3
    honmaple  
    OP
       2017-03-14 16:04:31 +08:00
    @ipconfiger 现在可以使用 url/40965530537.png?width=300&height=100,不过只支持等比缩放
    felinx
        4
    felinx  
       2017-03-14 16:11:35 +08:00   ❤️ 1
    @ipconfiger 自己架一个图片处理的服务也容易,有开源的一些解决方案可参考,稍作改动可以达到类似于七牛自定义后缀规则的效果 https://github.com/agschwender/pilbox
    kba977
        5
    kba977  
       2017-03-15 22:10:42 +08:00
    七牛不是有 api 么 直接上传不就好了~~~
    honmaple
        6
    honmaple  
    OP
       2017-03-16 15:10:41 +08:00
    @kba977 是因为自定义域名要备案才自己写的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3171 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.