0

我想使用 Azure blob 存储中的 Azure 函数应用通过 URL 将上传文件上传到存储位置。我能够从 Azure blob 中提取文件。但无法通过 url 上传文件。下面我附上了我写的代码。有人可以帮我吗?

#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 processed\n");
        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");
                    }
                }
         }
}
4

2 回答 2

0

查看Blob 输出绑定- 这就是从 Azure Functions 上传 Blob 的方式,而不会弄乱 Azure 存储 SDK。

于 2018-07-05T17:58:13.387 回答
0

Azure 函数将多个图像文件上传到 Blob 存储。

  • 使用 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);
        }
    
    
    }
    
于 2019-03-05T09:12:59.850 回答