2016-11-24 4 views
0

Ich verwende den azure Blob-Speicher in meiner Android App, um Dateien zu speichern. für die Dateien von Android-Handy zu Blob Speicher Hochladen i "CloudBlockBlob" Instanz Beispiel bin mit: - „cloudBlockBlob.uploadFromFile (File_Path, File_Uri)Nein Fortschrittsinformationen beim Hochladen der Datei in den azure Blob-Speicher

Ausgabe: 1. Ich bin Fortschritte zu bekommen nicht in der Lage auf Upload hochladen Aktion. 2. wenn Upload schlägt fehl, aufgrund einiger Netzwerkprobleme nicht in der Lage, den Bericht zu erhalten. 3. keinen Bericht bestätigt nach erfolgten Upload.

bitte helfen Sie mir das mit.

+0

In Bezug auf # 1, diese finden Sie unter: http://stackoverflow.com/questions/21175293/How-To-Track-Fortschritt-von-Async-Datei-Upload-zu-Azure-Speicher. Für # 2 und # 3 teilen Sie bitte mehr Code. Wie machen Sie Fehlerbehandlung in Ihrem Code? –

Antwort

1

über die mehr Kontrolle haben Upload-Prozess, können Sie Ihre Dateien in kleinere Blöcke teilen, laden Sie die Single Blöcke, zeigen den Fortschritt anhand der hochgeladenen Blöcke an und übernehmen den Upload, sobald alle Blöcke erfolgreich übertragen wurden. Sie können sogar mehrere Blöcke gleichzeitig hochladen, den Upload innerhalb von 7 Tagen anhalten/fortsetzen oder fehlerhafte Block-Uploads erneut versuchen.

Dies ist zum einen mehr Codierung, aber zum anderen mehr Kontrolle.

als Einstiegspunkt, hier einige Beispielcode in C#, da ich mit Java für Android nicht vertraut bin:

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 sind schrecklich. Das AWS-SDK von Amazon macht das Hochladen von Inhalten und das Bereitstellen von Fortschrittsfeedback überflüssig. Es sollte nicht die Verantwortung des Programmierers sein, schließlich stellen sie ein SDK zur Verfügung. Es ähnelt stark einem einfachen Wrapper über die REST-API. Furchtbar. – Loudenvier

+0

@Sascha Dittmann, danke für die Wiederholung, aber es löst nicht mein Problem in Android ... – Manu

+0

das hat mir geholfen .. http://StackOverflow.com/Questions/34616554/merging-multiple-azures-cloud-block- Blobs-in-Android – Manu

Verwandte Themen