azure function c# http trigger blob output

azure function c# http trigger blob output(azure function c# http trigger blob output)

本文介绍了azure function c# http trigger blob output的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能描述我如何配置使用 HTTP 输入触发器和 blob 存储输出触发器的 C# azure 函数?

Can someone describe me how I can configure a C# azure function which uses an HTTP input trigger and a blob storage output trigger?

也许还有一个示例代码片段和一个示例 function.json.我无法使用 azure functions 核心工具在本地工作.

Maybe also with an example code snippet and an example function.json. I don't get it to work locally with the azure functions core tools.

推荐答案

这是一个带有输出 blob 绑定的组合 HTTP 触发函数:

This is a combined HTTP triggered function with a output blob binding:

[FunctionName("HttpTriggeredFunction")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest httpRequest,
    [Blob("blobcontainer", Connection = "StorageConnectionString")] CloudBlobContainer outputContainer,
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    await outputContainer.CreateIfNotExistsAsync();

    var requestBody = await new StreamReader(httpRequest.Body).ReadToEndAsync();
    var blobName = Guid.NewGuid().ToString();

    var cloudBlockBlob = outputContainer.GetBlockBlobReference(blobName);
    await cloudBlockBlob.UploadTextAsync(requestBody);

    return new OkObjectResult(blobName);
}

它使用 CloudBlobContainer 输出类型来获取对 blob 容器的引用,然后您可以使用诸如 .GetBlockBlobReference("blobPath") 之类的方法来获取对 blob 的引用.

It uses the CloudBlobContainer output type to get a reference to the blob container which then enables you to use methods such as .GetBlockBlobReference("blobPath") to get a reference to a blob.

一旦你有一个 blob 的引用,你就可以使用不同的方法来上传:

Once you have a reference to a blob, you can use different methods to upload:

  • cloudBlockBlob.UploadFromByteArrayAsync()
  • cloudBlockBlob.UploadFromFileAsync()
  • cloudBlockBlob.UploadTextAsync()
  • cloudBlockBlob.UploadFromStreamAsync()

要让它在本地运行,您需要进行一些设置.请注意我的示例中的属性 [Blob("blobcontainer", Connection = "StorageConnectionString")]

To get it running locally, you need set some things up. Notice in my example the attribute [Blob("blobcontainer", Connection = "StorageConnectionString")]

  • "blobcontainer" 这可以是您想要的任何内容,并且将是通过这一行 outputContainer.CreateIfNotExistsAsync(); 在您的存储帐户中创建的容器的名称(如果不是已经存在).
  • Connection = "StorageConnectionString" 这可以是您的 local.settings.json 中用于存储帐户连接字符串的设置.在本地开发时,我建议将其设置为 "UseDevelopmentStorage=true" 以便您可以利用存储模拟器.然后,当您准备好部署到 Azure 上时,您将在函数应用中创建一个包含实际连接字符串的设置.
  • "blobcontainer" this can be whatever you want and will be the name of the container that will be created in your storage account by this line outputContainer.CreateIfNotExistsAsync(); (if it doesn't exist already).
  • Connection = "StorageConnectionString" this can be a setting in your local.settings.json for the connection string of your storage account. When developing locally I would recommend setting this to "UseDevelopmentStorage=true" so that you can take advantage of the storage emulator. Then when you are ready to deploy onto Azure, you would create a setting in the function app containing the real connection string.

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "StorageConnectionString": "UseDevelopmentStorage=true"
  }
}

这篇关于azure function c# http trigger blob output的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:azure function c# http trigger blob output

基础教程推荐