【问题标题】:Uploading a photo stream from camera into azure blob in WP7将照片流从相机上传到 WP7 中的 azure blob
【发布时间】:2012-04-28 08:09:16
【问题描述】:

我有以下简单的应用程序页面,它使用手机摄像头将拍摄的照片上传到 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;
                    }));
            }));

    }
}

这是一个简单的案例,我没有使用 SAS 进行身份验证,而是将密钥保存在应用程序本身中(这仅用于测试目的),而且我的 blob 也是公开可用的。

当我在调试模式下运行时,似乎一切正常,但照片没有上传到 blob。另外,我不知道如何调试它以查看 blob 服务是否有任何错误。

谁能告诉我可能出了什么问题?

EDIT1:似乎也没有创建容器。我已经使用azure blob explorer 确认了这一点

EDIT2:我收到了System.Net.WebException : "The remote server returned an error: NotFound."

【问题讨论】:

  • 您使用什么库来访问 WA 存储?它是 Windows Phone 的 WA 工具包吗?你怎么知道它不起作用? (您使用什么工具在 blob 存储中查找 blob?)
  • @smarx 我正在使用 WAT 中的 WindowsPhoneCloud.StorageClientMicrosoft.Samples.Data.Services.Client 用于 WP。要查看 blob 是否已上传,我使用的是 azure blob 存储资源管理器:blobexplorer.codeplex.com

标签: silverlight windows-phone-7 azure azure-storage azure-blob-storage


【解决方案1】:

经过很长时间,我终于发现问题出在这行:

string containerName = "reportsPhotos";

根据here,容器名称中的所有字母都必须是小写。 将其更改为reportsphotos 解决了问题

那是值得的时间。

【讨论】:

    【解决方案2】:

    你可以试试这样吗:

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

    来自:

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

    【讨论】:

    • 似乎与 Windows Phone 项目无关。
    猜你喜欢
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2011-04-11
    • 2016-03-06
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多