Ejemplo n.º 1
0
        private async Task <string> TryCreateSnapshot(BlobClient blobClient, string versionKey)
        {
            var version = 1;

            try
            {
                var propResponse = await blobClient.GetPropertiesAsync();

                if (propResponse != null)
                {
                    var metadata = propResponse.Value.Metadata;
                    if (metadata.TryGetValue(versionKey, out string prevVersion))
                    {
                        version = int.Parse(prevVersion) + 1;
                    }
                    else
                    {
                        throw new InvalidOperationException("version not found in blob metadata");
                    }
                    await blobClient.CreateSnapshotAsync(metadata);
                }
            }
            catch (RequestFailedException ex)
            {
                if (ex.Status != 404)
                {
                    throw ex;
                }
            }
            return(Convert.ToString(version));
        }
Ejemplo n.º 2
0
        public async Task <BlobSnapshotInfo> CreateBlobSnapshot(string blobName, string containerName, CancellationToken cancellationToken = default)
        {
            BlobClient client =
                BlobStorageManager.GetBlobClient(
                    azureStorageOptions: _azureStorageOptions,
                    containerName: containerName,
                    blobName: blobName);

            try
            {
                return(await client.CreateSnapshotAsync(cancellationToken : cancellationToken));
            }
            catch (Exception ex)
            {
                throw new BlobServiceException(
                          message: ex.Message,
                          blobName: blobName,
                          containerName: containerName,
                          methodName: nameof(CreateBlobSnapshot),
                          innerException: ex);
            }
        }
        private static async Task SoftDeleteTest()
        {
            // Retrieve a CloudBlobClient object and enable soft delete
            BlobServiceClient blobClient = GetCloudBlobClient();

            try
            {
                BlobServiceProperties serviceProperties = blobClient.GetProperties();
                serviceProperties.DeleteRetentionPolicy.Enabled = true;
                serviceProperties.DeleteRetentionPolicy.Days    = RetentionDays;
                blobClient.SetProperties(serviceProperties);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Error returned from the service: {0}", ex.Message);
                throw;
            }

            // Create a container
            BlobContainerClient container = blobClient.GetBlobContainerClient("softdelete-" + System.Guid.NewGuid().ToString());

            try
            {
                await container.CreateIfNotExistsAsync();
            }
            catch (RequestFailedException)
            {
                Console.WriteLine("If you are using the storage emulator, please make sure you have started it. Press the Windows key and type Azure Storage to select and run it from the list of applications - then restart the sample.");
                Console.ReadLine();
                throw;
            }

            try
            {
                // Upload
                Console.WriteLine("\nUpload:");
                BlobClient blockBlob = container.GetBlobClient("HelloWorld");
                await blockBlob.UploadAsync(ImageToUpload, overwrite : true);

                PrintBlobsInContainer(container, BlobTraits.All);

                // Overwrite
                Console.WriteLine("\nOverwrite:");
                await blockBlob.UploadAsync(TextToUpload, overwrite : true);

                PrintBlobsInContainer(container, BlobTraits.All);

                // Snapshot
                Console.WriteLine("\nSnapshot:");
                await blockBlob.CreateSnapshotAsync();

                PrintBlobsInContainer(container, BlobTraits.All);

                // Delete (including snapshots)
                Console.WriteLine("\nDelete (including snapshots):");
                blockBlob.Delete(DeleteSnapshotsOption.IncludeSnapshots);
                PrintBlobsInContainer(container, BlobTraits.All);

                // Undelete
                Console.WriteLine("\nUndelete:");
                await blockBlob.UndeleteAsync();

                PrintBlobsInContainer(container, BlobTraits.All);

                // Recover
                Console.WriteLine("\nCopy the most recent snapshot over the base blob:");
                blockBlob.StartCopyFromUri(blockBlob.Uri);
                PrintBlobsInContainer(container, BlobTraits.All);
            }
            catch (RequestFailedException ex)
            {
                Console.WriteLine("Error returned from the service: {0}", ex.Message);
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("\nDone.\n\nEnter 'd' to cleanup resources. Doing so will also turn off the soft delete feature. Enter any other key to leave the container intact.");
            String cleanup = Console.ReadLine();

            if (cleanup == "d")
            {
                try
                {
                    // Delete the container
                    await container.DeleteIfExistsAsync();

                    Console.WriteLine("\nContainer deleted.");

                    // Turn off soft delete
                    BlobServiceProperties serviceProperties = blobClient.GetProperties();
                    serviceProperties.DeleteRetentionPolicy.Enabled = false;
                    blobClient.SetProperties(serviceProperties);
                }
                catch (RequestFailedException ex)
                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                    throw;
                }
            }
        }