2017-06-15 1 views
0

Ich möchte ein Blob auf ein anderes Speicherkonto kopieren, wenn das Ziel nicht existiert. Aber ich muß die Dokumentation auf StartCopyAsync und GenerateIfNot​Exists​Condition wird Verlesen, weil ich dachte, dass ich es in einem Aufruf tun könnte (ohne Ziel Existenz in einem separaten zu überprüfen) mit etwas wie folgt aus:Warum wirft StartCopyAsync() 409 Conflict, wenn GenerateIfNotExistsCondition für Ziel verwendet wird?

await _targetClient.StartCopyAsync(
     _schemaBlob, 
     null, 
     _schemaBlob.Name, 
     AccessCondition.GenerateIfNotExistsCondition(), 
     null, 
     ContainerName, 
     cancellationToken); 

Aber es wirft ein 409 Conflict wenn das Ziel-Blob existiert. Ist das nicht der Punkt des AccessCondition.GenerateIfNotExistsCondition() Parameters, um sicherzustellen, dass die Operation nichts tut, wenn der Blob existiert?

Wie geht das richtig?

Antwort

2

Aber es wirft einen 409 Konflikt, wenn das Ziel Blob existiert. Ist das nicht der Punkt des Parameters AccessCondition.GenerateIfNotExistsCondition(), um sicherzustellen, dass der Vorgang nur dann ausgeführt wird, wenn der Blob existiert?

Auf Azure Storage Service-Seite wird es nichts tun und nur einen 409-Statuscode zurückgeben. Wenn der Rückgabecode auf Ihrer Clientseite nicht gleich 200 ist, wird eine Ausnahme ausgelöst. Ich schlage vor, dass Sie Ihrem Code einen try-catch-Blob hinzufügen und im catch-Blob nichts tun.

try 
{ 
    //put your copy code here 
} 
catch (StorageException ex) 
{ 
    //If the exception is 409, just skip the exception 
    if (!ex.Message.Contains("409")) 
    { 
     throw ex; 
    } 
} 

Andernfalls könnten Sie überprüfen, ob das Ziel blob vorhanden ist, bevor Sie den Kopierbefehl tun.

if (targetBlob.Exists()) 
{ 
    //do the copy here 
} 

Das Verhalten Verlassen dieser Parameter null und initialisiert, was ich tat das gleiche ist.

Es könnte einige Fehler in Ihrem Code enthalten. Es gibt zwei AccessConditions, eine für das Quell-Blob, eine andere für das Ziel-Blob. Hier ist eine Beispielmethode. Wenn Sie den Wert der Ziel-AccessCondition auf null ändern. Das Ziel-Blob überschreibt das Quell-Blob.

public static async void CopyBlob(Uri sourceBlob, CloudBlockBlob targetBlob, System.Threading.CancellationToken cancellationToken) 
{ 
    string text = await targetBlob.StartCopyAsync(
     sourceBlob, 
     //Source blob access condition, it will check whether the source is exist. If source doesn't exist, a exeception will throw. 
     Access​Condition.GenerateIfExistsCondition(), 
     //Target blob access condition, it will check whether the target is exist. If target blob exist, 409 error will occur. 
     AccessCondition.GenerateIfNotExistsCondition(), 
     null, 
     null, 
     cancellationToken); 
} 

Hier sind meine Testcode. Wenn der Quellcontainer privat ist, müssen Sie dem ersten Parameter der StartCopyAsync-Methode einen Quellblob-URI mit SAS bereitstellen.

Uri sourceUri = new Uri("Put your source blob uri with SAS"); 

string targetConnectionString = "target blob connectionString "; 
CloudStorageAccount targetStorageAccount = CloudStorageAccount.Parse(targetConnectionString); 
// Create the blob client. 
CloudBlobClient targetBlobClient = targetStorageAccount.CreateCloudBlobClient(); 

CloudBlobContainer targetContainer = targetBlobClient.GetContainerReference("mycontainer2"); 
CloudBlockBlob targetBlob = targetContainer.GetBlockBlobReference("text1.txt"); 

CopyBblob(sourceUri, targetBlob, new System.Threading.CancellationToken()); 
+0

Ok, das macht Sinn, aber ich bin nicht sicher, was ist der Punkt der Zugriffsbedingung dann? Das Verhalten, diesen Parameter 'null' zu lassen und zu initialisieren, ist dasselbe. –

+0

Ich habe meine Antwort basierend auf Ihrem Kommentar aktualisiert. – Amor

+0

Irgendwelche Updates? Hast du meine Vorschläge ausprobiert? Wenn Sie weitere Fragen haben, wenden Sie sich bitte an mich. – Amor

Verwandte Themen