gpt4 book ai didi

c# - 使用Azure功能下载文件并将其存储到Azure

转载 作者:行者123 更新时间:2023-12-04 04:15:42 27 4
gpt4 key购买 nike

我想从第三方服务器下载文件并存储在 Azure 存储中。但我想使用 Azure 功能执行此过程。以下是我想要实现的步骤。

  1. 使用 WebHook 创建将由第三方服务器执行的 Azure(HTTP 触发器启用)函数。
  2. 使用 C# 中的“Webclient”通过 Webhook 提供的下载 URL 下载文件内容。
  3. 将文件内容直接存储到 Azure 存储中。

       
    Task.Run(() =>
    {
    using (var webClient = new WebClient())
    {
    webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("Bearer {0}", {download token}));
    webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
    webClient.DownloadFileAsync("https://www.server.com/file1.mp4", {Here I want to store file into Azure Storage});
    }
    });

最佳答案

您可以像这样将文件放入 req 的请求正文中,然后将其上传到 Azure Blob 存储。

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.Storage;

namespace FunctionApp5
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");

//Here is the keycode.

//Connect to the Storage Account.
var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=bowmanimagestorage02;AccountKey=xxxxxxxfile.core.windows.net/");
var myClient = storageAccount.CreateCloudBlobClient();

//Here is the container name on portal.
var container = myClient.GetContainerReference("test");

//Here is the name you want to save as.
var blockBlob = container.GetBlockBlobReference("something.txt");

//Put the stream in this place.
await blockBlob.UploadFromStreamAsync(req.Body);


string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}
}
}

当然你也可以传入数据流,关键代码是一样的。

关于c# - 使用Azure功能下载文件并将其存储到Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60752914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com