//-------------------------------------------------
        // Recover a specific blob snapshot
        //-------------------------------------------------

        private static async Task CopySnapshotToBaseBlob()
        {
            var connectionString = Constants.connectionString;
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            BlobContainerClient container =
                blobServiceClient.GetBlobContainerClient(Constants.containerName);

            // Get a specific blob to restore.
            BlobClient blockBlob = container.GetBlobClient("blob1.txt");

            // <Snippet_RecoverSpecificBlobSnapshot>
            // Restore the deleted blob.
            await blockBlob.UndeleteAsync();

            // List blobs in this container that match prefix.
            // Include snapshots in listing.
            Pageable <BlobItem> blobItems = container.GetBlobs
                                                (BlobTraits.None, BlobStates.Snapshots, prefix: blockBlob.Name);

            // Get the URI for the most recent snapshot.
            BlobUriBuilder blobSnapshotUri = new BlobUriBuilder(blockBlob.Uri)
            {
                Snapshot = blobItems
                           .OrderByDescending(snapshot => snapshot.Snapshot)
                           .ElementAtOrDefault(1)?.Snapshot
            };

            // Restore the most recent snapshot by copying it to the blob.
            blockBlob.StartCopyFromUri(blobSnapshotUri.ToUri());
            // </Snippet_RecoverSpecificBlobSnapshot>
        }
        //-------------------------------------------------
        // Restore a previous version
        //-------------------------------------------------

        private static void CopyVersionToBaseBlob(string blobName)
        {
            var connectionString = Constants.connectionString;
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

            BlobContainerClient container =
                blobServiceClient.GetBlobContainerClient(Constants.containerName);

            // Get a specific blob to restore.
            BlobClient blockBlob = container.GetBlobClient(blobName);

            // <Snippet_RestorePreviousVersion>
            // List blobs in this container that match prefix.
            // Include versions in listing.
            Pageable <BlobItem> blobItems = container.GetBlobs
                                                (BlobTraits.None, BlobStates.Version, prefix: blockBlob.Name);

            // Get the URI for the most recent version.
            BlobUriBuilder blobVersionUri = new BlobUriBuilder(blockBlob.Uri)
            {
                VersionId = blobItems
                            .OrderByDescending(version => version.VersionId)
                            .ElementAtOrDefault(1)?.VersionId
            };

            // Restore the most recently generated version by copying it to the base blob.
            blockBlob.StartCopyFromUri(blobVersionUri.ToUri());
            // </Snippet_RestorePreviousVersion>
        }
Ejemplo n.º 3
0
        private void ExecuteCore()
        {
            var containerClient        = new BlobContainerClient(new Uri(BlobContainerSasUrl));
            Pageable <BlobItem> blobs  = containerClient.GetBlobs(prefix: RepoName + "/");
            BlobItem            newest = blobs.OrderByDescending(b => b.Name).FirstOrDefault();

            if (newest == null)
            {
                Log.LogError($"Unable to find stage1 output for repo {RepoName}");
                return;
            }

            BlobClient blobClient  = containerClient.GetBlobClient(newest.Name);
            var        loggableUrl = new UriBuilder(blobClient.Uri)
            {
                Fragment = "", Query = ""
            };

            Log.LogMessage($"Extracting {loggableUrl} to {OutputDirectory}");
            using Stream fileStream = blobClient.OpenRead();
            using var input         = new GZipInputStream(fileStream);
            using var archive       = TarArchive.CreateInputTarArchive(input, Encoding.UTF8);
            archive.ExtractContents(OutputDirectory, false);
        }