有人可以描述我如何配置使用HTTP输入欺骗器和Blob存储输出触发器的C#azure函数吗?
也许还有一个示例代码片段和示例function.json,但我不知道它是否可以与azure函数核心工具一起在本地使用。
答案 0 :(得分:1)
这是一个组合的HTTP触发函数,具有输出Blob绑定:
[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的引用。
一旦您引用了Blob,就可以使用其他方法上载:
cloudBlockBlob.UploadFromByteArrayAsync()
cloudBlockBlob.UploadFromFileAsync()
cloudBlockBlob.UploadTextAsync()
cloudBlockBlob.UploadFromStreamAsync()
要使其在本地运行,您需要进行一些设置。注意在我的示例中,属性[Blob("blobcontainer", Connection = "StorageConnectionString")]
outputContainer.CreateIfNotExistsAsync();
在存储帐户中创建的容器的名称(如果尚不存在)。local.settings.json
中存储帐户连接字符串的设置。在本地进行开发时,建议将其设置为"UseDevelopmentStorage=true"
,以便您可以利用存储模拟器。然后,当您准备部署到Azure时,您将在函数应用程序中创建一个包含实际连接字符串的设置。local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"StorageConnectionString": "UseDevelopmentStorage=true"
}
}
答案 1 :(得分:0)
您需要的一切都在“官方文档”页面上
(ii) Output binding blob storage
Http触发示例代码
[FunctionName("HttpTriggerCSharp")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]
HttpRequest req, ILogger log)
Blob存储输出绑定
[FunctionName("ResizeImage")]
public static void Run(
[BlobTrigger("sample-images/{name}")] Stream image,
[Blob("sample-images-sm/{name}", FileAccess.Write)] Stream imageSmall,
[Blob("sample-images-md/{name}", FileAccess.Write)] Stream imageMedium)