Uploading a photo stream from camera into azure bl

2019-06-13 20:30发布

问题:

I have the following simple application page that uses the phone camera to upload the taken photo to azure blob:

public partial class AddReport : PhoneApplicationPage
{
    // blobs stuff
    string storageAccount = "MYACCOUNT";
    string storageKey = "MYKEY";
    string blobServiceUri = "http://MYACCOUNT.blob.core.windows.net";
    CloudBlobClient blobClient;

    private Report newReport;
    public AddReport()
    {
        InitializeComponent();   
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        //base.OnNavigatedTo(e);

        newReport = new Report();
        var credentials = new StorageCredentialsAccountAndKey(storageAccount, storageKey);
        blobClient = new CloudBlobClient(blobServiceUri, credentials);
    }

    private void TakePhotoClick(object sender, EventArgs eventArgs)
    {
        //The camera chooser used to capture a picture.
        CameraCaptureTask ctask;

        //Create new instance of CameraCaptureClass
        ctask = new CameraCaptureTask();

        //Create new event handler for capturing a photo
        ctask.Completed += new EventHandler<PhotoResult>(ctask_Completed);

        //Show the camera.
        ctask.Show();

    }

    void ctask_Completed(object sender, PhotoResult e)
    {

        if (e.TaskResult == TaskResult.OK && e.ChosenPhoto != null)
        {

            WriteableBitmap CapturedImage = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
            UploadToBlobContainer(e.ChosenPhoto);
        }
        else
        {
            //user decided not to take a picture
        }
    }
    private void UploadToBlobContainer(System.IO.Stream stream)
    {
        string containerName = "reportsPhotos";
        var container = blobClient.GetContainerReference(containerName);

        container.CreateIfNotExist(true, r =>
            Dispatcher.BeginInvoke(() =>
            {
                var blobName = "report" + newReport.ReportId.ToString();
                var blob = container.GetBlobReference(blobName);
                blob.Metadata["ReportId"] = newReport.ReportId.ToString();
                blob.UploadFromStream(stream, r2 =>
                    Dispatcher.BeginInvoke(() =>
                    {
                         newReport.Photo = container.Uri + "/" + blobName;
                    }));
            }));

    }
}

This is a simple case and I am not using SAS to authenticate, instead I save the key in the app itself (this is only for testing purposes) and also my blobs are publicly available.

when I run in debug mode it seems that everything is working, but the photo doesn't get uploaded to the blob. Also, I don't know how I can debug this to see if there was any error from the blob service.

Can anyone tell me what might be wrong ?

EDIT1: it seems that the container is not being created either. i've confirmed this using azure blob explorer

EDIT2: I am getting a System.Net.WebException : "The remote server returned an error: NotFound."

回答1:

After long hours I have finally discovered that the problem was with this line:

string containerName = "reportsPhotos";

According to here all letters in a container name must be lowercase. Changing it to reportsphotos solved the issue

That was time well spent.



回答2:

Can you try just doing it like this instead:

// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

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

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blob.UploadFromStream(fileStream);
} 

This is from:

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/#upload-blob