private Metadata GetActualMetadata(BlobItem item) { // Pass all custom metadata through the converter var convertedMeta = item.Metadata.ToDictionary(k => k.Key, k => AzureStoreMetadataEncoder.DecodeMetadata(k.Value)); var metadata = new Metadata(convertedMeta); metadata.StoredLastModified = item.Properties.LastModified.Value.UtcDateTime; if (!metadata.LastModified.HasValue) { metadata.LastModified = metadata.StoredLastModified; } metadata.StoredContentLength = item.Properties.ContentLength; metadata.StoredContentType = item.Properties.ContentType; metadata.ETag = item.Properties.ETag.ToString(); // Remove the snapshot key at this point if we have it if (metadata.ContainsKey(InternalSnapshotKey)) { metadata.Remove(InternalSnapshotKey); } // Remove the store key as well... if (metadata.ContainsKey(StoreVersionKey)) { metadata.Remove(StoreVersionKey); } if (_enableSnapshots && !string.IsNullOrEmpty(item.Snapshot)) { metadata.Snapshot = item.Snapshot; } return(metadata); }
private async ValueTask <Metadata> GetActualMetadata(BlockBlobClient blob, BlobProperties props, string snapshot) { if (props == null) { return(null); } // Pass all custom metadata through the converter var convertedMeta = props.Metadata.ToDictionary(k => k.Key, k => AzureStoreMetadataEncoder.DecodeMetadata(k.Value)); var metadata = new Metadata(convertedMeta); metadata.StoredLastModified = props.LastModified.UtcDateTime; if (!metadata.LastModified.HasValue) { metadata.LastModified = metadata.StoredLastModified; } metadata.StoredContentLength = props.ContentLength; metadata.StoredContentType = props.ContentType; metadata.ETag = props.ETag.ToString(); // Remove the snapshot key at this point if we have it if (metadata.ContainsKey(InternalSnapshotKey)) { metadata.Remove(InternalSnapshotKey); } // Remove the store key as well... if (metadata.ContainsKey(StoreVersionKey)) { metadata.Remove(StoreVersionKey); } if (_enableSnapshots) { if (!string.IsNullOrEmpty(snapshot)) { metadata.Snapshot = snapshot; } else if (props.Metadata.ContainsKey(InternalSnapshotKey)) { // Try and use our save snapshot instead of making more calls... var date = props.Metadata[InternalSnapshotKey]; if (long.TryParse(date, out var ticks)) { // This is in old format, convert back to snapshot format date date = new DateTime(ticks, DateTimeKind.Utc).ToString("o", CultureInfo.InvariantCulture); } metadata.Snapshot = date; } else { // Try and find last snapshot // Unfortunately we need to make a request since this information isn't on the actual blob that we are working with... var container = _blobStorage.GetBlobContainerClient(blob.BlobContainerName); var snapBlob = await ListBlobs(container, blob.Name, BlobTraits.None, BlobStates.Snapshots) .Where(b => !string.IsNullOrEmpty(b.Snapshot) && b.Name == blob.Name) .AggregateAsync((BlobItem)null, (a, b) => a == null || b == null ? (a ?? b) : (ParseSnapshotDate(b.Snapshot) > ParseSnapshotDate(b.Snapshot) ? a : b)); metadata.Snapshot = snapBlob?.Snapshot; } } return(metadata); }