2016-11-24 52 views
0

我在我的android应用中使用azure blob存储来存储文件。 上传从Android手机Blob存储中的文件,我使用“CloudBlockBlob”实例 例子: - “cloudBlockBlob.uploadFromFile(FILE_PATH,File_Uri)将文件上传到azure blob存储时没有进度信息

问题: 1.我不能够让上传的上传进度动作。 2.如果上传失败,由于一些没能获得该报告的网络问题。 3.不承认报告上传结束后。

请帮助我。

+0

关于#1,请参阅本:http://stackoverflow.com/questions/21175293 /如何对跟踪进度 - 的 - 异步文件上传到Azure的存储。对于#2和#3,请分享更多代码。你如何在你的代码中进行错误处理? –

回答

1

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

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

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

Azure的android SDK非常糟糕。亚马逊的AWS开发工具包完成所有繁重的上传工作并提供进度反馈。它不应该是程序员的责任,毕竟他们提供了一个SDK。它看起来很像REST API的简单包装。可怕。 – Loudenvier

+0

@Sascha Dittmann,感谢您的重播,但它并没有解决我在Android的问题... – Manu

+0

这帮了我.. http://stackoverflow.com/questions/34616554/merging-multiple-azures-cloud-block-斑点功能于安卓 – Manu

相关问题