Azure函数可以将Git存储库克隆到Blob存储吗?

问题描述 投票:0回答:1

我的业务需求是,每次提交时都要克隆Github存储库,以首先从逻辑应用程序开始触发工作流,然后触发可将Github存储库克隆到我的Azure存储Blob的Azure函数。我从未在互联网上看到过这样的用例,有人对可以做什么有任何建议?

python azure github azure-functions azure-storage-blobs
1个回答
0
投票

如果您要将Github存储库克隆到我的Azure存储Blob,我们可以使用python sdk PyGithub从仓库中下载文件,然后可以将这些文件上传到Azure blob。

例如(我使用Azure HTTP触发功能)

  1. 安装软件包
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")
© www.soinside.com 2019 - 2024. All rights reserved.