最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 阿里云oss實(shí)現(xiàn)直傳簽名與回調(diào)驗(yàn)證的示例方法

 更新時(shí)間:2021年03月29日 10:35:20   作者:weixin_54126636  
這篇文章主要介紹了python 阿里云oss實(shí)現(xiàn)直傳簽名與回調(diào)驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

簽名

import base64
import json
import time
from datetime import datetime
import hmac
from hashlib import sha1

access_key_id = ''
# 請?zhí)顚懩腁ccessKeySecret。
access_key_secret = ''
# host的格式為 bucketname.endpoint ,請?zhí)鎿Q為您的真實(shí)信息。
host = ''
# callback_url為 上傳回調(diào)服務(wù)器的URL,請將下面的IP和Port配置為您自己的真實(shí)信息。
callback_url = ""
# 用戶上傳文件時(shí)指定的前綴。
upload_dir = 'user-dir-prefix/'
expire_time = 1200
expire_syncpoint = int(time.time() + expire_time)

policy_dict = {
  'expiration': datetime.utcfromtimestamp(expire_syncpoint).isoformat() + 'Z',
  'conditions': [
    {"bucket": "test-paige"},
    ['starts-with', '$key', 'user/test/']
  ]
}
policy = json.dumps(policy_dict).strip()
policy_encode = base64.b64encode(policy.encode())
signature = base64.encodebytes(hmac.new(access_key_secret.encode(), policy_encode, sha1).digest())

callback_dict = {
  'callbackUrl': callback_url,
  'callbackBody': 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${'
          'imageInfo.width}',
  'callbackBodyType': 'application/json'
}

callback = base64.b64encode(json.dumps(callback_dict).strip().encode()).decode()

var = {
  'accessid': access_key_id,
  'host': host,
  'policy': policy_encode.decode(),
  'signature': signature.decode().strip(),
  'expire': expire_syncpoint,
  'callback': callback
}

回調(diào)驗(yàn)簽

import asyncio
import base64
import time
import aiomysql
import rsa
from aiohttp import web, ClientSession
from urllib import parse
import uuid


def success(msg='', data=None):
  if data is None:
    data = {}
  dict_data = {
    'code': 1,
    'msg': msg,
    'data': data
  }
  return web.json_response(dict_data)


def failed(msg='', data=None):
  if data is None:
    data = {}
  dict_data = {
    'code': 0,
    'msg': msg,
    'data': data
  }
  return web.json_response(dict_data)


async def handle(request):
  """
  獲取連接池
  :param web.BaseRequest request:
  :return:
  """
  authorization_base64 = request.headers['authorization']
  x_oss_pub_key_url_base64 = request.headers['x-oss-pub-key-url']
  pub_key_url = base64.b64decode(x_oss_pub_key_url_base64.encode())
  authorization = base64.b64decode(authorization_base64.encode())
  path = request.path

  async with ClientSession() as session:
    async with session.get(pub_key_url.decode()) as resp:
      pub_key_body = await resp.text()
      pubkey = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key_body.encode())
      body = await request.content.read()
      auth_str = parse.unquote(path) + '\n' + body.decode()
      parse_url = parse.parse_qs(body.decode())
      print(parse_url)
      try:
        rsa.verify(auth_str.encode(), authorization, pubkey)
        pool = request.app['mysql_pool']
        async with pool.acquire() as conn:
          async with conn.cursor() as cur:
            id = str(uuid.uuid4())
            url = parse_url['filename'][0]
            mime = parse_url['mimeType'][0]
            disk = 'oss'
            time_at = time.strftime("%Y-%m-%d %H:%I:%S", time.localtime())
            sql = "INSERT INTO media(id,url,mime,disk,created_at,updated_at) VALUES(%s,%s,%s,%s,%s,%s)"
            await cur.execute(sql, (id, url, mime, disk, time_at, time_at))
            await conn.commit()
        dict_data = {
          'id': id,
          'url': url,
          'cdn_url': 'https://cdn.***.net' + '/' + url,
          'mime': mime,
          'disk': disk,
          'created_at': time_at,
          'updated_at': time_at,
        }
        return success(data=dict_data)
      except rsa.pkcs1.VerificationError:
        return failed(msg='驗(yàn)證錯(cuò)誤')


async def init(loop):
  # 創(chuàng)建連接池
  mysql_pool = await aiomysql.create_pool(host='127.0.0.1', port=3306,
                      user='', password='',
                      db='', loop=loop)

  async def on_shutdown(application):
    """
    接收到關(guān)閉信號(hào)時(shí),要先關(guān)閉連接池,并等待連接池關(guān)閉成功.
    :param web.Application application:
    :return:
    """
    application['mysql_pool'].close()
    # 沒有下面這句話會(huì)報(bào)錯(cuò) RuntimeError: Event loop is closed ,因?yàn)檫B接池沒有真正關(guān)關(guān)閉程序就關(guān)閉了,引發(fā)python的報(bào)錯(cuò)
    await application['mysql_pool'].wait_closed()

  application = web.Application()
  application.on_shutdown.append(on_shutdown)
  # 把連接池放到 application 實(shí)例中
  application['mysql_pool'] = mysql_pool
  application.add_routes([web.get('/', handle), web.post('/oss', handle)])
  return application


if __name__ == '__main__':
  loop = asyncio.get_event_loop()
  application = loop.run_until_complete(init(loop))
  web.run_app(application, host='127.0.0.1')
  loop.close()

到此這篇關(guān)于python 阿里云oss實(shí)現(xiàn)直傳簽名與回調(diào)驗(yàn)證的文章就介紹到這了,更多相關(guān)python 直傳簽名與回調(diào)驗(yàn)證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 搭建?Selenium+Python開發(fā)環(huán)境詳細(xì)步驟

    搭建?Selenium+Python開發(fā)環(huán)境詳細(xì)步驟

    這篇文章主要介紹了搭建?Selenium+Python開發(fā)環(huán)境詳細(xì)步驟的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • Pycharm學(xué)習(xí)教程(5) Python快捷鍵相關(guān)設(shè)置

    Pycharm學(xué)習(xí)教程(5) Python快捷鍵相關(guān)設(shè)置

    這篇文章主要為大家詳細(xì)介紹了最全的Pycharm學(xué)習(xí)教程第五篇,Python快捷鍵相關(guān)設(shè)置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Python字符串中添加、插入特定字符的方法

    Python字符串中添加、插入特定字符的方法

    這篇文章主要介紹了Python字符串中添加、插入特定字符的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • 關(guān)于python通過新建環(huán)境安裝tfx的問題

    關(guān)于python通過新建環(huán)境安裝tfx的問題

    這篇文章主要介紹了python安裝tfx/新建環(huán)境,新建一個(gè)環(huán)境tfx專門用來運(yùn)行流水線,這個(gè)環(huán)境安裝python3.8,對(duì)python安裝tfx相關(guān)知識(shí)感興趣的朋友一起看看吧
    2022-05-05
  • 關(guān)于探究python中sys.argv時(shí)遇到的問題詳解

    關(guān)于探究python中sys.argv時(shí)遇到的問題詳解

    這篇文章主要給大家介紹了python里sys.argv時(shí)遇到問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • python爬蟲爬取某圖書網(wǎng)頁實(shí)例講解

    python爬蟲爬取某圖書網(wǎng)頁實(shí)例講解

    這篇文章主要介紹了python爬蟲爬取某圖書網(wǎng)頁實(shí)例,下面是通過requests庫來對(duì)ajax頁面進(jìn)行爬取的案例,與正常頁面不同,這里我們獲取url的方式也會(huì)不同,這里我們通過爬取一個(gè)簡單的ajax小說頁面來為大家講解,需要的朋友可以參考下
    2024-08-08
  • Python爬蟲實(shí)現(xiàn)Cookie模擬登錄

    Python爬蟲實(shí)現(xiàn)Cookie模擬登錄

    這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)Cookie模擬登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python如何獲取文件路徑/目錄

    Python如何獲取文件路徑/目錄

    這篇文章主要介紹了Python如何獲取文件路徑/目錄,幫助大家更好的利用python處理文件,感興趣的朋友可以了解下
    2020-09-09
  • python內(nèi)置模塊collections詳解

    python內(nèi)置模塊collections詳解

    這篇文章主要介紹了python內(nèi)置模塊collections詳解,collections是Python內(nèi)建的一個(gè)集合模塊,提供了許多有用的集合類,python提供了很多非常好用的基本類型,比如不可變類型tuple,我們可以輕松地用它來表示一個(gè)二元向量,需要的朋友可以參考下
    2023-09-09
  • Python數(shù)據(jù)存儲(chǔ)之 h5py詳解

    Python數(shù)據(jù)存儲(chǔ)之 h5py詳解

    今天小編就為大家分享一篇Python數(shù)據(jù)存儲(chǔ)之 h5py詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論

新泰市| 鄱阳县| 南华县| 武胜县| 五原县| 长乐市| 宜昌市| 白玉县| 贺州市| 永州市| 平顶山市| 璧山县| 东安县| 嘉祥县| 建湖县| 平阳县| 临泉县| 富蕴县| 巴楚县| 博兴县| 卢湾区| 无锡市| 西峡县| 扬州市| 崇左市| 玉环县| 都匀市| 海安县| 九龙坡区| 多伦县| 淄博市| 秦皇岛市| 绥宁县| 平山县| 遂溪县| 明水县| 莆田市| 监利县| 化州市| 东兰县| 龙州县|