我想从某个第三方服务器下载文件,并想存储在Azure存储中。但是我想使用Azure函数执行此过程。以下是我要实现的步骤。
将文件内容直接存储到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});
}
});
您可以像这样将文件放入请求的请求主体中,然后将其上传到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");
}
}
}
当然您也可以传入数据流,其关键代码是相同的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句