C# 使用Azure函数将文件下载并存储到Azure

C# 使用Azure函数将文件下载并存储到Azure,c#,azure,azure-functions,azure-storage,C#,Azure,Azure Functions,Azure Storage,我想从某个第三方服务器下载文件,并希望将其存储在Azure存储中。但我想使用Azure函数执行此过程。下面是我想要实现的步骤 创建Azure(HTTP触发器启用)功能,第三方服务器将使用WebHook执行该功能 使用C#中的“Webclient”使用Webhook提供的下载URL下载文件内容 将文件内容直接存储到Azure存储中 Task.Run(() => { using (var webClient = new WebClient()) {

我想从某个第三方服务器下载文件,并希望将其存储在Azure存储中。但我想使用Azure函数执行此过程。下面是我想要实现的步骤

  • 创建Azure(HTTP触发器启用)功能,第三方服务器将使用WebHook执行该功能
  • 使用C#中的“Webclient”使用Webhook提供的下载URL下载文件内容
  • 将文件内容直接存储到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");
            }
        }
    }
    
    使用系统;
    使用System.IO;
    使用System.Threading.Tasks;
    使用Microsoft.AspNetCore.Mvc;
    使用Microsoft.Azure.WebJobs;
    使用Microsoft.Azure.WebJobs.Extensions.Http;
    使用Microsoft.AspNetCore.Http;
    使用Microsoft.Extensions.Logging;
    使用Newtonsoft.Json;
    使用Microsoft.WindowsAzure.Storage;
    命名空间函数pp5
    {
    公共静态类函数1
    {
    [功能名称(“功能1”)]
    公共静态异步任务运行(
    [HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
    ILogger日志)
    {
    LogInformation(“C#HTTP触发器函数处理了一个请求。”);
    //这是钥匙码。
    //连接到存储帐户。
    var storageAccount=CloudStorageAccount.Parse(“defaultendpointsprotol=https;AccountName=bowmanimagestorage02;AccountKey=xxxxxxx file.core.windows.net/”;
    var myClient=storageAccount.CreateCloudBlobClient();
    //这是门户上的容器名称。
    var container=myClient.GetContainerReference(“测试”);
    //这是您要另存为的名称。
    var blockBlob=container.GetBlockBlobReference(“something.txt”);
    //把小溪放在这个地方。
    等待blockBlob.UploadFromStreamAsync(请求正文);
    字符串名称=请求查询[“名称”];
    string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
    动态数据=JsonConvert.DeserializeObject(requestBody);
    名称=名称??数据?.name;
    返回名称!=null
    ?(ActionResult)新的OkObjectResult($“你好,{name}”)
    :new BadRequestObjectResult(“请在查询字符串或请求正文中传递名称”);
    }
    }
    }
    

    当然,您也可以传入数据流,关键代码相同。

    您可以将文件放入httptrigger的请求正文中,然后创建一个blob并保存它。如果我的回答对您有帮助,您可以结束这个问题吗?谢谢。:)谢谢@BowmanZhu。在网上搜索时,我在某个地方读到,我们可以直接关联存储帐户,而无需编写代码。这是真的还是您建议的方法也是一样的?@DharmeshSolanki关联存储帐户是必要的,我提供的代码也需要关联存储帐户。不必使用代码是什么意思?根据您的描述,您希望使用Azure函数将文件上载到Azure存储,那么您显然需要在Azure函数中编写处理逻辑。是的,您是对的,但在示例代码中,您编写了查找容器、存储帐户、blob等的代码。。。这就是为什么我问like我们是否可以做类似的事情,但是我在Azure方面没有太多经验,您提供的代码肯定会帮助我实现我想要做的事情。@DharmeshSolanki是的,Azure函数支持存储Blob的输出绑定。但是你应该遵循以下链接,而不是blobtrigger:谢谢你的帮助。我将通过你提供的所有链接。