Azure blob 存储 DownloadTextAsync 与 BlobRequestOptions
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/en-us/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);
}
您可以看到它们明确指定了 null
编码、accessCondition 和 operationContext 的值。如果需要,您可以进一步向下钻取,但我认为您可以使用 null
作为不需要的参数。
使用 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/en-us/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);
}
您可以看到它们明确指定了 null
编码、accessCondition 和 operationContext 的值。如果需要,您可以进一步向下钻取,但我认为您可以使用 null
作为不需要的参数。