如何使用 Azure 函数应用通过 URL 将文件上传到存储位置



我想使用 Azure Blob 存储中的 Azure 函数应用通过 URL 将文件上传到存储位置。我可以从 Azure blob 中提取文件。但无法通过网址上传文件。 下面我附上了我编写的代码。谁能帮我解决这个问题?

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "System.IO"

using System;
using System.IO;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using System.Xml;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net;
public static void Run(string input, TraceWriter log)
{
log.Info($"C# manual trigger function processedn");
const string StorageAccountName = "";
const string StorageAccountKey = "";
var storageAccount = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageAccountKey), true);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("hannahtest");
var Destcontainer = blobClient.GetContainerReference("hannahtestoutput");
var blobs = container.ListBlobs();
log.Info($"Creating Client and Connecting");
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item is CloudBlockBlob blockBlob)
{
using (StreamReader reader = new StreamReader(blockBlob.OpenRead())                                     
{
//old content string will read the blockblob (xml)till end 
string oldContent1 = reader.ReadToEnd();    
log.Info(oldContent1);
var content = new FormUrlEncodedContent(oldContent1);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
log.Info($"Success");
}
}
}
}

查看 Blob 输出绑定 - 这就是 Blob 的预期方式,不会干扰 Azure 存储 SDK。

用于将多个映像文件上传到 Blob 存储的 Azure 函数。

  • 使用 Microsoft.WindowsAzure.Storage.Auth;
  • 使用 Microsoft.WindowsAzure.Storage;
  • 使用 Microsoft.WindowsAzure.Storage.Blob;

    public static class ImageUploadFunction
    {
    [FunctionName("ImageUploadFunction")]
    public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post")]HttpRequestMessage req, ILogger log)
    {
    var provider = new MultipartMemoryStreamProvider();
    await req.Content.ReadAsMultipartAsync(provider);
    var files = provider.Contents;
    List<string> uploadsurls = new List<string>();
    foreach (var file in files) {
    var fileInfo = file.Headers.ContentDisposition;
    Guid guid = Guid.NewGuid();
    string oldFileName = fileInfo.FileName;
    string newFileName = guid.ToString();
    var fileExtension = oldFileName.Split('.').Last().Replace(""", "").Trim();
    var fileData = await file.ReadAsByteArrayAsync();
    try {
    //Upload file to azure blob storage method
    var upload = await UploadFileToStorage(fileData, newFileName + "." + fileExtension);
    uploadsurls.Add(upload);
    }
    catch (Exception ex) {
    log.LogError(ex.Message);
    return new BadRequestObjectResult("Somthing went wrong.");
    }
    }
    return uploadsurls != null
    ? (ActionResult)new OkObjectResult(uploadsurls)
    : new BadRequestObjectResult("Somthing went wrong.");                 
    }
    private static async Task<string> UploadFileToStorage(byte[] fileStream, string fileName)
    {
    // Create storagecredentials object by reading the values from the configuration (appsettings.json)
    StorageCredentials storageCredentials = new StorageCredentials("<AccountName>", "<KeyValue>");
    // Create cloudstorage account by passing the storagecredentials
    CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);
    // Create the blob client.
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
    CloudBlobContainer container = blobClient.GetContainerReference("digital-material-library-images");
    // Get the reference to the block blob from the container
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
    // Upload the file
    await blockBlob.UploadFromByteArrayAsync(fileStream,0, fileStream.Length);
    blockBlob.Properties.ContentType = "image/jpg";
    await blockBlob.SetPropertiesAsync();
    return blockBlob.Uri.ToString();
    //return await Task.FromResult(true);
    }
    
    }
    

相关内容

  • 没有找到相关文章

最新更新