Esempio n. 1
0
        internal async Task DeleteLargeMessageBlobs(string instanceId, AzureStorageOrchestrationServiceStats stats)
        {
            var blobForDeletionTaskList = new List <Task>();

            if (!await this.cloudBlobContainer.ExistsAsync())
            {
                return;
            }

            CloudBlobDirectory    instanceDirectory     = this.cloudBlobContainer.GetDirectoryReference(instanceId);
            BlobContinuationToken blobContinuationToken = null;

            while (true)
            {
                BlobResultSegment segment = await instanceDirectory.ListBlobsSegmentedAsync(blobContinuationToken);

                stats.StorageRequests.Increment();
                foreach (IListBlobItem blobListItem in segment.Results)
                {
                    var            cloudBlockBlob = blobListItem as CloudBlockBlob;
                    CloudBlockBlob blob           = this.cloudBlobContainer.GetBlockBlobReference(cloudBlockBlob?.Name);
                    blobForDeletionTaskList.Add(blob.DeleteIfExistsAsync());
                }

                await Task.WhenAll(blobForDeletionTaskList);

                stats.StorageRequests.Increment(blobForDeletionTaskList.Count);
                if (blobContinuationToken == null)
                {
                    break;
                }
            }
        }
Esempio n. 2
0
        void CopyDirectory(CloudBlobDirectory dir, string destinationKey, bool deleteSource = false)
        {
            var children = new List <IListBlobItem>();
            BlobContinuationToken continuationToken = null;

            do
            {
                BlobResultSegment segmentResult = dir.ListBlobsSegmented(continuationToken);
                continuationToken = segmentResult.ContinuationToken;
                children.AddRange(segmentResult.Results);
            } while(continuationToken != null);

            foreach (IListBlobItem blob in children)
            {
                string childCopyName = GetFileItemName(blob);
                string childCopyKey  = $"{destinationKey}{childCopyName}";
                if (blob is CloudBlob)
                {
                    CopyFile((CloudBlob)blob, childCopyKey, deleteSource);
                }
                else if (blob is CloudBlobDirectory)
                {
                    CopyDirectory((CloudBlobDirectory)blob, childCopyKey, deleteSource);
                }
                else
                {
                    throw new Exception("Unsupported blob type");
                }
            }
        }
        public async Task <List <Receipt> > ListBlobsSegmentedInFlatListing(CloudBlobContainer container)
        {
            int i = 0;
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            var list = new List <Receipt>();

            do
            {
                resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);

                if (resultSegment.Results.Count <IListBlobItem>() > 0)
                {
                    Console.WriteLine("Page {0}:", ++i);
                }
                foreach (var blobItem in resultSegment.Results)
                {
                    CloudBlockBlob thing = (CloudBlockBlob)blobItem;
                    list.Add(new Receipt()
                    {
                        Name = thing.Name, Link = thing.StorageUri.PrimaryUri.ToString()
                    });
                }

                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);

            return(list);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns a <see cref="System.Threading.Tasks.Task"/> instance for the copy blobs operation from <paramref name="sourceContainer"/> to <paramref name="destinationContainer"/>.
        /// </summary>
        /// <param name="sourceContainer">The <see cref="Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer"/> instance that contains the blobs to be copied into <paramref name="destinationContainer"/>.</param>
        /// <param name="destinationContainer">The <see cref="Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer"/> instance where the blobs from <paramref name="sourceContainer"/> will be copied.</param>
        /// <param name="options">The <see cref="Microsoft.WindowsAzure.Storage.Blob.BlobRequestOptions"/>.</param>
        /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> instance used for cancellation.</param>
        /// <returns>A <see cref="System.Threading.Tasks.Task"/> instance for the copy blobs operation from <paramref name="sourceContainer"/> to <paramref name="destinationContainer"/>.</returns>
        public static async Task CopyBlobsAsync(CloudBlobContainer sourceContainer, CloudBlobContainer destinationContainer, BlobRequestOptions options, CancellationToken cancellationToken)
        {
            BlobContinuationToken continuationToken = null;

            do
            {
                BlobResultSegment resultSegment = await sourceContainer.ListBlobsSegmentedAsync(null, true, BlobListingDetails.None, MaxNumberOfConcurrentCopyFromBlobOperations, continuationToken, options, null, cancellationToken).ConfigureAwait(false);

                IEnumerable <Task> copyTasks = resultSegment
                                               .Results
                                               .Cast <CloudBlockBlob>()
                                               .Select(
                    sourceBlob => {
                    CloudBlockBlob destinationBlob = destinationContainer.GetBlockBlobReference(sourceBlob.Name);

                    return(CopyBlobAsync(sourceBlob, destinationBlob, options, cancellationToken));
                });

                await Task.WhenAll(copyTasks).ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);
        }
Esempio n. 5
0
        public async Task <IEnumerable <string> > Get()
        {
            List <string> results = new List <string>();

            StorageCredentials  storageCredentials  = new StorageCredentials(_configuration["blobStorageAccountName"], _configuration["blobStorageAccountKey"]);
            CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentials, true);
            CloudBlobClient     cloudBlobClient     = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container           = cloudBlobClient.GetContainerReference(_configuration["blobStorageContainer"]);

            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            do
            {
                resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.None, 999, continuationToken, null, null);

                foreach (CloudBlob blobItem in resultSegment.Results)
                {
                    results.Add(blobItem.StorageUri.PrimaryUri.ToString());
                }
                continuationToken = resultSegment.ContinuationToken;
            } while (continuationToken != null);

            return(results);
        }
Esempio n. 6
0
        private async Task <List <string> > listPictures(string folderPath)
        {
            List <string>         blobList          = new List <string>();
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            do
            {
                resultSegment = await blobContainer.ListBlobsSegmentedAsync(
                    "",                     //Prefix for listed images
                    true,                   //Flat listing of blobs, not hierarchical
                    BlobListingDetails.All, //List all items in folder
                    10,                     //List 10 items at a time
                    continuationToken,      //Continue till all items listed
                    null,                   //Object for additional options
                    null                    //Object for current operation context
                    );

                foreach (var item in resultSegment.Results)
                {
                    string blobUri = item.StorageUri.PrimaryUri.ToString();
                    blobList.Add(blobUri);
                }
                //Update continuation token
                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);
            return(blobList);
        }
        public async Task <string[]> ListSubDirsAsync(string pathPrefix)
        {
            BlobContinuationToken continuationToken = null;

            List <string> list = new List <string>();

            do
            {
                const int          BatchSize          = 100;
                bool               useFlatBlobListing = false;                   // only subdirs
                BlobListingDetails details            = BlobListingDetails.None; // only  committed blobs, no metadata
                BlobResultSegment  segment            = await _container.ListBlobsSegmentedAsync(
                    pathPrefix, useFlatBlobListing, details, BatchSize, continuationToken, null, null);

                continuationToken = segment.ContinuationToken;

                foreach (IListBlobItem item in segment.Results)
                {
                    var dir = item as CloudBlobDirectory;
                    if (dir == null)
                    {
                        continue;
                    }

                    list.Add(dir.Prefix);
                }
            } while (continuationToken != null);

            return(list.ToArray());
        }
Esempio n. 8
0
        /// <summary>
        /// Filters a collection of entities from a filter definition
        /// </summary>
        /// <param name="prefix">Filter objects by prefix</param>
        /// <returns>Filtered collection of entities</returns>
        public async Task <IEnumerable <Interfaces.BlobInfo> > FindAsync(string prefix)
        {
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;
            var blobs = new List <Interfaces.BlobInfo>();

            do
            {
                resultSegment = await container.ListBlobsSegmentedAsync(prefix, true, BlobListingDetails.Metadata, null, continuationToken, null, null);

                if (resultSegment != null && resultSegment.Results != null)
                {
                    foreach (var cloudBlob in resultSegment.Results)
                    {
                        var block = cloudBlob as CloudBlockBlob;
                        if (block != null)
                        {
                            var blob = new Interfaces.BlobInfo();
                            blob.Id   = block.Name;
                            blob.Size = block.Properties.Length;
                            blob.Uri  = block.Uri;
                            blobs.Add(blob);
                        }
                    }

                    continuationToken = resultSegment.ContinuationToken;
                }
                else
                {
                    continuationToken = null;
                }
            }while (continuationToken != null);

            return(blobs);
        }
Esempio n. 9
0
        public async Task <ActionResult> Edit(string id, Movies model)
        {
            try
            {
                string blobstorageconnection = _configuration.GetValue <string>("blobstorage");

                byte[] dataFiles;
                // Retrieve storage account from connection string.
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
                // Create the blob client.
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                // Retrieve a reference to a container.
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("blobcontainer");

                BlobContainerPermissions permissions = new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                };
                string systemFileName = model.ImageFile.FileName;
                await cloudBlobContainer.SetPermissionsAsync(permissions);

                await using (var target = new MemoryStream())
                {
                    model.ImageFile.CopyTo(target);
                    dataFiles = target.ToArray();
                }
                // This also does not make a service call; it only creates a local object.
                CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(systemFileName);
                await cloudBlockBlob.UploadFromByteArrayAsync(dataFiles, 0, dataFiles.Length);

                BlobResultSegment resultSegment = await cloudBlobContainer.ListBlobsSegmentedAsync(string.Empty,
                                                                                                   true, BlobListingDetails.Metadata, 100, null, null, null);

                List <FileData> fileList = new List <FileData>();


                Movies m = new Movies()
                {
                    ObjectId       = ObjectId.Parse(id),
                    MovieName      = model.MovieName,
                    InitialRelease = model.InitialRelease,
                    Director       = model.Director,
                    Genre          = model.Genre,
                    ImageURL       = resultSegment.Results.LastOrDefault().Uri.ToString()
                };

                FilterDefinition <Movies> filter = Builders <Movies> .Filter.Eq("_id", ObjectId.Parse(id));

                await this._dbContext.Movies.ReplaceOneAsync(filter, m, new UpdateOptions()
                {
                    IsUpsert = true
                });

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }
Esempio n. 10
0
        /// <inheritdoc/>
        public async Task <List <string> > ListAllFilesFromContainer(string container,
                                                                     string containerSubDirectory = null)
        {
            var fileList = new List <string>();

            if (string.IsNullOrWhiteSpace(containerSubDirectory))
            {
                containerSubDirectory = string.Empty;
            }

            CloudBlobClient    cloudBlobClient    = GetCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(container);

            BlobContinuationToken token;

            do
            {
                BlobResultSegment blobResultSegment = await cloudBlobContainer
                                                      .ListBlobsSegmentedAsync(containerSubDirectory, true, BlobListingDetails.None,
                                                                               int.MaxValue, null, null, null);

                token = blobResultSegment.ContinuationToken;
                foreach (IListBlobItem listBlobItem in blobResultSegment.Results)
                {
                    if (!listBlobItem.Uri.AbsoluteUri.EndsWith(containerSubDirectory))
                    {
                        fileList.Add(listBlobItem.Uri.AbsoluteUri);
                    }
                }
            } while (token != null);

            return(fileList);
        }
Esempio n. 11
0
        private async Task CopyKeysFromFileSystemToBlobStorage(BlobStorageSecretsRepository blobStorageSecretsRepository)
        {
            string migrateSentinelPath = Path.Combine(blobStorageSecretsRepository.SecretsSentinelFilePath, "migrate-sentinel.json");

            if (File.Exists(migrateSentinelPath))
            {
                // Migration is already done
                _logger?.LogTrace("Sentinel file is detected.");
                return;
            }

            try
            {
                using (var stream = new FileStream(migrateSentinelPath, FileMode.CreateNew))
                    using (var writer = new StreamWriter(stream))
                    {
                        //write file
                        _logger?.LogTrace("Sentinel file created.");
                    }
            }
            catch (IOException)
            {
                _logger?.LogTrace("Sentinel file is already created by another instance.");
                return;
            }

            string[] files = Directory.GetFiles(blobStorageSecretsRepository.SecretsSentinelFilePath.Replace("Sentinels", string.Empty));
            if (await blobStorageSecretsRepository.BlobContainer.ExistsAsync())
            {
                BlobResultSegment resultSegment = await blobStorageSecretsRepository.BlobContainer.ListBlobsSegmentedAsync(blobStorageSecretsRepository.SecretsBlobPath + "/", null);

                // Check for conflicts
                if (resultSegment.Results.ToArray().Length > 0)
                {
                    _logger?.LogTrace("Conflict detected. Secrets container is not empty.");
                    return;
                }
            }
            else
            {
                await blobStorageSecretsRepository.BlobContainer.CreateIfNotExistsAsync();
            }

            if (files.Length > 0)
            {
                List <Task> copyTasks = new List <Task>();
                foreach (string file in files)
                {
                    string         blobName       = Path.GetFileName(file);
                    CloudBlockBlob cloudBlockBlob = blobStorageSecretsRepository.BlobContainer.GetBlockBlobReference(blobStorageSecretsRepository.SecretsBlobPath + "/" + blobName);

                    string contents = File.ReadAllText(file);
                    Task   copyTask = cloudBlockBlob.UploadTextAsync(contents);
                    copyTasks.Add(copyTask);
                    _logger?.LogTrace("'{0}' was migrated.", cloudBlockBlob.StorageUri.PrimaryUri.AbsoluteUri.ToString());
                }
                await Task.WhenAll(copyTasks);
            }
            _logger?.LogTrace("Finished successfully.");
        }
Esempio n. 12
0
        //public async Task<IActionResult> Create(IFormFile files)
        //{
        //  string systemFileName = files.FileName;
        //  string blobstorageconnection = _configuration.GetValue<string>("blobstorage");
        //  // Retrieve storage account from connection string.
        //  CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
        //  // Create the blob client.
        //  CloudBlobClient blobClient = cloudStorageAccount.CreateCloudBlobClient();
        //  // Retrieve a reference to a container.
        //  CloudBlobContainer container = blobClient.GetContainerReference("filescontainers");
        //  // This also does not make a service call; it only creates a local object.
        //  CloudBlockBlob blockBlob = container.GetBlockBlobReference(systemFileName);
        //  await using (var data = files.OpenReadStream())
        //  {
        //    await blockBlob.UploadFromStreamAsync(data);
        //  }
        //  return View("Create");
        //}


        public async Task <IActionResult> ShowAllBlobs()
        {
            //string blobstorageconnection = _configuration.GetValue<string>("blobstorage");
            string blobstorageconnection = "DefaultEndpointsProtocol=https;AccountName=nilimastorage;AccountKey=xYRTP6eeSYehYkw2YeXgC1y63IyChemxrSG/KSSU0sQU48L8O/8RMsTctLDVcWORbApiQZ2tyEEBpx4jL0YE+Q==;EndpointSuffix=core.windows.net";

            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
            // Create the blob client.
            CloudBlobClient    blobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer container  = blobClient.GetContainerReference("filescontainers");
            CloudBlobDirectory dirb       = container.GetDirectoryReference("filescontainers");


            BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(string.Empty,
                                                                                      true, BlobListingDetails.Metadata, 100, null, null, null);

            List <FileData> fileList = new List <FileData>();

            foreach (var blobItem in resultSegment.Results)
            {
                // A flat listing operation returns only blobs, not virtual directories.
                var blob = (CloudBlob)blobItem;
                fileList.Add(new FileData()
                {
                    FileName   = blob.Name,
                    FileSize   = Math.Round((blob.Properties.Length / 1024f) / 1024f, 2).ToString(),
                    ModifiedOn = DateTime.Parse(blob.Properties.LastModified.ToString()).ToLocalTime().ToString()
                });
            }

            return(View(fileList));
        }
Esempio n. 13
0
        public static async Task <List <Uri> > GetBlobUriList(string containerName)
        {
            if (!isInitialized)
            {
                Init();
            }

            var list = new List <Uri>();

            CloudBlobContainer container = BlobClient.GetContainerReference(containerName);

            BlobContinuationToken token = null;

            do
            {
                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, token, null, null);

                token = resultSegment.ContinuationToken;

                foreach (IListBlobItem item in resultSegment.Results)
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        CloudBlockBlob blob = (CloudBlockBlob)item;
                        if (await blob.ExistsAsync())
                        {
                            list.Add(blob.Uri);
                        }
                    }
                }
            } while (token != null);

            return(list);
        }
Esempio n. 14
0
        public async Task ListBlobs(CloudBlobContainer container)
        {
            BlobContinuationToken token = null;

            do
            {
                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(token);

                token = resultSegment.ContinuationToken;

                foreach (IListBlobItem item in resultSegment.Results)
                {
                    if (item.GetType() == typeof(CloudBlockBlob))
                    {
                        CloudBlockBlob blob = (CloudBlockBlob)item;
                        Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
                    }

                    else if (item.GetType() == typeof(CloudPageBlob))
                    {
                        CloudPageBlob pageBlob = (CloudPageBlob)item;

                        Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
                    }

                    else if (item.GetType() == typeof(CloudBlobDirectory))
                    {
                        CloudBlobDirectory directory = (CloudBlobDirectory)item;

                        Console.WriteLine("Directory: {0}", directory.Uri);
                    }
                }
            } while (token != null);
        }
Esempio n. 15
0
        private IEnumerable <BlobResultSegment> EnumerateDirectoryBlobs(CloudBlobDirectory cloudBlobDirectory)
        {
            Log.Info($"enter {cloudBlobDirectory.Uri}");
            BlobResultSegment     resultSegment = default(BlobResultSegment);
            BlobContinuationToken blobToken     = null;

            while (true)
            {
                resultSegment = _blobChildTasks.TaskFunction((blobresultsegment) =>
                                                             cloudBlobDirectory.ListBlobsSegmentedAsync(
                                                                 false,
                                                                 BlobListingDetails.None,
                                                                 MaxResults,
                                                                 blobToken,
                                                                 null,
                                                                 null).Result as BlobResultSegment).Result as BlobResultSegment;

                blobToken = resultSegment.ContinuationToken;
                yield return(resultSegment);

                if (blobToken == null)
                {
                    break;
                }
            }

            Log.Info($"exit {cloudBlobDirectory.Uri}");
        }
Esempio n. 16
0
        public static async Task <string[]> ListImagesAsync(string Location)
        {
            // Retrieve storage account information from connection string
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a blob client for interacting with the blob service.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // get container for organizing blobs within the storage account.
            Log.LogInformation("Load container: {0}", PRIVATE_CONTAINER_NAME);
            CloudBlobContainer container = blobClient.GetContainerReference(PRIVATE_CONTAINER_NAME);

            BlobContinuationToken token = null;
            BlobListingDetails    blobListingDetails = BlobListingDetails.All;
            ///TODO: add support for pagination
            BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(Location, true, blobListingDetails, 5000, token, null, null);

            token = resultSegment.ContinuationToken;

            string[] ImagesUrls = new string[resultSegment.Results.Count()];

            int i = 0;

            foreach (IListBlobItem blob in resultSegment.Results)
            {
                ImagesUrls[i] = blob.Uri.ToString();
                i++;
            }

            return(ImagesUrls);
        }
Esempio n. 17
0
        private async Task <IStorageBlobResultSegment> ListBlobsSegmentedAsyncCore(Task <BlobResultSegment> sdkTask)
        {
            BlobResultSegment sdkSegment = await sdkTask;

            if (sdkSegment == null)
            {
                return(null);
            }

            IEnumerable <IListBlobItem> sdkResults = sdkSegment.Results;

            List <IStorageListBlobItem> results;

            if (sdkResults != null)
            {
                results = new List <IStorageListBlobItem>();

                foreach (IListBlobItem sdkResult in sdkResults)
                {
                    IStorageListBlobItem result = ToStorageListBlobItem(this, sdkResult);
                    results.Add(result);
                }
            }
            else
            {
                results = null;
            }

            return(new StorageBlobResultSegment(sdkSegment.ContinuationToken, results));
        }
Esempio n. 18
0
        public static async Task <string> ListBlobsSegmentedInFlatListing(CloudBlobContainer container, TraceWriter log)
        {
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            string         lastWriteBlob = string.Empty;
            DateTimeOffset lastWrite     = DateTimeOffset.MinValue;

            //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
            //When the continuation token is null, the last page has been returned and execution can exit the loop.
            do
            {
                //This overload allows control of the page size.
                // You can return all remaining results by passing null for the maxResults parameter, or by calling a different overload.
                resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.Metadata, 10, continuationToken, null, null);

                foreach (var blobItem in resultSegment.Results)
                {
                    DateTimeOffset?blobLastModified = ((CloudBlob)blobItem).Properties.LastModified;

                    if (blobLastModified > lastWrite)
                    {
                        lastWrite     = (DateTimeOffset)blobLastModified;
                        lastWriteBlob = ((CloudBlob)blobItem).Name;
                    }

                    log.Info(blobItem.Uri.ToString());
                }

                //Get the continuation token.
                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);

            return(lastWriteBlob);
        }
Esempio n. 19
0
        private IEnumerable <CloudBlockBlob> GetAllBlobs()
        {
            string prefix = AzureUtils.DirectoryPathToPrefix(this.physicalRootPath);

            BlobContinuationToken blobContinuationToken = null;

            do
            {
                BlobResultSegment results = this.cloudBlobContainer.ListBlobsSegmented(
                    prefix,
                    true,
                    BlobListingDetails.None,
                    null,
                    blobContinuationToken,
                    null,
                    null);

                blobContinuationToken = results.ContinuationToken;
                foreach (IListBlobItem item in results.Results)
                {
                    if (item is CloudBlockBlob cloudBlockBlob)
                    {
                        yield return(cloudBlockBlob);
                    }
                }
            }while (blobContinuationToken != null);
        }
Esempio n. 20
0
        public override async Task <IReadOnlyList <ISleetFile> > GetFiles(ILogger log, CancellationToken token)
        {
            string prefix             = null;
            var    useFlatBlobListing = true;
            var    blobListingDetails = BlobListingDetails.All;
            int?   maxResults         = null;

            // Return all files except feedlock
            var blobs = new List <IListBlobItem>();

            BlobResultSegment result = null;

            do
            {
                result = await _container.ListBlobsSegmentedAsync(prefix, useFlatBlobListing, blobListingDetails, maxResults, result?.ContinuationToken, options : null, operationContext : null);

                blobs.AddRange(result.Results);
            }while (result.ContinuationToken != null);

            // Skip the feed lock, and limit this to the current sub feed.
            return(blobs.Where(e => !e.Uri.AbsoluteUri.EndsWith($"/{AzureFileSystemLock.LockFile}"))
                   .Where(e => string.IsNullOrEmpty(FeedSubPath) || e.Uri.AbsoluteUri.StartsWith(UriUtility.EnsureTrailingSlash(BaseURI).AbsoluteUri, StringComparison.Ordinal))
                   .Select(e => Get(e.Uri))
                   .ToList());
        }
        public async Task <string[]> ListPathsAsync(string pathPrefix)
        {
            BlobContinuationToken continuationToken = null;

            List <string> list = new List <string>();

            do
            {
                const int          BatchSize          = 100;
                bool               useFlatBlobListing = true;                    // files, full recursive,
                BlobListingDetails details            = BlobListingDetails.None; // only  committed blobs, no metadata
                BlobResultSegment  segment            = await _container.ListBlobsSegmentedAsync(
                    pathPrefix, useFlatBlobListing, details, BatchSize, continuationToken, null, null);

                continuationToken = segment.ContinuationToken;

                foreach (IListBlobItem item in segment.Results)
                {
                    var blob = item as CloudBlockBlob;
                    if (blob == null)
                    {
                        continue;
                    }

                    list.Add(blob.Name);
                }
            } while (continuationToken != null);

            return(list.ToArray());
        }
Esempio n. 22
0
        /// <summary>
        /// Load's a blob listing's items.
        /// </summary>
        /// <param name="segmentLoader">
        /// A func for getting the blob listing's next segment.
        /// </param>
        /// <returns>
        /// A concattenation of all the blob listing's resulting segments.
        /// </returns>
        public static async Task <IEnumerable <IListBlobItem> > LoadBlobItemsAsync(
            Func <BlobContinuationToken, Task <BlobResultSegment> > segmentLoader)
        {
            if (segmentLoader == null)
            {
                throw new ArgumentNullException("segmentLoader");
            }

            IEnumerable <IListBlobItem> blobItems = new IListBlobItem[0];

            BlobResultSegment segment = await segmentLoader(null);

            while ((segment != null) &&
                   (segment.Results != null))
            {
                blobItems = blobItems.Concat(segment.Results);

                if (segment.ContinuationToken == null)
                {
                    break;
                }

                segment = await segmentLoader(segment.ContinuationToken);
            }

            return(blobItems);
        }
Esempio n. 23
0
        /// <summary>
        /// Check to see if the specified blob folder exists
        /// </summary>
        /// <param name="blobContainer">Container item</param>
        /// <param name="blobUri">Blob uri of the blob folder</param>
        /// <param name="checkXStoreFolderManifest">Indicates whether to check XStore folder manifest.</param>
        /// <param name="requestOptions">Represents the Azure blob request option.</param>
        /// <returns>
        /// Returns true if exists
        /// </returns>
        public static bool XStoreFolderExists(CloudBlobContainer blobContainer, string blobUri, bool checkXStoreFolderManifest, BlobRequestOptions requestOptions)
        {
            bool folderExists = false;
            CloudBlobDirectory cloudBlobDirectory = blobContainer.GetDirectoryReference(blobUri);

#if !DotNetCoreClr
            IEnumerable <IListBlobItem> blobItems = cloudBlobDirectory.ListBlobs(false, BlobListingDetails.None, requestOptions);
#else
            BlobContinuationToken continuationToken = null;
            do
            {
                BlobResultSegment           resultSegment = cloudBlobDirectory.ListBlobsSegmentedAsync(continuationToken).Result;
                IEnumerable <IListBlobItem> blobItems     = resultSegment.Results;
                continuationToken = resultSegment.ContinuationToken;
#endif

            // Windows Azure storage has strong consistency guarantees.
            // As soon the manifest file is ready, all users are guaranteed
            // to see the complete folder
            if ((!checkXStoreFolderManifest || XStoreFileExists(blobContainer, GetXStoreFolderManifest(blobUri), requestOptions)) &&
                (blobItems.Count() > 0))
            {
                folderExists = true;
            }
#if DotNetCoreClr
        }

        while (!folderExists && (continuationToken != null))
        {
            ;
        }
Esempio n. 24
0
        async private static Task <BlobResultSegment> listBlobsSegmentedInFlatListing(CloudBlobContainer container)
        {
            Console.WriteLine("List blobs in pages: ");

            int i = 0;
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
            //When the continuation token is null, the last page has been returned and execution can exit loop
            do
            {
                //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
                //or by calling a different overload.
                resultSegment = await container.ListBlobsSegmentedAsync("", true, BlobListingDetails.All, 10, continuationToken, null, null);

                if (resultSegment.Results.Count <IListBlobItem>() > 0)
                {
                    Console.WriteLine("Page {0}: ", i++);
                }

                foreach (var blobItem in resultSegment.Results)
                {
                    Console.WriteLine("\t{0}", blobItem.StorageUri.PrimaryUri);
                }
                Console.WriteLine();

                //Get the continuation token
                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);

            return(resultSegment);
        }
Esempio n. 25
0
        void RemoveDirectory(CloudBlobDirectory dir)
        {
            var children = new List <IListBlobItem>();
            BlobContinuationToken continuationToken = null;

            do
            {
                BlobResultSegment segmentResult = dir.ListBlobsSegmented(continuationToken);
                continuationToken = segmentResult.ContinuationToken;
                children.AddRange(segmentResult.Results);
            } while(continuationToken != null);

            foreach (IListBlobItem blob in children)
            {
                if (blob is CloudBlob)
                {
                    RemoveFile((CloudBlob)blob);
                }
                else if (blob is CloudBlobDirectory)
                {
                    RemoveDirectory((CloudBlobDirectory)blob);
                }
                else
                {
                    throw new Exception("Unsupported blob type");
                }
            }
        }
Esempio n. 26
0
        /// <summary>
        /// Lists all the blob names from directly under a directory, excluding subdirectories
        /// </summary>
        /// <param name="directoryName">The name of the directory to list</param>
        /// <param name="cancellationToken">The cancellation token assigned for the operation</param>
        /// <returns>A list for blob names found in the directory</returns>
        public async Task <IList <string> > ListBlobsAsync(string directoryName, CancellationToken cancellationToken)
        {
            // get a reference to the directory
            CloudBlobDirectory blobDirectory = _container.GetDirectoryReference(directoryName ?? string.Empty);

            var blobsList = new List <string>();
            BlobContinuationToken token = null;

            Trace.TraceVerbose($"Listing all blobs under the '{directoryName}' directory");

            do
            {
                // get next blobs segment
                BlobResultSegment segment = await blobDirectory.ListBlobsSegmentedAsync(token, cancellationToken);

                // set the next token
                token = segment.ContinuationToken;

                // add segment results to the list
                blobsList.AddRange(segment.Results.OfType <CloudBlob>().Select(blob => blob.Name));
            } while (token != null);

            Trace.TraceVerbose($"Found {blobsList.Count} blobs under the '{directoryName}' directory");
            return(blobsList);
        }
Esempio n. 27
0
        public ActionResult ListMyFiles2(int?pageNumber)
        {
            CloudBlobContainer     container     = GetEcryptorBlobContainer("ecryptor-outbox");
            List <CAuthorizedFile> blobs         = new List <CAuthorizedFile>();
            BlobResultSegment      resultSegment = container.ListBlobsSegmentedAsync(null).Result;
            int idx = 0;

            foreach (IListBlobItem item in resultSegment.Results)
            {
                if (item.GetType() == typeof(CloudBlockBlob))
                {
                    idx += 1;
                    CloudBlockBlob  blob = (CloudBlockBlob)item;
                    CAuthorizedFile x    = new CAuthorizedFile(blob, idx);
                    blobs.Add(x);
                }
                else if (item.GetType() == typeof(CloudPageBlob))
                {
                    idx += 1;
                    CloudBlockBlob  blob = (CloudBlockBlob)item;
                    CAuthorizedFile x    = new CAuthorizedFile(blob, idx);
                    blobs.Add(x);
                }
                else if (item.GetType() == typeof(CloudBlobDirectory))
                {
                    // ignore folders for now...
                    //CloudBlobDirectory dir = (CloudBlobDirectory)item;
                    //blobs.Add(dir.Uri.ToString(), dir.Uri.ToString());
                }
            }

            return(View(blobs));
        }
Esempio n. 28
0
        private async static Task ShowList(string pattern, bool showSize = false)
        {
            var storageConnectionString = ConfigurationManager.AppSettings[STORAGE_CONNECTION_STRING];
            var storageContainer        = ConfigurationManager.AppSettings[STORAGE_CONTAINER];
            var storageAccount          = CloudStorageAccount.Parse(storageConnectionString);
            var blobClient    = storageAccount.CreateCloudBlobClient();
            var blobContainer = blobClient.GetContainerReference(storageContainer);
            BlobContinuationToken continuationToken = null;
            BlobResultSegment     resultSegment     = null;

            do
            {
                resultSegment = await blobContainer.ListBlobsSegmentedAsync(pattern, true, BlobListingDetails.None, null, continuationToken, null, null);

                foreach (var zz in resultSegment.Results)
                {
                    var blob = (CloudBlockBlob)zz;
                    Console.Write(blob.Name);
                    if (showSize)
                    {
                        blob.FetchAttributes();
                        Console.WriteLine(" {0}", blob.Properties.Length);
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null);
        }
        public async Task <IEnumerable <BlobLease> > ListLeasesAsync()
        {
            var blobLeases = new List <BlobLease>();

            BlobContinuationToken continuationToken = null;

            do
            {
                OperationContext context = new OperationContext {
                    ClientRequestID = Guid.NewGuid().ToString()
                };
                BlobResultSegment segment = await TimeoutHandler.ExecuteWithTimeout("ListLeases", context.ClientRequestID, storageAccountName, taskHubName, () =>
                {
                    return(this.consumerGroupDirectory.ListBlobsSegmentedAsync(continuationToken));
                });

                continuationToken = segment.ContinuationToken;

                var downloadTasks = new List <Task <BlobLease> >();
                foreach (IListBlobItem blob in segment.Results)
                {
                    CloudBlockBlob lease = blob as CloudBlockBlob;
                    if (lease != null)
                    {
                        downloadTasks.Add(this.DownloadLeaseBlob(lease));
                    }
                }

                await Task.WhenAll(downloadTasks);

                blobLeases.AddRange(downloadTasks.Select(t => t.Result));
            }while (continuationToken != null);

            return(blobLeases);
        }
Esempio n. 30
0
        private static async Task <List <IListBlobItem> > ListBlobsAsync(CloudBlobDirectory cloudBlobDirectory)
        {
            BlobContinuationToken continuationToken = null;

            BlobResultSegment blobResultSegment = null;

            List <IListBlobItem> blobItems = new List <IListBlobItem>();

            do
            {
                try
                {
                    blobResultSegment = await cloudBlobDirectory.ListBlobsSegmentedAsync(continuationToken);

                    continuationToken = blobResultSegment.ContinuationToken;

                    blobItems.AddRange(blobResultSegment.Results);

                    if (blobItems.Count >= 100000)
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Problem with {blobResultSegment?.Results} {ex.Message} {ex?.InnerException?.Message}");
                }
            }while (continuationToken != null);

            return(blobItems);
        }