下面是我为容器中的所有blob创建快照的方法:
public static EmsStorageSnapshot CreateSnapshot(EmsStorageAccount storageAccount, EmsStorageContainer nContainer)
{
try
{
DateTime snapDate = DateTime.Now;
EmsStorageSnapshot snapshot = new EmsStorageSnapshot() { CreatedAt = snapDate, SnapshotReference = snapDate.ToString() };
// var metadata = new Dictionary<string, string> { { "snapshotIssued", snapDate.ToString() } };
CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials(storageAccount.StorageAccountName, storageAccount.StorageAccountKey), false);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference(nContainer.ContainerName);
List<CloudBlockBlob> blobs = container.ListBlobs().OfType<CloudBlockBlob>().ToList();
foreach (var blob in blobs)
{
try
{
if (!blob.IsSnapshot)
{
var snapBlob = blob.CreateSnapshot();
snapBlob.Metadata.Add("snapshotIssued", snapDate.ToString());
snapBlob.SetMetadata();
}
}
catch (StorageException ex)
{
Console.WriteLine("Failed to create a snapshot of blob {0}.", blob.Name);
Logger.Log("Failed to create a snapshot of blob '" + blob.Name + "': " + ex.ToString(), VerboseLevel.Error);
}
}
return snapshot;
}
catch (Exception ex)
{
Logger.Log(ex.ToString(), VerboseLevel.Error);
}
return null;
}
在该行(var snapBlob = blob.CreateSnapshot();)我收到以下错误:
Microsoft.WindowsAzure.Storage.StorageException: The argument is out of range. Value passed:
Parameter name: snapshot ---> System.ArgumentOutOfRangeException: The argument is out of range. Value passed:
Parameter name: snapshot
at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.ArgumentOutOfRange(String paramName, Object value) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Core\Util\CommonUtility.cs:line 168
at Microsoft.WindowsAzure.Storage.Core.Util.NavigationHelper.ParseSnapshotTime(String snapshot) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Core\Util\NavigationHelper.cs:line 665
at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.<>c__DisplayClass69.<CreateSnapshotImpl>b__68(RESTCommand`1 cmd, HttpWebResponse resp, Exception ex, OperationContext ctx) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlockBlob.cs:line 3006
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 711
我做错了什么或这是一个错误?
Microsoft.WindowsAzure.Storage.dll版本= 8.7.0.0
答案 0 :(得分:0)
我读了源代码,发现在解析快照时间时抛出了异常。你可以参考这个article。
internal static DateTimeOffset ParseSnapshotTime(string snapshot)
{
DateTimeOffset snapshotTime;
if (!DateTimeOffset.TryParse(snapshot, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out snapshotTime))
{
CommonUtility.ArgumentOutOfRange("snapshot", snapshot);
}
return snapshotTime;
}
我在当地测试,效果很好。因此,我建议您尝试在本地计算机上测试代码,并使用fiddler检查x-ms快照。
另外,据我所知,您无法在创建快照后设置元数据。因此,您可以参考以下代码首先设置元数据并创建快照。
Dictionary<string, string> metadata = new Dictionary<string, string>();
metadata.Add("snapshotIssued", snapDate.ToString());
blob.CreateSnapshot(metadata);