Search code examples
c#azureazure-storageazure-blob-storagemultipartform-data

Cant post to Azure storage? Invalid Path error


I am trying to add an image I am receiving like this.

[FunctionName("Imageupload")]
public static async System.Threading.Tasks.Task<HttpResponseMessage> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "HttpTriggerCSharp/name/{name}")]HttpRequestMessage req, string name, TraceWriter log)
{
    //Check if the request contains multipart/form-data.
    if (!req.Content.IsMimeMultipartContent())
    {
        return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);
    }

    var storageConnectionString = "XXXXXXXXXXXXXXXXXXX";

    var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        log.Info(storageConnectionString);
    var blobClient = storageAccount.CreateCloudBlobClient();

    // Retrieve a reference to a container.
    CloudBlobContainer container = blobClient.GetContainerReference("temporary-images");

    // Create the container if it doesn't already exist.
    container.CreateIfNotExists();

    //Retrieve reference to a blob named "myblob".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("images");

    try
    {
        string root = blockBlob.Uri.AbsoluteUri;
        var provider2 = new MultipartFormDataStreamProvider(root);
        await req.Content.ReadAsMultipartAsync(provider);
    }
    catch (StorageException e)
    {
        return req.CreateErrorResponse(HttpStatusCode.NotFound,e.Message);
    }
}

The path I am using is right as I checked it and it points to the storage container.

How Can I send the images I receive to Azure storage?

I recieve the error that the path's format is not supported.

My path looks like this : "https://XXXXXXXXXXX.blob.core.windows.net/temporary-images/images"


Solution

  • I recieve the error that the path's format is not supported.

    As MultipartFormDataStreamProvider requests local file path as parameter, so blob address http/https is not valid.

    If you want to upload mutiple files to Azure storage container, please have try to use req.Content.ReadAsMultipartAsync(); and blockBlob.UploadFromStream(stream); to upload file to azure storage blob. I do a demo, it works correctly on my side.

     if (!req.Content.IsMimeMultipartContent())
     {
         return req.CreateResponse(HttpStatusCode.UnsupportedMediaType);
     }
     var storageConnectionString = "connection string";
     var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
     log.Info(storageConnectionString);
     var blobClient = storageAccount.CreateCloudBlobClient();
     CloudBlobContainer container = blobClient.GetContainerReference("temporary-images");
     container.CreateIfNotExists();
     try
     {
        var filesReadToProvider = await req.Content.ReadAsMultipartAsync();
        foreach (var stream in filesReadToProvider.Contents)
        {
           var blobName = stream.Headers.ContentDisposition.FileName.Trim('"');
           CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);          
           blockBlob.UploadFromStream(stream.ReadAsStreamAsync().Result);
    
        }
    
     }
     catch (StorageException e)
     {
            return req.CreateErrorResponse(HttpStatusCode.NotFound, e.Message);
     }