2014-03-30 188 views
2

我知道这个问题可以被解释为重复,但我可以根本不能让blop服务工作。我遵循标准example on msdn。我已经在我的代码中实现,但是遵循了这个例子。我可以通过示例中提供的脚本来获取MobileService,以便插入具有打开属性的Blob。然后我用这个代码将图像上传到Blob存储:上传图像到azure 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保存在mobileservice表中,用户需要进行身份验证,我希望存储上具有相同的安全性。但不知道这是否完成?我很抱歉所有愚蠢的问题,但我一直无法自己解决,所以我不得不“看起来”愚蠢:)

+1

有点愚蠢的问题,但......容器是否存在? –

+0

是的,它存在于存储器中,这就是为什么我无法理解它。并感谢您的理解 – JTIM

+0

我建议的一件事是通过Fiddler追踪您的请求/响应。这应该给你更多的信息。请让我们知道你找到了什么。 –

回答

3

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

但是,即使在来自工具箱的测试图像控件上显示图像也没有成功。问题是,我不得不设置流开始时:

stream.Seek(0, SeekOrigin.Begin); 

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

+0

坦白地说,我不认为改变HTTP是一个修复,也许是一个快速入侵,但也许我可能是错误的 – Citrus

+1

@LászlóCitrusNagy你可能是对的,但它工作我。但是,如果你有其他方法可以做到这一点,请随时告诉我,我当然会为你的麻烦投票;) – JTIM

+1

我已经在研究这个,如果它会完成的话,我会发布为回答:) – Citrus