Nizar Blond
Nizar Blond

Reputation: 1866

Azure Storage - CloudBlob.UploadFile - how do I verify upload succeeded?

I am writing automatic tests and I need to check if the Upload succeeded. How do I do that? How come there is no FileExist method?

Upvotes: 1

Views: 2735

Answers (2)

David
David

Reputation: 1520

I recommend checking file size for case that forexample connection to server was closed before completing data transfer.

public bool WriteDocumentStream(String documentId, Stream dataStream, long length)
{
  CloudBlobContainer container = BlobClient.GetContainerReference(ContainerName);
  CloudBlob blob = container.GetBlobReference(documentId);
  blob.UploadFromStream(dataStream);

  blob.FetchAttributes();
  bool success = blob.Properties.Length == length;
  if (!success)
    blob.Delete();

  return success;
}

//length should be like this: context.Request.ContentLength 
//(if request have ContentLength defined at headers)

Upvotes: 1

Serdar Ozler
Serdar Ozler

Reputation: 3802

Exists method for blobs have been added in the new Storage Client Library 2.0 release. Since you are using an older library version, you can instead use FetchAttributes. It will throw an exception if the blob does not exist.

On the other hand, as Magnus also mentioned, Upload* methods throw an exception if they do not succeed.

Upvotes: 2

Related Questions