2016-11-24 53 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

相關問題