2

我知道这个问题可以解释为重复,但我根本无法让 blop 服务正常工作。我遵循了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 分钟的公共注释。因此不是问题。我的服务器脚本与链接相同。但在这里复制:

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 分钟。我保存在 mobileservice 表中的 blob 的 url,用户需要在其中进行身份验证,我希望在存储上具有相同的安全性。但不知道这是否实现?对于所有愚蠢的问题,我很抱歉,但我无法自己解决,所以我不得不“看起来”很愚蠢:)

4

1 回答 1

3

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

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

stream.Seek(0, SeekOrigin.Begin);

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

于 2014-04-01T12:06:18.897 回答