0
votes

I am using azure blob storage in my android app to store files. for uploading the files from android phone to blob storage i am using "CloudBlockBlob" instance Example:- "cloudBlockBlob.uploadFromFile(File_Path,File_Uri)

issue: 1. i am not able to get upload progress on upload action. 2. if upload fails due to some network issues not able to get the report. 3. no acknowledge report after upload completed.

please help me with this.

1
Regarding #1, please see this: stackoverflow.com/questions/21175293/…. For #2 and #3, please share more code. How are you doing error handling in your code?Gaurav Mantri

1 Answers

1
votes

to have more control over the upload process, you could split your files into smaller blocks, upload the single blocks, show the progress based on the uploaded blocks and commit the upload as soon as all blocks are transfered successfully. You can even upload multiple blocks at the same time, pause / resume the upload within 7 days or retry failed block-uploads.

This is on the one hand more coding, but on the other hand more control.

As an entry point, here is some sample code in C# since I'm not familiar with Java for Android:

CloudBlockBlob blob = cloudBlobContainer.GetBlockBlobReference(Path.GetFileName(fileName));

int blockSize = 256 * 1024; //256 kb

using (FileStream fileStream = 
  new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
  long fileSize = fileStream.Length;

  //block count is the number of blocks + 1 for the last one
  int blockCount = (int)((float)fileSize / (float)blockSize) + 1;

  //List of block ids; the blocks will be committed in the order of this list 
  List<string> blockIDs = new List<string>();

  //starting block number - 1
  int blockNumber = 0;

  try
  {
    int bytesRead = 0; //number of bytes read so far
    long bytesLeft = fileSize; //number of bytes left to read and upload

    //do until all of the bytes are uploaded
    while (bytesLeft > 0)
    {
      blockNumber++;
      int bytesToRead;
      if (bytesLeft >= blockSize)
      {
        //more than one block left, so put up another whole block
        bytesToRead = blockSize;
      }
      else
      {
        //less than one block left, read the rest of it
        bytesToRead = (int)bytesLeft;
      }

      //create a blockID from the block number, add it to the block ID list
      //the block ID is a base64 string
      string blockId = 
        Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("BlockId{0}",
          blockNumber.ToString("0000000"))));
      blockIDs.Add(blockId);
      //set up new buffer with the right size, and read that many bytes into it 
      byte[] bytes = new byte[bytesToRead];
      fileStream.Read(bytes, 0, bytesToRead);

      //calculate the MD5 hash of the byte array
      string blockHash = GetMD5HashFromStream(bytes);

      //upload the block, provide the hash so Azure can verify it
      blob.PutBlock(blockId, new MemoryStream(bytes), blockHash);

      //increment/decrement counters
      bytesRead += bytesToRead;
      bytesLeft -= bytesToRead;
    }

    //commit the blocks
    blob.PutBlockList(blockIDs);
  }
  catch (Exception ex)
  {
    System.Diagnostics.Debug.Print("Exception thrown = {0}", ex);
  }
}