使用Azure函数下载并存储文件到Azure。

4

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

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

       
        Task.Run(() =>
        {
        using (var webClient = new WebClient())
        {
            webClient.Headers.Add(HttpRequestHeader.Authorization, string.Format("Bearer {0}", {下载令牌}));
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            webClient.DownloadFileAsync("https://www.server.com/file1.mp4", {此处我想将文件存储到Azure Storage中});
            }
            });
        
        

您可以将文件放置在httptrigger的请求正文中,然后创建一个Blob并保存它。 - Cindy Pau
1个回答

2
您可以像这样将文件放在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");
        }
    }
}

当然,您也可以传递数据流,密钥代码是相同的。

感谢@BowmanZhu。在互联网上研究时,我读到过一篇文章,称我们可以直接关联存储帐户而不编写代码。这是真的吗?还是您建议的方法也是同样的事情? - Dharmesh Solanki
@DharmeshSolanki 需要关联存储帐户,而我提供的代码也需要关联存储帐户。您所说的不需要使用代码是什么意思?根据您的描述,您想使用Azure Function将文件上传到Azure Storage,那么显然需要在Azure Function中编写处理逻辑。 - Cindy Pau
是的,你说得对。但在示例代码中,你写了一些查找容器、存储帐户、Blob等的代码...这就是为什么我问是否可以做类似https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=csharp 的事情。然而,我在Azure方面没有太多经验,你提供的代码肯定会帮助我实现我想要做的事情。 - Dharmesh Solanki
@DharmeshSolanki 是的,Azure Function支持存储blob的输出绑定。但是,你应该按照这个链接而不是使用blobtrigger:https://learn.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-storage-blob-output?tabs=csharp - Cindy Pau
@DharmeshSolanki 您需要提供存储账户的连接字符串,注意您不能将连接字符串的值直接放入其中。在连接部分填写一个键,并在环境变量配置部分中配置相应的值(在本地,环境变量的值配置在local.settings.json中,在Azure中,请在Configuration中设置)。 - Cindy Pau
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接