internal int?GetDiskSizeGbFromBlobUri(string sBlobUri) { if (String.IsNullOrEmpty(sBlobUri)) { return(null); } var blobMatch = Regex.Match(sBlobUri, "https?://(\\S*?)\\..*?/(.*)"); if (!blobMatch.Success) { WriteError("Blob URI of disk does not match known pattern {0}", sBlobUri); throw new ArgumentException("Blob URI of disk does not match known pattern"); } var accountName = blobMatch.Groups[1].Value; BlobUri blobUri; if (BlobUri.TryParseUri(new Uri(sBlobUri), out blobUri)) { try { var account = this.GetStorageAccountFromCache(accountName); var resGroupName = new ResourceIdentifier(account.Id).ResourceGroupName; StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(resGroupName, this._StorageClient, this._Subscription); StorageCredentials sc = storageCredentialsFactory.Create(blobUri); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(sc, this._StorageEndpoint, true); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobUri.BlobContainerName); var cloudBlob = blobContainer.GetPageBlobReference(blobUri.BlobName); var sasToken = cloudBlob.GetSharedAccessSignature( new SharedAccessBlobPolicy() { SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24.0), Permissions = SharedAccessBlobPermissions.Read }); cloudBlob.FetchAttributesAsync() .ConfigureAwait(false).GetAwaiter().GetResult(); return((int?)(cloudBlob.Properties.Length / (1024 * 1024 * 1024))); } catch (Exception) { this.WriteWarning("Could not determine OS Disk size."); } } return(null); }
/// <summary> /// find the snapshot with the tags /// </summary> /// <param name="blobUris"></param> /// <param name="snapshotTag"></param> /// <param name="taskId"></param> /// <param name="storageCredentialsFactory"></param> /// <returns></returns> public List <CloudPageBlob> FindSnapshot(List <string> blobUris, Dictionary <string, string> snapshotQuery, StorageCredentialsFactory storageCredentialsFactory) { List <CloudPageBlob> snapshots = new List <CloudPageBlob>(); for (int i = 0; i < blobUris.Count; i++) { BlobUri blobUri = null; if (BlobUri.TryParseUri(new Uri(blobUris[i]), out blobUri)) { StorageCredentials sc = storageCredentialsFactory.Create(blobUri); CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(sc, true); CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference(blobUri.BlobContainerName); IEnumerable <IListBlobItem> blobs = blobContainer.ListBlobs(null, true, BlobListingDetails.All); foreach (var blob in blobs) { if (blob is CloudPageBlob) { CloudPageBlob pageBlob = blob as CloudPageBlob; if (pageBlob.IsSnapshot && pageBlob.Name == blobUri.BlobName) { bool allMatch = true; foreach (string keyToQuey in snapshotQuery.Keys) { if (!pageBlob.Metadata.Keys.Contains(keyToQuey)) { allMatch = false; } else if (!string.Equals(pageBlob.Metadata[keyToQuey], snapshotQuery[keyToQuey])) { allMatch = false; } } if (allMatch) { snapshots.Add(pageBlob); } } } } } } return(snapshots); }