Upload image to Azure blob storage from Windows Ph

2019-04-13 19:38发布

问题:

I am using Windows Azure to store images in my windows phone application.

The camera takes a photo and the chosen photo stream is then uploaded. However it is throwing NO error but is not uploading?

            var blobContainer = CloudStorageContext.Current.Resolver.CreateCloudBlobClient();
            var container = blobContainer.GetContainerReference("pics");


            var blob = container.GetBlobReference("picture.jpg");
            blob.UploadFromStream(e.ChosenPhoto, response => { MessageBox.Show(blob.Uri.ToString()) });

I don't have a clue what is happening. The Resolver contains the correct user, key and urls. The container "pics" does exist, but no image is being uploaded. The message box pops up with a url which does not exist.

UPDATE - There seems to be a simular (well almost identical) question posted here - Uploading a photo stream from camera into azure blob in WP7. However the upper case container name is not an issue here, so that solution did not fix this

回答1:

I have an application (Windows Phone 8) that uploads an image taken to an Azure webrole, which in turn stores the image in an Azure Storage Blob. The code below is how the server stores the images. Again, this code does not run on the phone, but you can use it as a reference.

                string randomGUID = locationID
                + "-"
                + Guid.NewGuid().ToString();

            //Retrieve storage account from application settings
            CloudStorageAccount storageAccount = GetStorageAccount();

            //Create blob client
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Retrieve reference to images container
            CloudBlobContainer container = blobClient.GetContainerReference(
                RoleEnvironment.GetConfigurationSettingValue("BlobContainer"));                    

            //Retrieve references to the blob inside the container
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomGUID);

            blockBlob.UploadFromStream(imageToUpload);

The variable imageToUpload is of the type Stream.

As you can see, this is pretty straightforward code. Perhaps your problem has to do with the lambda expression you have in UploadFromStream?