Cant post to Azure storage? Invalid Path error

大兔子大兔子 提交于 2019-12-08 09:10:55

问题


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"


回答1:


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);
 }



回答2:


Your await req.Content.ReadAsMultipartAsync(provider); has provider passed in, but the line above indicates provider2 is the variable. Is it possible you've accidentally got provider defined elsewhere and it's invalid? Usually when I've got a something2, it indicates I'm trying to get something to work and am fearful of losing the initial idea.

If you instead passed in provider2, does it also fail?



来源:https://stackoverflow.com/questions/47225740/cant-post-to-azure-storage-invalid-path-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!