private void EnsureContainerExists() { if (!BlobContainerClient.Exists()) { BlobContainerInfo response = BlobContainerClient.Create(); } }
public async Task <BlobContainerInfo> CloneContainer(string sourceContainerName, string targetContainerName) { try { // create target container BlobContainerClient targetContainerClient = new BlobContainerClient(connectionString, targetContainerName); BlobContainerInfo targetContainer = await targetContainerClient.CreateAsync(); // get source container BlobContainerClient sourceContainerClient = blobServiceClient.GetBlobContainerClient(sourceContainerName); // blobs from source container await foreach (BlobItem blob in sourceContainerClient.GetBlobsAsync()) { // create a blob client for the source blob BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name); // Ensure that the source blob exists. if (await sourceBlob.ExistsAsync()) { // Lease the source blob for the copy operation // to prevent another client from modifying it. BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient(); // Specifying -1 for the lease interval creates an infinite lease. await lease.AcquireAsync(TimeSpan.FromSeconds(60)); // Get a BlobClient representing the destination blob with a unique name. BlobClient destBlob = targetContainerClient.GetBlobClient(sourceBlob.Name); // Start the copy operation. await destBlob.StartCopyFromUriAsync(sourceBlob.Uri); // Update the source blob's properties. BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync(); if (sourceProperties.LeaseState == LeaseState.Leased) { // Break the lease on the source blob. await lease.BreakAsync(); } } } return(targetContainer); } catch (RequestFailedException e) { Console.WriteLine("HTTP error code {0}: {1}", e.Status, e.ErrorCode); Console.WriteLine(e.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public static ContainerViewData Create(BlobContainerInfo container) { var res = new ContainerViewData() { Name = container.Name, Uri = container.Uri, }; return(res); }
public async Task CreateContainerTest() { string containerName = _mockupService.Randomize(AppConstant.SAMPLE_CONTAINER_NAME); try { BlobContainerInfo response = await _containerService.CreateContainer(containerName, PublicAccessType.BlobContainer); Assert.IsTrue(response != null); } finally { //// delete container await _containerService.DeleteContainerAsync(containerName); } }
public async Task <BlobContainerInfo> CreateContainer(string containerName, PublicAccessType publicAccessType = PublicAccessType.None) { try { BlobContainerClient container = new BlobContainerClient(connectionString, containerName); BlobContainerInfo containerInfo = await container.CreateAsync(publicAccessType); return(containerInfo); } catch (RequestFailedException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
public BlobContainerInfo GetBlobContainerInfo(string containerName) { if (string.IsNullOrEmpty(containerName) || _cloudBlobClient == null) { return(null); } var container = _cloudBlobClient.GetContainerReference(containerName.ToLower()); var containerInfo = new BlobContainerInfo() { FileCount = 0, DirectoryCount = 0, TotalBytes = 0 }; foreach (var blobItem in container.ListBlobs(null, true, BlobListingDetails.None)) { if (blobItem is CloudBlockBlob) { CloudBlockBlob blob = blobItem as CloudBlockBlob; containerInfo.FileCount++; containerInfo.TotalBytes += blob.Properties.Length; } else if (blobItem is CloudPageBlob) { CloudPageBlob pageBlob = blobItem as CloudPageBlob; containerInfo.FileCount++; containerInfo.TotalBytes += pageBlob.Properties.Length; } else if (blobItem is CloudBlobDirectory) { CloudBlobDirectory directory = blobItem as CloudBlobDirectory; containerInfo.DirectoryCount++; } } return(containerInfo); }
public PaginatedList <BlobContainerInfo> GetBlobContainers(int start = 0, int count = int.MaxValue) { var res = BlobContainerInfo.Create(new BlobHelper().GetBlobContainers()); return(new PaginatedList <BlobContainerInfo>(res, start / count, count)); }