2014-03-30 61 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