locked
PageBlob WritePages gives "The page range specified is invalid." exception on last chunk RRS feed

  • Question

  • I can call CloudBlob.UploadFile fine. Now I need to deal with larger files so am experimenting with PageBlob. I've called Create and made my file size a multiple of 512. I'm uploading chunks that are multiples of 512. All seems OK until the final chunk when I get the above exception. There are people out there uploading whole VHDs and I can't upload a crummy little jpg. AAGH.

    Other posts: http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/814cdaf6-e168-401d-942c-053d7b3666a6/

    Development Storage version 1.0.0.0, Azure SDK V 1.0

     

     

    private void Button_Click_5(object sender, RoutedEventArgs e)

    {

     

    var client = new Microsoft.WindowsAzure.StorageClient.CloudBlobClient(

    Microsoft.WindowsAzure.

    CloudStorageAccount.DevelopmentStorageAccount.BlobEndpoint,

    Microsoft.WindowsAzure.

    CloudStorageAccount.DevelopmentStorageAccount.Credentials

    );

     

    var dlg = new System.Windows.Forms.OpenFileDialog() { Filter = "Images|*.jpg|All files|*.*", Title = "Select file to upload" };

    dlg.ShowDialog();

     

    var fn = dlg.FileName;

     

    if(fn != null && System.IO.File.Exists(fn))

    {

     

    try

    {

     

    int blockSize = 512 * 8;

     

    long offset = 0;

     

    var container = client.GetContainerReference("sandpit");

    container.CreateIfNotExist();

     

    var pb = container.GetPageBlobReference(System.IO.Path.GetFileName(fn));

     

    var fileSize = new System.IO.FileInfo(fn).Length;

    fileSize += (512 - (fileSize % 512));

    pb.Create(fileSize);

     

    using (var st = System.IO.File.OpenRead(fn))

    {

     

    while (offset < fileSize - 1)

    {

     

    var buffer = new byte[blockSize];

     

    var bytesRead = st.Read(buffer, 0, blockSize);

     

    using (var ms = new System.IO.MemoryStream(buffer))

    {

    pb.WritePages(ms, offset);

    }

    offset += blockSize;

    }

    }

    }

     

    catch (Microsoft.WindowsAzure.StorageClient.StorageClientException ____)

    {

    }

     

    catch (Exception ex)

    {

    }

    }

     

    Wednesday, September 22, 2010 5:09 PM

Answers

  • Try printing out the file size and the offset you're writing to at each iteration... presumably it's some little off-by-one error.
    • Marked as answer by Nick Ajd Wednesday, September 22, 2010 9:52 PM
    Wednesday, September 22, 2010 6:39 PM

All replies

  • Try printing out the file size and the offset you're writing to at each iteration... presumably it's some little off-by-one error.
    • Marked as answer by Nick Ajd Wednesday, September 22, 2010 9:52 PM
    Wednesday, September 22, 2010 6:39 PM
  • Thank you! my last page was too big, I was concentrating too hard on the 512 bit.

    FileSize += (BlockSize-(FileSize % BlockSize));

    Wednesday, September 22, 2010 9:56 PM