Azure Function App Phyton - 访问存储帐户

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

我在 Linux 主机上创建了一个函数应用程序。我通过管道进行部署,一切都按预期进行。在我开始导入天蓝色库之前,这些功能还没有部署。没有错误。

#When adding below functions fails to show in the azure portal. 
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

完整代码:

import logging
import azure.functions as func
import json
from azure.identity import DefaultAzureCredential
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient

app = func.FunctionApp()

import logging
import azure.functions as func

app = func.FunctionApp()


@app.route(route="get_port_data", auth_level=func.AuthLevel.FUNCTION)
def get_port_data(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    lat = req.params.get('lat')
    long = req.params.get('lng')

    # credential = DefaultAzureCredential()
    # blob_service_client = BlobServiceClient(account_url="https://xx.blob.core.windows.net", credential=credential)    

    jsn = {"region":"USAC","port":"West"}

    return func.HttpResponse(
        json.dumps(jsn),
        mimetype="application/json",
            status_code=200
    )

Deployment pipeline:

# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- none

variables:
# Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: 'xx'

# Web app name
  webAppName: 'xx'

# Agent VM image name
  vmImageName: 'ubuntu-latest'

# Environment name
  environmentName: 'func-xx'

# Project root folder.
  projectRoot: $(System.DefaultWorkingDirectory)

# Python version: 3.11. Change this to match the Python runtime version running on your web app.
  pythonVersion: '3.11'

  workingDirectory: ''

pool:
  vmImage: $(vmImageName)

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    # - bash: |
    #     if [ -f extensions.csproj ]
    #     then
    #         dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
    #     fi
    #   workingDirectory: $(workingDirectory)
    #   displayName: 'Build extensions'

    - task: UsePythonVersion@0
      displayName: 'Use Python 3.11'
      inputs:
        versionSpec: 3.11

    - bash: |
        pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(workingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop
- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureServiceConnectionId)'
              appType: functionAppLinux
              appName: $(webAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
python azure azure-functions
1个回答
0
投票

在Python函数中,当代码语法不正确时,即使部署正确,也不会在门户中显示。

要从 blob 容器访问文件,请使用下面给定的代码。 这段代码对我有用。

function_app.py
:

import azure.functions as func
import logging
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential


app = func.FunctionApp()


@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    accountName = "vivekrgb97d"
    containerName = "test"
    fileName = "upload.txt"


    credential = DefaultAzureCredential()
    blobServiceClient = BlobServiceClient(account_url=f"https://{accountName}.blob.core.windows.net/", credential= credential)

    containerClient = blobServiceClient.get_container_client(containerName)
    blobClient = containerClient.get_blob_client(fileName)

    content = blobClient.download_blob().readall().decode("utf-8")

    return func.HttpResponse(content)
    

azure-pipelines.yml
:

trigger:
- main

variables:

  azureSubscription: 'xxxxxxxxxxxxxxxxx'

  functionAppName: 'pyfunc22aug'

  vmImageName: 'ubuntu-latest'

  workingDirectory: '.'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - bash: |
        if [ -f extensions.csproj ]
        then
            dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
        fi
      workingDirectory: $(workingDirectory)
      displayName: 'Build extensions'

    - task: UsePythonVersion@0
      displayName: 'Use Python 3.11'
      inputs:
        versionSpec: 3.11 

    - bash: |
        pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(workingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionAppLinux
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

使用

DefaultAzureCredential
时,请确保根据需要执行的操作分配所需的角色。

作为参考,请检查此文档

OUTPUT

© www.soinside.com 2019 - 2024. All rights reserved.