![](/img/trans.png)
[英]Azure Storage, using BlobRequestOptions without CloudBlobClient
[英]Azure blob storage DownloadTextAsync with BlobRequestOptions
使用azure blob存储和Azure SDK,我当前正在下载一个字符串,如下所示:
var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var blockBlob = container.GetBlockBlobReference(blobUid);
var text = await blockBlob.DownloadTextAsync();
我想传入blobRequestOptions来设置自定义重试策略,因此它看起来像这样:
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);
我的问题是我不确定要为编码,accessCondition或operationContext传入什么。 我查看了文档( https://msdn.microsoft.com/zh-cn/library/dn434829.aspx ),并进行了大量搜索,但我不知道要输入什么。
编码:在Us-En中,我的数据只是json,所以我认为使用ACSII或UTF-8会很好,但是我无法确定azure是否应使用默认值。
AccessCondition和OperationContext:不知道我应该传递什么。
或者,也许有一种更好的方法可以执行我想做的事情,而无需使用重载的DownloadTextAsync。
当我遇到这样的问题并且很幸运该SDK是开源的时,我通常会检查源代码。 Azure存储SDK的源代码在这里:
https://github.com/Azure/azure-storage-net
因此,这是您要调用的方法:
public virtual Task<string> DownloadTextAsync(CancellationToken cancellationToken)
{
return AsyncExtensions.TaskFromApm(this.BeginDownloadText, this.EndDownloadText, cancellationToken);
}
这基本上只是将BeginDownloadText
方法从旧的APL模型转换为新的基于TPL的调用。 如果您签出BeginDownloadText:
public virtual ICancellableAsyncResult BeginDownloadText(AsyncCallback callback, object state)
{
return this.BeginDownloadText(null /* encoding */, null /* accessCondition */, null /* options */, null /* operationContext */, callback, state);
}
您可以看到它们为编码,accessCondition和operationContext明确指定了null
值。 如果需要,您甚至可以进一步进行深入研究,但是我说您可以使用null
作为不需要的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.