简体   繁体   中英

Azure blob storage DownloadTextAsync with BlobRequestOptions

Using azure blob storage and the Azure SDK, I currently am downloading a string like so:

var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blockBlob = container.GetBlockBlobReference(blobUid);
var text = await blockBlob.DownloadTextAsync();

I want to pass in a blobRequestOptions to set a custom retry policy so it would look like this:

var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blockBlob = container.GetBlockBlobReference(blobUid);
var blobRequestOptions = new BlobRequestOptions()
{
     RetryPolicy = new ExponentialRetry(TimeSpan.FromSeconds(5), 3)
};

var text await blockBlob.DownloadTextAsync(encoding, accessCondition, blobRequestOptions, operationContext);

My problem is I am not sure what to pass in for encoding, accessCondition or operationContext. I have looked at the documentation ( https://msdn.microsoft.com/en-us/library/dn434829.aspx ) and did a bunch of searching but I cannot figure out what to pass in.

Encoding: My data is just json in Us-En so I think I would be fine with ACSII or UTF-8 but I cannot find if azure has a default I should be using.

AccessCondition and OperationContext: No idea what I should be passing in.

Or maybe there is a better way to do what I am trying to do without using the overloaded DownloadTextAsync.

When I have a problem like this and I'm lucky enough that the SDK is open-source, I usually check out the source code. Source code for the Azure Storage SDK is here:

https://github.com/Azure/azure-storage-net

So this is the method that you are calling:

public virtual Task<string> DownloadTextAsync(CancellationToken cancellationToken)
{
   return AsyncExtensions.TaskFromApm(this.BeginDownloadText, this.EndDownloadText, cancellationToken);
}

This just basically converts the BeginDownloadText method from the old APL model to the new TPL-based call. If you check out BeginDownloadText:

public virtual ICancellableAsyncResult BeginDownloadText(AsyncCallback callback, object state)
{
    return this.BeginDownloadText(null /* encoding */, null /* accessCondition */, null /* options */, null /* operationContext */, callback, state);
}

You can see that they are explicitly specifying null values for the encoding, the accessCondition and the operationContext. You can drill even further down if you want, but I'd say you're OK using null for the parameters you don't need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM