【问题标题】:No Progress info while uploading file to azure blob storage将文件上传到 azure blob 存储时没有进度信息
【发布时间】:2017-04-08 14:31:50
【问题描述】:

我在我的 android 应用程序中使用 azure blob 存储来存储文件。 为了将文件从 android 手机上传到 blob 存储,我正在使用“CloudBlockBlob”实例 示例:-“cloudBlockBlob.uploadFromFile(File_Path,File_Uri)

问题: 1. 我无法获得上传操作的上传进度。 2.如果由于某些网络问题导致上传失败无法获取报告。 3.上传完成后没有确认报告。

请帮帮我。

【问题讨论】:

标签: azure azure-cloud-services azure-blob-storage


【解决方案1】:

为了更好地控制上传过程,您可以将文件拆分成更小的块,上传单个块,根据上传的块显示进度,并在所有块传输成功后立即提交上传。 您甚至可以同时上传多个区块、在 7 天内暂停/恢复上传或重试失败的区块上传。

这一方面是更多的编码,但另一方面是更多的控制。

作为切入点,这里是一些 C# 示例代码,因为我不熟悉 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);
  }
}

【讨论】:

  • Azure 的 android SDK 很糟糕。 Amazon 的 AWS 开发工具包完成了上传和提供进度反馈的所有繁重工作。这不应该是程序员的责任,毕竟他们提供的是 SDK。它看起来很像 REST API 的简单包装器。太可怕了。
  • @Sascha Dittmann,感谢重播,但它不能解决我在 android 中的问题...
猜你喜欢
  • 2021-03-02
  • 2017-01-24
  • 2017-08-19
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 2021-11-30
相关资源
最近更新 更多