将图像上传到azure blob存储

时间:2014-03-30 14:48:52

标签: c# azure windows-phone-8 azure-mobile-services blobstorage

我知道这个问题可以解释为重复,但我根本无法让blop服务工作。我遵循了标准example on msdn。我在我的代码中实现了但是遵循了示例。我可以通过示例中提供的脚本获取我的MobileService,以插入具有开放属性的blob。然后我使用此代码将图像上传到blob存储:

 BitmapImage bi = new BitmapImage();
 MemoryStream stream = new MemoryStream();
 if (bi != null)
 {
      WriteableBitmap bmp = new WriteableBitmap((BitmapSource)bi);
      bmp.SaveJpeg(stream, bmp.PixelWidth, bmp.PixelHeight, 0, 100);
 }

 if (!string.IsNullOrEmpty(uploadImage.SasQueryString))
 {
       // Get the URI generated that contains the SAS 
       // and extract the storage credentials.
       StorageCredentials cred = new StorageCredentials(uploadImage.SasQueryString);
       var imageUri = new Uri(uploadImage.ImageUri);

       // Instantiate a Blob store container based on the info in the returned item.
       CloudBlobContainer container = new CloudBlobContainer(
       new Uri(string.Format("https://{0}/{1}",
       imageUri.Host, uploadImage.ContainerName)), cred);

       // Upload the new image as a BLOB from the stream.
       CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(uploadImage.ResourceName);
       await blobFromSASCredential.UploadFromStreamAsync(stream);//error!

       // When you request an SAS at the container-level instead of the blob-level,
       // you are able to upload multiple streams using the same container credentials.

       stream = null;
 }

我在此代码中出现错误标记错误,出现以下错误:

+       ex  {Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.

我不明白,因为从脚本返回字符串的代码是:

// Generate the upload URL with SAS for the new image.
var sasQueryUrl = blobService.generateSharedAccessSignature(item.containerName, 
item.resourceName, sharedAccessPolicy);

// Set the query string.
item.sasQueryString = qs.stringify(sasQueryUrl.queryString);

// Set the full path on the new new item, 
// which is used for data binding on the client. 
item.imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path;

当然这也说明我并没有完全掌握blob存储的构造。因此,任何帮助将不胜感激。

评论详解 从服务器代码中,它应该创建一个公共注释至少5分钟。因此不是问题。我的服务器脚本与the link相同。但在这里复制:

var azure = require('azure');
var qs = require('querystring');
var appSettings = require('mobileservice-config').appSettings;

function insert(item, user, request) {
// Get storage account settings from app settings. 
var accountName = appSettings.STORAGE_ACCOUNT_NAME;
var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
var host = accountName + '.blob.core.windows.net';

if ((typeof item.containerName !== "undefined") && (
item.containerName !== null)) {
    // Set the BLOB store container name on the item, which must be lowercase.
    item.containerName = item.containerName.toLowerCase();

    // If it does not already exist, create the container 
    // with public read access for blobs.        
    var blobService = azure.createBlobService(accountName, accountKey, host);
    blobService.createContainerIfNotExists(item.containerName, {
        publicAccessLevel: 'blob'
    }, function(error) {
        if (!error) {

            // Provide write access to the container for the next 5 mins.        
            var sharedAccessPolicy = {
                AccessPolicy: {
                    Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
                    Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
                }
            };

            // Generate the upload URL with SAS for the new image.
            var sasQueryUrl = 
            blobService.generateSharedAccessSignature(item.containerName, 
            item.resourceName, sharedAccessPolicy);

            // Set the query string.
            item.sasQueryString = qs.stringify(sasQueryUrl.queryString);

            // Set the full path on the new new item, 
            // which is used for data binding on the client. 
            item.imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path;

        } else {
            console.error(error);
        }

        request.execute();
    });
} else {
    request.execute();
}
}

图片的想法是应用程序的其他用户应该能够访问它们。据我所知,我已公开,但只公开了5分钟。我保存在移动服务表中的blob的url,用户需要进行身份验证,我希望存储上的安全性相同。但是不知道这是否成功了?我很抱歉所有愚蠢的问题,但我无法自己解决,所以我必须“看起来”愚蠢:))

1 个答案:

答案 0 :(得分:3)

如果有人最终需要帮助。对我来说问题是uri。它应该是http而不是https。然后没有上传错误。

但是,即使在工具箱中的测试图像控件上显示图像也没有成功。问题是我必须将流设置为开头:

stream.Seek(0, SeekOrigin.Begin);

然后上传工作,并能够检索数据。