美文网首页
python异步向OSS上传图片示例

python异步向OSS上传图片示例

作者: Curious1990 | 来源:发表于2019-03-23 09:47 被阅读0次
    import asyncio
    import aiohttp
    import oss2
    
    auth = oss2.Auth('<>', '<>')
    bucket = oss2.Bucket(auth, '<>', '<>')
    
    
    def parser(url, res):
        result = bucket.put_object(f'test/{url[1]}', res)
        print(result.status)
    
    
    async def downloader(url, callback=parser):
        async with aiohttp.ClientSession() as session:
            response = await session.get(url[0])
            if response.reason == 'OK':
                result = await response.read()
                callback(url, result)
    
    
    if __name__ == '__main__':
        tasks = [
            downloader(['https://sc02.alicdn.com/kf/UT8uwlJX44aXXagOFbX8/Wholesale-cheap-slim-fit-leather-jacket-women.jpg',
                        '111.jpg']),
            downloader(['https://sc02.alicdn.com/kf/UT8j9RJX1RaXXagOFbXi/Wholesale-cheap-slim-fit-leather-jacket-women.jpg',
                        '222.jpg']),
        ]
    
        loop = asyncio.get_event_loop()
        loop.run_until_complete(asyncio.wait(tasks))
        loop.close()
    

    相关文章

      网友评论

          本文标题:python异步向OSS上传图片示例

          本文链接:https://www.haomeiwen.com/subject/hepjvqtx.html