Ejemplo n.º 1
0
        public async Task ContainerSample()
        {
            // Instantiate a new BlobServiceClient using a connection string.
            BlobServiceClient blobServiceClient = new BlobServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString);

            // Instantiate a new BlobContainerClient
            BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient($"mycontainer-{Guid.NewGuid()}");

            try
            {
                // Create new Container in the Service
                await blobContainerClient.CreateAsync();

                // List All Containers
                await foreach (var container in blobServiceClient.GetContainersAsync())
                {
                    Assert.IsNotNull(container.Value.Name);
                }

                // List Containers By Page
                await foreach (var page in blobServiceClient.GetContainersAsync().ByPage())
                {
                    Assert.NotZero(page.Values.Length);
                }
            }
            finally
            {
                // Delete Container in the Service
                await blobContainerClient.DeleteAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task CollectGarbage_Explicit()
        {
            // Get all of our configs
            var configs = new List <TenantConfiguration>();

            try { configs.Add(TestConfigurations.DefaultTargetTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultSecondaryTargetTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetPremiumBlobTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetPreviewBlobTenant); } catch (InconclusiveException) { }
            try { configs.Add(TestConfigurations.DefaultTargetOAuthTenant); } catch (InconclusiveException) { }
            foreach (TenantConfiguration config in configs)
            {
                // Blobs
                var blobs = new BlobServiceClient(config.ConnectionString);
                await foreach (Response <ContainerItem> container in blobs.GetContainersAsync())
                {
                    try
                    {
                        await blobs.DeleteBlobContainerAsync(container.Value.Name);
                    }
                    catch (StorageRequestFailedException ex) when(ex.ErrorCode == BlobErrorCode.LeaseIdMissing)
                    {
                        // Break any lingering leases
                        await blobs.GetBlobContainerClient(container.Value.Name).GetLeaseClient().BreakAsync();
                    }
                    catch (StorageRequestFailedException ex) when(ex.ErrorCode == BlobErrorCode.ContainerBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }

                // Queues
                var queues = new QueueServiceClient(config.ConnectionString);
                await foreach (Response <QueueItem> queue in queues.GetQueuesAsync())
                {
                    try
                    {
                        await queues.DeleteQueueAsync(queue.Value.Name);
                    }
                    catch (StorageRequestFailedException ex) when(ex.ErrorCode == QueueErrorCode.QueueBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }

                // Files
                var files = new FileServiceClient(config.ConnectionString);
                await foreach (Response <ShareItem> share in files.GetSharesAsync())
                {
                    try
                    {
                        await files.DeleteShareAsync(share.Value.Name);
                    }
                    catch (StorageRequestFailedException ex) when(ex.ErrorCode == FileErrorCode.ShareBeingDeleted)
                    {
                        // Ignore anything already being deleted
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public static async Task Main()
        {
            BlobClientOptions options = new BlobClientOptions();

            options.Retry.MaxRetries             = 10;
            options.Retry.Delay                  = TimeSpan.FromSeconds(3);
            options.Diagnostics.IsLoggingEnabled = true;


            options.AddPolicy(new SimpleTracingPolicy(), HttpPipelinePosition.PerCall);


            BlobServiceClient serviceClient = new BlobServiceClient(BlobServiceUri, new DefaultAzureCredential(), options);

            await foreach (ContainerItem container in serviceClient.GetContainersAsync())
            {
                BlobContainerClient containerClient = serviceClient.GetBlobContainerClient(container.Name);
                await containerClient.DeleteAsync();
            }
        }