簡體   English   中英

Azure 函數可以將 Git repo 克隆到 Blob 存儲嗎?

[英]Can Azure functions clone Git repo to a Blob storage?

我有一個業務需求,即在每次提交時克隆 Github 存儲庫,以首先觸發從邏輯應用程序開始的工作流,然后觸發 azure function,它可以將 Github 存儲庫克隆到我的 azure 存儲 blob。 我從未在 inte.net 上看到過這樣的用例,有人對可以做什么有任何建議嗎?

如果您想將 Github 存儲庫克隆到我的 azure 存儲 blob,我們可以使用 python sdk PyGithub從存儲庫下載文件,然后我們可以將這些文件上傳到 Azure blob。

例如(我用的是Azure HTTP觸發函數)

  1. 安裝 package
pip install PyGithub
pip install aiohttp
pip install azure-storage-blob
  1. 代碼
import azure.functions as func
from github import Github
from azure.storage.blob.aio import BlobServiceClient
async def upload(data,container_name,blob_name):
        connection_string='<your storage account connection string>'
        blob_service_client = BlobServiceClient.from_connection_string(connection_string)
        async with blob_service_client:
            container_client = blob_service_client.get_container_client(container_name)
            try:
                # Create new Container in the Service
                await container_client.create_container()
            except  Exception as ex:
                logging.info("the container has existed")

            # Get a new BlobClient
            blob_client = container_client.get_blob_client(blob_name)
            await blob_client.upload_blob(data, blob_type="BlockBlob")


async def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    g=Github('<your github usename>','<your github password>')
    repo = g.get_user().get_repo('<your repo path>')
    contents = repo.get_contents(path='',ref='master')
    while contents:
      file_content = contents.pop(0)
      if file_content.type == "dir":
          contents.extend(repo.get_contents(file_content.path))
      else:
          logging.info(file_content.path)
          await upload(file_content.decoded_content,container_name=repo.name,blob_name=file_content.path)

    return func.HttpResponse("OK")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM