public async Task <IEnumerable <string> > GetDirectoriesAsync(string directoryPath,
                                                                      string searchPattern = null,
                                                                      bool recursive       = false)
        {
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                directoryPath = _rootDir.Name;
            }

            directoryPath = Url.DirToUrlSeparator(directoryPath);

            var directory = await GetDirectoryAsync(directoryPath);

            if (directory == null || !await directory.ExistsAsync())
            {
                throw new DirectoryNotFoundException($"Cannot find directory '{directoryPath}'");
            }

            var token = new FileContinuationToken();
            var items = await directory.ListFilesAndDirectoriesSegmentedAsync(token);

            var directories = new List <string>();

            foreach (var fileDir in items.Results)
            {
                if (fileDir is CloudFileDirectory)
                {
                    var dir = (CloudFileDirectory)fileDir;
                    if (string.IsNullOrWhiteSpace(searchPattern) || dir.Name.Like(searchPattern))
                    {
                        directories.Add(Url.Combine(directoryPath, dir.Name));
                    }

                    if (recursive)
                    {
                        directories.AddRange(await GetDirectoriesAsync(Url.Combine(directoryPath, dir.Name),
                                                                       searchPattern, recursive));
                    }
                }
            }

            return(directories);
        }
 protected bool ShareIsEmpty(CloudFileShare share)
 {
     try
     {
         FileContinuationToken       fileToken   = new FileContinuationToken();
         IEnumerator <IListFileItem> listedFiles = share.GetRootDirectoryReference().ListFilesAndDirectoriesSegmented(1, fileToken, RequestOptions, OperationContext).Results.GetEnumerator();
         if (listedFiles.MoveNext() && listedFiles.Current != null)
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
     catch (Exception)
     {
         return(false);
     }
 }
        public async Task <IEnumerable <CloudFileShare> > GetManagedSnapshotsAsync(string shareName, CancellationToken cancellationToken = default(CancellationToken))
        {
            var results = new List <CloudFileShare>();

            FileContinuationToken fileContinuationToken = null;

            do
            {
                var shareResultSegment = await this.cloudFileClient.ListSharesSegmentedAsync(shareName, ShareListingDetails.Snapshots | ShareListingDetails.Metadata, null, fileContinuationToken, null, null, cancellationToken);

                results.AddRange(shareResultSegment.Results.Where(x =>
                                                                  x.Name == shareName &&
                                                                  x.IsSnapshot &&
                                                                  x.Metadata.ContainsKey(this.ManagedSnapshotMetadataKey)));

                fileContinuationToken = shareResultSegment.ContinuationToken;
            } while (fileContinuationToken != null);

            return(results);
        }
Example #4
0
        protected override async Task <IReadOnlyCollection <Blob> > ListAtAsync(
            string path, ListOptions options, CancellationToken cancellationToken)
        {
            if (StoragePath.IsRootPath(path))
            {
                //list file shares

                ShareResultSegment shares = await _client.ListSharesSegmentedAsync(null, cancellationToken).ConfigureAwait(false);

                return(shares.Results.Select(AzConvert.ToBlob).ToList());
            }
            else
            {
                var chunk = new List <Blob>();

                CloudFileDirectory dir = await GetDirectoryReferenceAsync(path, cancellationToken).ConfigureAwait(false);

                FileContinuationToken token = null;
                do
                {
                    try
                    {
                        FileResultSegment segment = await dir.ListFilesAndDirectoriesSegmentedAsync(options.FilePrefix, token, cancellationToken).ConfigureAwait(false);

                        token = segment.ContinuationToken;

                        chunk.AddRange(segment.Results.Select(r => AzConvert.ToBlob(path, r)));
                    }
                    catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ShareNotFound")
                    {
                        break;
                    }
                    catch (AzStorageException ex) when(ex.RequestInformation.ErrorCode == "ResourceNotFound")
                    {
                        break;
                    }
                }while(token != null);

                return(chunk);
            }
        }
        public async Task <IEnumerable <StorageProviderItem> > GetChildrenByParentItem(StorageProviderItem parent)
        {
            var fileShare = await GetShare(_account);

            var rootDir = fileShare.GetRootDirectoryReference();

            var directory = rootDir;

            if (!String.IsNullOrEmpty(parent.Id))
            {
                directory = rootDir.GetDirectoryReference(parent.Id);
            }

            FileContinuationToken fileContinuationToken = null;
            var result = new List <StorageProviderItem>();

            do
            {
                var listSegment = await directory.ListFilesAndDirectoriesSegmentedAsync(fileContinuationToken);

                result.AddRange(listSegment.Results.OfType <CloudFileDirectory>().Select(s => new StorageProviderItem
                {
                    Id   = GetId(s.Parent, s.Name),
                    Name = s.Name,
                    Type = StorageProviderItemType.Folder,
                    ParentReferenceId = parent.Id
                }));

                result.AddRange(listSegment.Results.OfType <CloudFile>().Select(s => new StorageProviderItem
                {
                    Id   = GetId(s.Parent, s.Name),
                    Name = s.Name,
                    Type = StorageProviderItemType.File,
                    ParentReferenceId = parent.Id
                }));

                fileContinuationToken = listSegment.ContinuationToken;
            } while (fileContinuationToken != null);

            return(result);
        }
Example #6
0
        public static async Task <List <IListFileItem> > ListFilesAndDirsAsync(string account, string key, string share, string folder = null)
        {
            CloudFileShare        shareRef          = Get(account, key, share);
            FileContinuationToken continuationToken = null;
            List <IListFileItem>  results           = new List <IListFileItem>();

            do
            {
                CloudFileDirectory dir = shareRef.GetRootDirectoryReference();
                if (!string.IsNullOrEmpty(folder))
                {
                    dir = dir.GetDirectoryReference(folder);
                }

                var response = await dir.ListFilesAndDirectoriesSegmentedAsync(continuationToken);

                continuationToken = response.ContinuationToken;
                results.AddRange(response.Results);
            }while (continuationToken != null);

            return(results);
        }
        private IEnumerable <CloudFile> GetFiles(string folder)
        {
            var results = new List <CloudFile>();
            FileContinuationToken token = null;

            do
            {
                var resultSegment = GetDirectory(folder)
                                    .ListFilesAndDirectoriesSegmented(token);

                foreach (var result in resultSegment.Results)
                {
                    var cloadFiles = result as CloudFile;
                    if (cloadFiles != null)
                    {
                        results.Add(cloadFiles);
                    }
                }
                token = resultSegment.ContinuationToken;
            }while (token != null);
            return(results);
        }
Example #8
0
        public async Task <IEnumerable <CloudFile> > GetFiles()
        {
            do
            {
                List <CloudFile> cloudFiles = new List <CloudFile>();

                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AzureStorageAccount);
                CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();

                FileContinuationToken token = null;
                ShareResultSegment    shareResultSegment = await fileClient.ListSharesSegmentedAsync("Pat", token);

                foreach (CloudFileShare share in shareResultSegment.Results)
                {
                    CloudFileDirectory rootDir   = share.GetRootDirectoryReference();
                    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(DateTime.Now.ToString("yyyyMMdd"));
                    if (await sampleDir.ExistsAsync())                            //Console.WriteLine(cloudFile.Uri.ToString()+'\n');
                    {
                        do
                        {
                            FileResultSegment resultSegment = await sampleDir.ListFilesAndDirectoriesSegmentedAsync(token);

                            token = resultSegment.ContinuationToken;

                            List <IListFileItem> listedFileItems = new List <IListFileItem>();

                            foreach (IListFileItem listResultItem in resultSegment.Results)
                            {
                                CloudFile cloudFile = sampleDir.GetFileReference(listResultItem.Uri.ToString());
                                cloudFiles.Add(cloudFile);
                            }
                        }while (token != null);
                    }
                }

                return(cloudFiles);
            } while (true);
        }
        public async Task Test_Azure_File_Storage_Query_Shared_Files()
        {
            Check.That(this._storageAccountName).IsNotEmpty();

            Check.That(this._storageKey).IsNotEmpty();


            List <CloudFileShare> fileShares = new List <CloudFileShare>();

            FileContinuationToken fct = null;

            do
            {
                var result = await this._cloudFileClient.ListSharesSegmentedAsync(fct);

                fct = result.ContinuationToken;

                fileShares.AddRange(result.Results);
            } while (fct != null);


            Check.That(fileShares.Count).IsStrictlyGreaterThan(0);
        }
Example #10
0
        private async Task DeleteDirectoryAsync(CloudFileDirectory dir, CancellationToken cancellationToken)
        {
            FileContinuationToken token = null;

            do
            {
                FileResultSegment chunk = await dir.ListFilesAndDirectoriesSegmentedAsync(token, cancellationToken).ConfigureAwait(false);

                foreach (IListFileItem item in chunk.Results)
                {
                    if (item is CloudFile file)
                    {
                        await file.DeleteIfExistsAsync(cancellationToken).ConfigureAwait(false);
                    }
                    else if (item is CloudFileDirectory subdir)
                    {
                        await DeleteDirectoryAsync(subdir, cancellationToken);
                    }
                }

                token = chunk.ContinuationToken;
            }while(token != null);
        }
Example #11
0
        public override async Task <IEnumerable <T> > ListContainedItemsAsync <T>(string filter, int maxResults, CancellationToken cancellationToken, TimeSpan timeout)
        {
            DateTime             start  = DateTime.UtcNow;
            DateTime             end    = start.Add(timeout);
            List <IListFileItem> result = new List <IListFileItem>();
            CloudFileDirectory   d      = new CloudFileDirectory(URI, StorageAccount.Credentials);

            FileContinuationToken continuationToken = null;
            FileResultSegment     resultSegment     = null;

            do
            {
                resultSegment = await _dir.ListFilesAndDirectoriesSegmentedAsync(filter, maxResults, continuationToken, null, null, cancellationToken);

                result.AddRange(resultSegment.Results);
                if (result.Count > maxResults)
                {
                    return(result.Take(maxResults) as IEnumerable <T>);
                }
                continuationToken = resultSegment.ContinuationToken;
            }while (continuationToken != null && !cancellationToken.IsCancellationRequested && DateTime.UtcNow < end);
            return(result as IEnumerable <T>);
        }
        public override void ExecuteCmdlet()
        {
            if (this.ShouldProcess(string.Format("Close File Handles for File or FileDirectory on Path: {0}", this.Path != null? this.Path : (this.FileHandle != null? this.FileHandle.Path: null)), "This operation will force the provided file handle(s) closed, which may cause data loss or corruption for active applications/users.", null))
            {
                CloudFileDirectory baseDirectory = null;
                switch (this.ParameterSetName)
                {
                case DirectoryCloseAllParameterSetName:
                    baseDirectory = this.Directory;
                    break;

                case ShareNameCloseSingleParameterSetName:
                case ShareNameCloseAllParameterSetName:
                    baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference();
                    break;

                case ShareCloseSingleParameterSetName:
                case ShareCloseAllParameterSetName:
                    baseDirectory = this.Share.GetRootDirectoryReference();
                    break;

                case FileCloseAllParameterSetName:
                    // Don't need to set baseDirectory when input is a CloudFile
                    break;

                default:
                    throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
                }

                if (ParameterSetName == ShareNameCloseSingleParameterSetName || ParameterSetName == ShareCloseSingleParameterSetName)
                {
                    this.Path = FileHandle.Path;
                }

                // When not input path/File, the list handle target must be a Dir
                bool foundAFolder             = true;
                CloudFileDirectory targetDir  = baseDirectory;
                CloudFile          targetFile = null;
                if (this.File != null)
                {
                    targetFile   = this.File;
                    foundAFolder = false;
                }
                else
                {
                    if (!string.IsNullOrEmpty(this.Path))
                    {
                        string[] subfolders = NamingUtil.ValidatePath(this.Path);
                        targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders);

                        // Don't need check the path target to File or FileDir since:
                        // 1. check File/FileDir exist will fail on File/FileDir with DeletePending status
                        // 2. The File handle request send with CloudFileDirectory and CloudFile are same with same path, so need to differ it.
                    }
                }

                // Recursive only take effect on File Dir
                if (!foundAFolder && Recursive.IsPresent)
                {
                    WriteVerbose("The target object of the 'Path' is an Azure File, the parameter '-Recursive' won't take effect.");
                }

                //Close handle
                CloseFileHandleResultSegment closeResult       = null;
                FileContinuationToken        continuationToken = null;
                int numHandlesClosed = 0;
                do
                {
                    if (foundAFolder)
                    {
                        if (FileHandle != null)
                        {
                            // close single handle on fileDir
                            if (this.FileHandle.HandleId == null)
                            {
                                throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle");
                            }
                            closeResult = targetDir.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, Recursive, null, this.RequestOptions, this.OperationContext);
                        }
                        else
                        {
                            // close all handle on fileDir
                            closeResult = targetDir.CloseAllHandlesSegmented(continuationToken, Recursive, null, this.RequestOptions, this.OperationContext);
                        }
                    }
                    else
                    {
                        if (FileHandle != null)
                        {
                            // close single handle on file
                            if (this.FileHandle.HandleId == null)
                            {
                                throw new System.ArgumentException(string.Format("The HandleId of the FileHandle on path {0} should not be null.", this.FileHandle.Path), "FileHandle");
                            }
                            closeResult = targetFile.CloseHandleSegmented(this.FileHandle.HandleId.ToString(), continuationToken, null, this.RequestOptions, this.OperationContext);
                        }
                        else
                        {
                            // close all handle on file
                            closeResult = targetFile.CloseAllHandlesSegmented(continuationToken, null, this.RequestOptions, this.OperationContext);
                        }
                    }
                    numHandlesClosed += closeResult.NumHandlesClosed;
                    continuationToken = closeResult.ContinuationToken;
                } while (continuationToken != null && continuationToken.NextMarker != null);

                if (PassThru)
                {
                    WriteObject(numHandlesClosed);
                }
            }
        }
        private async Task <IEnumerable <TListFileItem> > ListFileItems <TListFileItem>(
            string[] directoryPath,
            CancellationToken cancellationToken)
            where TListFileItem : IListFileItem
        {
            IEnumerable <TListFileItem> toReturn = null;

            if (directoryPath == null)
            {
                throw new ArgumentNullException(nameof(directoryPath));
            }

            this.loggerWrapper.Debug(
                $"Getting root directory reference for " +
                $"\"{this.sourceCloudFileShare.Name}\"...");

            CloudFileDirectory shareRoot =
                this.sourceCloudFileShare.GetRootDirectoryReference();

            CloudFileDirectory innerDir = shareRoot;

            foreach (string directory in directoryPath)
            {
                this.loggerWrapper.Debug(
                    $"Getting reference to directory \"{directory}\"...");

                innerDir = innerDir.GetDirectoryReference(directory);
            }

            this.loggerWrapper.Debug(
                "Beginning listing of files/directories...");

            List <TListFileItem> listFileItems = new List <TListFileItem>();

            IEnumerable <IListFileItem> results               = null;
            IEnumerable <TListFileItem> castedResults         = null;
            FileContinuationToken       fileContinuationToken = null;

            do
            {
                FileResultSegment fileResultSegment =
                    await innerDir.ListFilesAndDirectoriesSegmentedAsync(
                        null,
                        fileContinuationToken,
                        this.fileRequestOptions,
                        this.operationContext,
                        cancellationToken)
                    .ConfigureAwait(false);

                results = fileResultSegment.Results;

                this.loggerWrapper.Debug(
                    $"{results.Count()} result(s) returned. Filtering to " +
                    $"directories...");

                castedResults = results
                                .Where(x => x is TListFileItem)
                                .Cast <TListFileItem>();

                this.loggerWrapper.Debug(
                    $"Adding {results.Count()} filtered results to the " +
                    $"overall results list...");

                listFileItems.AddRange(castedResults);

                this.loggerWrapper.Info(
                    $"Total filtered results so far: " +
                    $"{listFileItems.Count}.");

                fileContinuationToken = fileResultSegment.ContinuationToken;

                if (fileContinuationToken != null)
                {
                    this.loggerWrapper.Debug(
                        $"{nameof(FileContinuationToken)} present. Looping " +
                        $"round again...");
                }
            }while (fileContinuationToken != null);

            toReturn = listFileItems.ToList();

            return(toReturn);
        }
        public override void ExecuteCmdlet()
        {
            CloudFileDirectory baseDirectory = null;

            switch (this.ParameterSetName)
            {
            case Constants.DirectoryParameterSetName:
                baseDirectory = this.Directory;
                break;

            case Constants.ShareNameParameterSetName:
                baseDirectory = this.BuildFileShareObjectFromName(this.ShareName).GetRootDirectoryReference();
                break;

            case Constants.ShareParameterSetName:
                baseDirectory = this.Share.GetRootDirectoryReference();
                break;

            case Constants.FileParameterSetName:
                // Don't need to set baseDirectory when input is a CloudFile
                break;

            default:
                throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid parameter set name: {0}", this.ParameterSetName));
            }

            // When not input path/File, the list handle target must be a Dir
            bool foundAFolder             = true;
            CloudFileDirectory targetDir  = baseDirectory;
            CloudFile          targetFile = null;

            if (this.File != null)
            {
                targetFile   = this.File;
                foundAFolder = false;
            }
            else
            {
                if (!string.IsNullOrEmpty(this.Path))
                {
                    string[] subfolders = NamingUtil.ValidatePath(this.Path);
                    targetDir = baseDirectory.GetDirectoryReferenceByPath(subfolders);

                    if (!targetDir.Exists())
                    {
                        foundAFolder = false;
                    }

                    if (!foundAFolder)
                    {
                        //Get file
                        string[] filePath = NamingUtil.ValidatePath(this.Path, true);
                        targetFile = baseDirectory.GetFileReferenceByPath(filePath);

                        this.Channel.FetchFileAttributesAsync(
                            targetFile,
                            null,
                            this.RequestOptions,
                            this.OperationContext,
                            this.CmdletCancellationToken).ConfigureAwait(false);
                    }
                }
            }

            // Recursive only take effect on File Dir
            if (!foundAFolder && Recursive.IsPresent)
            {
                WriteVerbose("The target object of the 'Path' is an Azure File, the parameter '-Recursive' won't take effect.");
            }

            //List handle
            FileHandleResultSegment listResult;
            FileContinuationToken   continuationToken = null;
            List <PSFileHandle>     handleReturn      = new List <PSFileHandle>();
            ulong first = MyInvocation.BoundParameters.ContainsKey("First") ? this.PagingParameters.First : ulong.MaxValue;
            ulong skip  = MyInvocation.BoundParameters.ContainsKey("Skip") ? this.PagingParameters.Skip : 0;

            // Any items before this count should be return
            ulong lastCount    = MyInvocation.BoundParameters.ContainsKey("First") ? skip + first : ulong.MaxValue;
            ulong currentCount = 0;

            do
            {
                if (foundAFolder)
                {
                    // list handle on fileDir
                    listResult = targetDir.ListHandlesSegmented(continuationToken, null, Recursive, null, this.RequestOptions, this.OperationContext);
                }
                else
                {
                    // list handle on file
                    listResult = targetFile.ListHandlesSegmented(continuationToken, null, null, this.RequestOptions, this.OperationContext);
                }

                List <FileHandle> handleList = new List <FileHandle>(listResult.Results);

                if (currentCount + (ulong)handleList.Count - 1 < skip)
                {
                    // skip the whole chunk if they are all in skip
                    currentCount += (ulong)handleList.Count;
                }
                else
                {
                    foreach (FileHandle handle in handleList)
                    {
                        // not return "skip" count of items in the begin, and only return "first" count of items after that.
                        if (currentCount >= skip && currentCount < lastCount)
                        {
                            handleReturn.Add(new PSFileHandle(handle));
                        }
                        currentCount++;
                        if (currentCount >= lastCount)
                        {
                            break;
                        }
                    }
                }
                continuationToken = listResult.ContinuationToken;
            } while (continuationToken != null && continuationToken.NextMarker != null && currentCount < lastCount);

            WriteObject(handleReturn, true);
        }
Example #15
0
        private static async Task mainAsync(string[] args)
        {
            try
            {
                var builder = new ConfigurationBuilder()
                              .SetBasePath(Directory.GetCurrentDirectory())
                              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

                IConfigurationRoot configuration = builder.Build();
                var allCitDebtsLines             = new string[10000000];
                var allSettlementsLines          = new string[10000000];
                var allPaymentLines = new string[10000000];
                while (true)
                {
                    if (DateTime.Now.AddHours(3).ToShortTimeString() == "12:00 PM")
                    {
                        List <string>       fileNames         = new List <string>();
                        List <string[]>     lstCloudFilesdata = new List <string[]>();
                        CloudStorageAccount storageAccount    = CloudStorageAccount.Parse($"{configuration["ConnectionString1"]}");
                        CloudFileClient     fileClient        = storageAccount.CreateCloudFileClient();
                        CloudFileShare      fileShare         = fileClient.GetShareReference("import");
                        //looks for a file share in the cloud
                        bool fileShareExists = await fileShare.ExistsAsync();

                        if (fileShareExists)
                        {
                            List <CloudFile>   lstCloudFiles = new List <CloudFile>();
                            CloudFileDirectory rootDir       = fileShare.GetRootDirectoryReference();
                            //for each file in my fileshare
                            FileContinuationToken token = null;
                            FileResultSegment     k     = await rootDir.ListFilesAndDirectoriesSegmentedAsync(token);

                            token = k.ContinuationToken;

                            //var context_ = new Db.Data.();
                            List <string> sl = new List <string>();

                            foreach (IListFileItem fiile in k.Results)
                            {
                                //if the file exists
                                CloudFile file = (CloudFile)fiile;
                                bool      asd  = await file.ExistsAsync();

                                if (asd)
                                {
                                    //adds new datasting array
                                    sl = await ReadDataAsync(lstCloudFilesdata, file, fileNames);
                                }

                                foreach (string y in sl)
                                {
                                    Console.WriteLine("From list new : " + y);
                                }
                                ;

                                IEnumerable <CitizenDepts> o = from eachLine in (
                                    from inner in sl
                                    select inner.Split(';')
                                    )
                                                               select new CitizenDepts
                                {
                                    VAT              = eachLine[0],
                                    FirstName        = eachLine[1],
                                    LastName         = eachLine[2],
                                    Email            = eachLine[3],
                                    Phone            = eachLine[4],
                                    Address          = eachLine[5],
                                    County           = eachLine[6],
                                    BillId           = eachLine[7],
                                    Bill_description = eachLine[8],
                                    Amount           = Decimal.Parse(eachLine[9]),
                                    DueDate          = DateTime.ParseExact(eachLine[10],
                                                                           "yyyyMMdd", CultureInfo.InvariantCulture)
                                };
                                foreach (var p in o)
                                {
                                    Console.WriteLine(p.FirstName + " - " + p.BillId + ", - " + p.DueDate);
                                }
                                ;



                                //string s = context_.Database.ProviderName;

                                // Console.WriteLine(s);
                                /// var all = from c in context_.CitizenDepts select c;
                                //context_.CitizenDepts.RemoveRange(all);
                                /// context_.SaveChanges();

                                foreach (var p in o)
                                {
                                    //Add Student object into Students DBset
                                    //if (p.VAT!=null)
                                    //context_.Add(p);
                                }
                                //// call SaveChanges method to save student into database
                                //context_.SaveChanges();
                            }
                        }
                        if (lstCloudFilesdata != null && fileNames != null)
                        {
                            ProccessData(lstCloudFilesdata, fileNames);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
            }
        }
        /// <summary>
        /// Basic operations to work with Azure Files
        /// </summary>
        /// <returns>Task</returns>
        private static async Task BasicAzureFileOperationsAsync()
        {
            const string DemoShare     = "demofileshare";
            const string DemoDirectory = "demofiledirectory";
            const string ImageToUpload = "HelloWorld.png";

            // Retrieve storage account information from connection string
            // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create a file client for interacting with the file service.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            // Create a share for organizing files and directories within the storage account.
            Console.WriteLine("1. Creating file share");
            CloudFileShare share = fileClient.GetShareReference(DemoShare);

            try
            {
                await share.CreateIfNotExistsAsync();
            }
            catch (StorageException)
            {
                Console.WriteLine("Please make sure your storage account has storage file endpoint enabled and specified correctly in the app.config - then restart the sample.");
                Console.WriteLine("Press any key to exit");
                Console.ReadLine();
                throw;
            }

            // Get a reference to the root directory of the share.
            CloudFileDirectory root = share.GetRootDirectoryReference();

            // Create a directory under the root directory
            Console.WriteLine("2. Creating a directory under the root directory");
            CloudFileDirectory dir = root.GetDirectoryReference(DemoDirectory);
            await dir.CreateIfNotExistsAsync();

            // Uploading a local file to the directory created above
            Console.WriteLine("3. Uploading a file to directory");
            CloudFile file = dir.GetFileReference(ImageToUpload);
            await file.UploadFromFileAsync(ImageToUpload);

            // List all files/directories under the root directory
            Console.WriteLine("4. List Files/Directories in root directory");
            List <IListFileItem>  results = new List <IListFileItem>();
            FileContinuationToken token   = null;

            do
            {
                FileResultSegment resultSegment = await share.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(token);

                results.AddRange(resultSegment.Results);
                token = resultSegment.ContinuationToken;
            }while (token != null);

            // Print all files/directories listed above
            foreach (IListFileItem listItem in results)
            {
                // listItem type will be CloudFile or CloudFileDirectory
                Console.WriteLine("- {0} (type: {1})", listItem.Uri, listItem.GetType());
            }

            // Download the uploaded file to your file system
            Console.WriteLine("5. Download file from {0}", file.Uri.AbsoluteUri);
            await file.DownloadToFileAsync(string.Format("./CopyOf{0}", ImageToUpload), FileMode.Create);

            // Clean up after the demo
            Console.WriteLine("6. Delete file");
            await file.DeleteAsync();

            // When you delete a share it could take several seconds before you can recreate a share with the same
            // name - hence to enable you to run the demo in quick succession the share is not deleted. If you want
            // to delete the share uncomment the line of code below.
            // Console.WriteLine("7. Delete Share");
            // await share.DeleteAsync();
        }
Example #17
0
        /// <summary>
        /// Recursive delete of folder and all included subfolders or files.
        /// </summary>
        /// <param name="cloudFileDirectory">Directory to delete</param>
        public static void DeleteRecursive(this CloudFileDirectory cloudFileDirectory)
        {
            #region validation

            if (cloudFileDirectory == null)
            {
                throw new ArgumentNullException(nameof(cloudFileDirectory));
            }

            #endregion

            if (!TaskUtilities.ExecuteSync(cloudFileDirectory.ExistsAsync()))
            {
                return;
            }

            var directoriesToBeDeleted = new List <CloudFileDirectory>();

            var continuationToken = new FileContinuationToken();

            // get first segment
            FileResultSegment fileResultSegment;

            do
            {
                fileResultSegment = TaskUtilities.ExecuteSync(cloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(continuationToken));

                // iterate through items
                foreach (IListFileItem fileListItem in fileResultSegment.Results)
                {
                    switch (fileListItem)
                    {
                    case CloudFile file:

                        // Delete file directly
                        TaskUtilities.ExecuteSync(file.DeleteAsync());

                        break;

                    case CloudFileDirectory directory:

                        // Add the current directory
                        directoriesToBeDeleted.Add(directory);

                        // List all sub directories recursively
                        directoriesToBeDeleted.AddRange(directory.ListSubDirectories());

                        break;
                    }
                }
            } while (fileResultSegment.ContinuationToken != null);

            // Sort directories bottom to top so the most deepest nested directories will be deleted first
            directoriesToBeDeleted.Sort((aDirectory, bDirectory) => bDirectory.Uri.LocalPath.Length.CompareTo(aDirectory.Uri.LocalPath.Length));

            // Delete all found directories
            foreach (CloudFileDirectory cloudFileDirectoryToBeDeleted in directoriesToBeDeleted)
            {
                TaskUtilities.ExecuteSync(cloudFileDirectoryToBeDeleted.DeleteAsync());
            }

            // Delete the parent directory itself in case itĀ“s not the root directory (which is the file share)
            if (cloudFileDirectory.SnapshotQualifiedUri != cloudFileDirectory.Share.GetRootDirectoryReference().SnapshotQualifiedUri)
            {
                TaskUtilities.ExecuteSync(cloudFileDirectory.DeleteAsync());
            }
        }
Example #18
0
        /// <summary>
        /// Constructs a web request to return the list of open handles for a file or directory.
        /// </summary>
        /// <param name="uri">The absolute URI to the file.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="maxResults">The maximum number of results to be returned by the server.</param>
        /// <param name="recursive">Whether to recurse through a directory's files and subfolders.</param>
        /// <param name="nextMarker">Marker returned by a previous call to continue fetching results.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext" /> object for tracking the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static StorageRequestMessage ListHandles(Uri uri, int?timeout, int?maxResults, bool?recursive, FileContinuationToken nextMarker, AccessCondition accessCondition, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "listhandles");

            if (maxResults.HasValue)
            {
                builder.Add(Constants.MaxResults, maxResults.Value.ToString());
            }

            if (nextMarker != null)
            {
                builder.Add(Constants.HeaderConstants.Marker, nextMarker.NextMarker);
            }

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, content, operationContext, canonicalizer, credentials);

            request.ApplyAccessCondition(accessCondition);

            if (recursive.HasValue)
            {
                request.AddOptionalHeader(Constants.HeaderConstants.Recursive, recursive.Value.ToString());
            }

            return(request);
        }
Example #19
0
        /// <summary>
        /// Enumerates a specified cloud file directory and returns items based item type and searchPattern.
        /// </summary>
        /// <param name="cloudFileDirectory">Cloud file directory to enumerate on</param>
        /// <param name="fileType">Item type to filter with</param>
        /// <param name="searchPattern">Search pattern to filter with</param>
        /// <returns>Enumerable of strings which contains all found names based on file type</returns>
        public static IEnumerable <string> EnumerateDirectory(this CloudFileDirectory cloudFileDirectory, EFileType fileType, string searchPattern)
        {
            #region validation

            if (cloudFileDirectory == null)
            {
                throw new ArgumentNullException(nameof(cloudFileDirectory));
            }

            if (string.IsNullOrEmpty(searchPattern))
            {
                throw new ArgumentNullException(nameof(searchPattern));
            }

            if (!TaskUtilities.ExecuteSync(cloudFileDirectory.ExistsAsync()))
            {
                return(new List <string>());
            }

            #endregion

            var directoryNames = new List <string>();

            var fileContinuationToken = new FileContinuationToken();

            do
            {
                FileResultSegment fileResultSegment = TaskUtilities.ExecuteSync(cloudFileDirectory.ListFilesAndDirectoriesSegmentedAsync(fileContinuationToken));

                foreach (IListFileItem listFileItem in fileResultSegment.Results)
                {
                    string fullListItemPath = string.Empty;

                    switch (fileType)
                    {
                    case EFileType.Files:
                        if (listFileItem is CloudFile cloudFile)
                        {
                            fullListItemPath = cloudFile.StorageUri.PrimaryUri.ToString();
                        }

                        break;

                    case EFileType.Directories:
                        if (listFileItem is CloudFileDirectory pendingCloudFileDirectory)
                        {
                            fullListItemPath = pendingCloudFileDirectory.StorageUri.PrimaryUri.ToString();

                            if (fullListItemPath.EndsWith('/'))
                            {
                                fullListItemPath = fullListItemPath.Remove(fullListItemPath.LastIndexOf('/'));
                            }
                        }

                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(fileType), fileType, null);
                    }

                    // replace wildcard for 'multiple letters' with regex-wildcard
                    string regexableSearchPattern = searchPattern.Replace("*", ".*");

                    // replace wildcard for 'single letter' with regex-wildcard
                    regexableSearchPattern = regexableSearchPattern.Replace("?", ".?");

                    // set search pattern in 'begin-to-end'-symbols
                    regexableSearchPattern = $"^{regexableSearchPattern}$";

                    var rgxFileName = new Regex(regexableSearchPattern);

                    if (!string.IsNullOrEmpty(fullListItemPath) && rgxFileName.IsMatch(fullListItemPath))
                    {
                        directoryNames.Add(fullListItemPath);
                    }
                }

                fileContinuationToken = fileResultSegment.ContinuationToken;
            } while (fileContinuationToken != null);

            return(directoryNames);
        }
Example #20
0
        private IEnumerable <TransferEntry> EnumerateLocationRecursive(CancellationToken cancellationToken)
        {
            string fullPrefix = Uri.UnescapeDataString(this.location.FileDirectory.SnapshotQualifiedUri.AbsolutePath);

            // Normalize full prefix to end with slash.
            if (!string.IsNullOrEmpty(fullPrefix) && !fullPrefix.EndsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                fullPrefix += '/';
            }

            Stack <CloudFileDirectory> directoriesToList = new Stack <CloudFileDirectory>();

            directoriesToList.Push(this.location.FileDirectory);

            string[] pathSegList             = null;
            bool     passedContinuationToken = false;
            int      pathSegListIndex        = 0;

            if (null != this.listContinuationToken)
            {
                pathSegList = this.listContinuationToken.FilePath.Split(new char[] { UriDelimiter });
            }
            else
            {
                passedContinuationToken = true;
            }

            while (0 != directoriesToList.Count)
            {
                CloudFileDirectory directory       = directoriesToList.Pop();
                string             dirAbsolutePath = Uri.UnescapeDataString(directory.SnapshotQualifiedUri.AbsolutePath);
                if (dirAbsolutePath[dirAbsolutePath.Length - 1] != UriDelimiter)
                {
                    dirAbsolutePath = dirAbsolutePath + UriDelimiter;
                }

                Stack <CloudFileDirectory> innerDirList = new Stack <CloudFileDirectory>();

                FileContinuationToken continuationToken = null;

                // To check whether reached continuation token by dir or file in this round.
                bool   checkFile            = false;
                bool   passedSubFolder      = false;
                string continuationTokenSeg = null;
                if (!passedContinuationToken)
                {
                    if (pathSegList.Length - 1 == pathSegListIndex)
                    {
                        checkFile = true;
                    }

                    continuationTokenSeg = pathSegList[pathSegListIndex];
                    pathSegListIndex++;
                }

                do
                {
                    FileResultSegment resultSegment = null;
                    Utils.CheckCancellation(cancellationToken);

                    ErrorEntry errorEntry = null;

                    try
                    {
                        FileRequestOptions requestOptions = Transfer_RequestOptions.DefaultFileRequestOptions;
                        resultSegment = directory.ListFilesAndDirectoriesSegmentedAsync(
                            ListFilesSegmentSize,
                            continuationToken,
                            requestOptions,
                            null,
                            cancellationToken).Result;
                    }
                    catch (Exception ex)
                    {
                        string errorMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.FailedToEnumerateDirectory,
                            this.location.FileDirectory.SnapshotQualifiedUri.AbsoluteUri,
                            string.Empty);

                        TransferException exception =
                            new TransferException(TransferErrorCode.FailToEnumerateDirectory, errorMessage, ex);
                        errorEntry = new ErrorEntry(exception);
                    }

                    if (null != errorEntry)
                    {
                        yield return(errorEntry);

                        yield break;
                    }

                    continuationToken = resultSegment.ContinuationToken;

                    foreach (IListFileItem fileItem in resultSegment.Results)
                    {
                        Utils.CheckCancellation(cancellationToken);

                        if (fileItem is CloudFileDirectory)
                        {
                            if (checkFile || passedContinuationToken || passedSubFolder)
                            {
                                innerDirList.Push(fileItem as CloudFileDirectory);
                            }
                            else
                            {
                                CloudFileDirectory cloudDir = fileItem as CloudFileDirectory;
                                string             fullPath = Uri.UnescapeDataString(cloudDir.SnapshotQualifiedUri.AbsolutePath);
                                string             segName  = fullPath.Remove(0, dirAbsolutePath.Length);

                                int compareResult = string.Compare(segName, continuationTokenSeg, StringComparison.OrdinalIgnoreCase);

                                if (compareResult >= 0)
                                {
                                    passedSubFolder = true;
                                    innerDirList.Push(cloudDir);

                                    if (compareResult > 0)
                                    {
                                        passedContinuationToken = true;
                                    }
                                }
                            }
                        }
                        else if (fileItem is CloudFile)
                        {
                            if (!checkFile && !passedContinuationToken)
                            {
                                continue;
                            }

                            CloudFile cloudFile = fileItem as CloudFile;

                            string fullPath     = Uri.UnescapeDataString(cloudFile.SnapshotQualifiedUri.AbsolutePath);
                            string relativePath = fullPath.Remove(0, fullPrefix.Length);

                            if (passedContinuationToken)
                            {
                                yield return(new AzureFileEntry(
                                                 relativePath,
                                                 cloudFile,
                                                 new AzureFileListContinuationToken(relativePath)));
                            }
                            else
                            {
                                string segName       = fullPath.Remove(0, dirAbsolutePath.Length);
                                int    compareResult = string.Compare(segName, continuationTokenSeg, StringComparison.OrdinalIgnoreCase);

                                if (compareResult < 0)
                                {
                                    continue;
                                }

                                passedContinuationToken = true;

                                if (compareResult > 0)
                                {
                                    yield return(new AzureFileEntry(
                                                     relativePath,
                                                     cloudFile,
                                                     new AzureFileListContinuationToken(relativePath)));
                                }
                            }
                        }
                    }
                }while (continuationToken != null);

                if (checkFile)
                {
                    passedContinuationToken = true;
                }

                if (innerDirList.Count <= 0)
                {
                    if (!checkFile && !passedContinuationToken)
                    {
                        passedContinuationToken = true;
                    }
                }
                else
                {
                    while (innerDirList.Count > 0)
                    {
                        directoriesToList.Push(innerDirList.Pop());
                    }
                }
            }
        }
Example #21
0
        public async Task <List <string> > ListFolders(string shareName)
        {
            List <string> toReturn = new List <string>();

            try
            {
                CloudStorageAccount storageAccount  = CreateStorageAccountFromConnectionString(this.ConnectionString);
                CloudFileClient     cloudFileClient = storageAccount.CreateCloudFileClient();

                if (!string.IsNullOrEmpty(shareName))
                {
                    CloudFileShare cloudFileShare = cloudFileClient.GetShareReference(shareName);
                    await cloudFileShare.CreateIfNotExistsAsync();

                    CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
                    CloudFileDirectory fileDirectory = rootDirectory;
                    await fileDirectory.CreateIfNotExistsAsync();

                    List <IListFileItem>  results = new List <IListFileItem>();
                    FileContinuationToken token   = null;
                    do
                    {
                        FileResultSegment resultSegment = await fileDirectory.ListFilesAndDirectoriesSegmentedAsync(token);

                        results.AddRange(resultSegment.Results);
                        token = resultSegment.ContinuationToken;
                    }while (token != null);

                    foreach (var item in results)
                    {
                        if (item.GetType() == typeof(CloudFileDirectory))
                        {
                            CloudFileDirectory folder = (CloudFileDirectory)item;
                            toReturn.Add(folder.Name);
                        }
                    }
                }
                else
                {
                    List <CloudFileShare> results = new List <CloudFileShare>();
                    FileContinuationToken token   = null;
                    do
                    {
                        ShareResultSegment resultSegment = await cloudFileClient.ListSharesSegmentedAsync(token);

                        results.AddRange(resultSegment.Results);
                        token = resultSegment.ContinuationToken;
                    }while (token != null);

                    foreach (var item in results)
                    {
                        if (item.GetType() == typeof(CloudFileShare))
                        {
                            CloudFileShare share = (CloudFileShare)item;
                            toReturn.Add(share.Name);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.ErrorMessage = ex.ToString();
            }
            return(toReturn);
        }
        private async Task <IEnumerable <string> > ListFilesOrDirectoriesAsync(string folderPath, bool isDirectory)
        {
            if (!folderPath.StartsWith("/"))
            {
                throw new ArgumentException("First character on folderPath must be / (root).", nameof(folderPath));
            }
            string[] filePathSplitted = folderPath.Split("/");

            if (filePathSplitted.Length < 2)
            {
                throw new ArgumentException("FolderPath correct format is \"/{folder/s}\".", nameof(folderPath));
            }

            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference(filePathSplitted[1]);

            // Ensure that the share exists.
            if (true || await share.ExistsAsync().ConfigureAwait(false)) //Obviamos esta comprobaciĆ³n porque puede que no se tenga privilegios suficientes
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory selDir = share.GetRootDirectoryReference();
                if (!selDir.Exists())
                {
                    throw new Exception("Incorrect route path.");
                }
                // Get a reference to the directory we created previously.
                if (filePathSplitted.Length > 2)
                {
                    CloudFileDirectory tempDir = selDir.GetDirectoryReference(filePathSplitted[2]);
                    if (!tempDir.Exists())
                    {
                        throw new Exception("Incorrect route path.");
                    }
                    for (int i = 3; i < filePathSplitted.Length; i++)
                    {
                        tempDir = tempDir.GetDirectoryReference(filePathSplitted[i]);
                        if (!tempDir.Exists())
                        {
                            throw new Exception("Incorrect route path.");
                        }
                    }
                    selDir = tempDir;
                }

                List <IListFileItem>  results = new List <IListFileItem>();
                FileContinuationToken token   = null;
                do
                {
                    FileResultSegment resultSegment = await selDir.ListFilesAndDirectoriesSegmentedAsync(token);

                    results.AddRange(resultSegment.Results);
                    token = resultSegment.ContinuationToken;
                }while (token != null);
                if (isDirectory)
                {
                    return(results.Where(lfi => lfi is CloudFileDirectory).Select(lfi => ((CloudFileDirectory)lfi).Name));
                }
                else
                {
                    return(results.Where(lfi => lfi is CloudFile).Select(lfi => ((CloudFile)lfi).Name));
                }
            }
            else
            {
                throw new Exception("Share not found.");
            }
        }
Example #23
0
 public void Reset()
 {
     _continuationToken = null;
     _currentSegment    = null;
 }
Example #24
0
    public async void FileStorageTest()
    {
        ClearOutput();
        WriteLine("-- Testing File Storage --");

        WriteLine("0. Creating file client");

        // Create a file client for interacting with the file service.
        CloudFileClient fileClient = StorageAccount.CreateCloudFileClient();

        // Create a share for organizing files and directories within the storage account.
        WriteLine("1. Creating file share");
        CloudFileShare share = fileClient.GetShareReference(DemoShare);

        try
        {
            await share.CreateIfNotExistsAsync();
        }
        catch (StorageException)
        {
            WriteLine("Please make sure your storage account has storage file endpoint enabled and specified correctly in the app.config - then restart the sample.");
            throw;
        }

        // Get a reference to the root directory of the share.
        CloudFileDirectory root = share.GetRootDirectoryReference();

        // Create a directory under the root directory
        WriteLine("2. Creating a directory under the root directory");
        CloudFileDirectory dir = root.GetDirectoryReference(DemoDirectory);
        await dir.CreateIfNotExistsAsync();

        // Uploading a local file to the directory created above
        WriteLine("3. Uploading a file to directory");
        CloudFile file = dir.GetFileReference(ImageToUpload);

#if WINDOWS_UWP && ENABLE_DOTNET
        StorageFolder storageFolder = await StorageFolder.GetFolderFromPathAsync(Application.streamingAssetsPath.Replace('/', '\\'));

        StorageFile sf = await storageFolder.GetFileAsync(ImageToUpload);

        await file.UploadFromFileAsync(sf);
#else
        await file.UploadFromFileAsync(Path.Combine(Application.streamingAssetsPath, ImageToUpload));
#endif

        // List all files/directories under the root directory
        WriteLine("4. List Files/Directories in root directory");
        List <IListFileItem>  results = new List <IListFileItem>();
        FileContinuationToken token   = null;
        do
        {
            FileResultSegment resultSegment = await share.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(token);

            results.AddRange(resultSegment.Results);
            token = resultSegment.ContinuationToken;
        }while (token != null);

        // Print all files/directories listed above
        foreach (IListFileItem listItem in results)
        {
            // listItem type will be CloudFile or CloudFileDirectory
            WriteLine(string.Format("- {0} (type: {1})", listItem.Uri, listItem.GetType()));
        }

        // Download the uploaded file to your file system
        string path;
        WriteLine(string.Format("5. Download file from {0}", file.Uri.AbsoluteUri));
        string fileName = string.Format("CopyOf{0}", ImageToUpload);

#if WINDOWS_UWP && ENABLE_DOTNET
        storageFolder = ApplicationData.Current.TemporaryFolder;
        sf            = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

        path = sf.Path;
        await file.DownloadToFileAsync(sf);
#else
        path = Path.Combine(Application.temporaryCachePath, fileName);
        await file.DownloadToFileAsync(path, FileMode.Create);
#endif

        WriteLine("File written to " + path);

        // Clean up after the demo
        WriteLine("6. Delete file");
        await file.DeleteAsync();

        // When you delete a share it could take several seconds before you can recreate a share with the same
        // name - hence to enable you to run the demo in quick succession the share is not deleted. If you want
        // to delete the share uncomment the line of code below.
        WriteLine("7. Delete Share -- Note that it will take a few seconds before you can recreate a share with the same name");
        await share.DeleteAsync();

        WriteLine("-- Test Complete --");
    }
Example #25
0
        private async Task BasicAzureFileOperationsAsync()
        {
            const string DemoShare     = "demofileshare";
            const string DemoDirectory = "demofiledirectory";
            const string ImageToUpload = "HelloWorld.png";

            CloudStorageAccount storageAccount = CreateStorageAccountFromConnectionString(AppConfig.ConnectionString);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference(DemoShare);

            try
            {
                await share.CreateIfNotExistsAsync();
            }
            catch (StorageException)
            {
                Debug.WriteLine("Please make sure your storage account has storage file endpoint enabled and specified correctly in the app.config - then restart the sample.");
            }

            CloudFileDirectory root = share.GetRootDirectoryReference();

            Debug.WriteLine("2. Creating a directory under the root directory");
            CloudFileDirectory dir = root.GetDirectoryReference(DemoDirectory);
            await dir.CreateIfNotExistsAsync();

            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            var           realFile      = await storageFolder.CreateFileAsync(ImageToUpload, Windows.Storage.CreationCollisionOption.ReplaceExisting);

            Debug.WriteLine("3. Uploading a file to directory");
            CloudFile file = dir.GetFileReference(ImageToUpload);

            await file.UploadFromFileAsync(realFile);


            // List all files/directories under the root directory
            Debug.WriteLine("4. List Files/Directories in root directory");
            List <IListFileItem>  results = new List <IListFileItem>();
            FileContinuationToken token   = null;

            do
            {
                FileResultSegment resultSegment = await share.GetRootDirectoryReference().ListFilesAndDirectoriesSegmentedAsync(token);

                results.AddRange(resultSegment.Results);
                token = resultSegment.ContinuationToken;
            }while (token != null);

            // Print all files/directories listed above
            foreach (IListFileItem listItem in results)
            {
                // listItem type will be CloudFile or CloudFileDirectory
                Debug.WriteLine("- {0} (type: {1})", listItem.Uri, listItem.GetType());
            }

            // Download the uploaded file to your file system
            Debug.WriteLine("5. Download file from {0}", file.Uri.AbsoluteUri);
            var newFile = await storageFolder.CreateFileAsync(string.Format("./CopyOf{0}", ImageToUpload), Windows.Storage.CreationCollisionOption.ReplaceExisting);

            await file.DownloadToFileAsync(newFile);

            // Clean up after the demo
            Debug.WriteLine("6. Delete file");
            await file.DeleteAsync();

            // When you delete a share it could take several seconds before you can recreate a share with the same
            // name - hence to enable you to run the demo in quick succession the share is not deleted. If you want
            // to delete the share uncomment the line of code below.
            // Console.WriteLine("7. Delete Share");
            // await share.DeleteAsync();
        }
Example #26
0
        public async static Task Run([TimerTrigger("0 0 0 * * Sun"
            #if DEBUG
                                                   , RunOnStartup = true
            #endif
                                                   )] TimerInfo myTimer, ILogger log, ExecutionContext context)
        {
            log.LogInformation($"SimpleGhostBackup function started execution at: {DateTime.Now}");

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var clientId = config["ClientId"];

            if (String.IsNullOrEmpty(clientId))
            {
                throw new ArgumentNullException("ClientId is Required!");
            }

            var clientSecret = config["ClientSecret"];

            if (String.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentNullException("ClientSecret is Required!");
            }

            var blogUrl = config["BlogUrl"];

            if (String.IsNullOrEmpty(blogUrl))
            {
                throw new ArgumentNullException("BlogUrl is Required!");
            }

            var storageShareName = config["StorageShareName"];

            if (String.IsNullOrEmpty(storageShareName))
            {
                throw new ArgumentNullException("StorageShareName is Required!");
            }

            var storageConnection = config["StorageConnectionString"];

            if (String.IsNullOrEmpty(storageConnection))
            {
                throw new ArgumentNullException("storageConnection is Required!");
            }

            // Let get the number of snapshots that we should keep, default to last 4
            int maxSnapshots = 4;

            Int32.TryParse(config["MaxSnapshots"], out maxSnapshots);

            var client = new HttpClient(new HttpRetryMessageHandler(new HttpClientHandler()))
            {
                BaseAddress = new Uri(String.Format("https://{0}", blogUrl))
            };

            log.LogInformation($"Requesting Ghost Backup");
            var response = await client.PostAsync(String.Format("/ghost/api/v0.1/db/backup?client_id={0}&client_secret={1}", clientId, clientSecret), null);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                // Get our response content which contains the created backup file name
                var content = await response.Content.ReadAsStringAsync();

                var json = JObject.Parse(content);

                // Connect to our Azure Storage Account
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnection);
                CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
                CloudFileShare      share          = fileClient.GetShareReference(storageShareName);
                CloudFileDirectory  root           = share.GetRootDirectoryReference();
                CloudFileDirectory  data           = root.GetDirectoryReference("data");

                //Does the data folder exist
                if (await data.ExistsAsync())
                {
                    log.LogInformation($"Data folder exists.");

                    // get the backup file name
                    var       filename = System.IO.Path.GetFileName((string)json["db"][0]["filename"]);
                    CloudFile file     = data.GetFileReference(filename);

                    //Confirm that the backup file exists
                    if (await file.ExistsAsync())
                    {
                        // Create the snapshotg of the file share
                        log.LogInformation($"Backup file created - {filename}");
                        log.LogInformation($"Creating Azure Fileshare Snapshot");
                        var s = await share.SnapshotAsync();

                        if (s != null)
                        {
                            //Lets get all the current shares/snapshots
                            FileContinuationToken token = null;
                            var snapshots = new List <CloudFileShare>();
                            do
                            {
                                ShareResultSegment resultSegment = await fileClient.ListSharesSegmentedAsync(storageShareName, ShareListingDetails.Snapshots, 5, token, null, null);

                                snapshots.AddRange(resultSegment.Results);
                                token = resultSegment.ContinuationToken;
                            }while (token != null);

                            //lets delete the old ones
                            var toDelete = snapshots.Where(os => os.IsSnapshot).OrderByDescending(oos => oos.SnapshotTime).Skip(maxSnapshots).ToList();
                            foreach (var snapshot in toDelete)
                            {
                                try
                                {
                                    log.LogInformation($"Deleting snapshot - {snapshot.Name}, Created at {snapshot.SnapshotTime}");
                                    await snapshot.DeleteAsync();
                                }
                                catch (Exception ex)
                                {
                                    log.LogError($"Failed to delete snapshot - '{ex}'");
                                }
                            }
                        }
                    }
                }
            }

            log.LogInformation($"SimpleGhostBackup function ended execution at: {DateTime.Now}");
        }
        /// <summary>
        /// Constructs a web request to close one or more open handles for a file or directory.
        /// </summary>
        /// <param name="uri">The absolute URI to the file.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="shareSnapshot">A <see cref="DateTimeOffset"/> specifying the share snapshot timestamp, if the share is a snapshot.</param>
        /// <param name="handleId">ID of the handle to be closed, "*" if all should be closed.</param>
        /// <param name="recursive">Whether to recurse through this directory's subfiles and folders.</param>
        /// <param name="token">Continuation token for closing many handles.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <param name="operationContext">An <see cref="OperationContext" /> object for tracking the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static StorageRequestMessage CloseHandle(Uri uri, int?timeout, DateTimeOffset?shareSnapshot, string handleId, bool?recursive, FileContinuationToken token, AccessCondition accessCondition, HttpContent content, OperationContext operationContext, ICanonicalizer canonicalizer, StorageCredentials credentials)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            FileHttpRequestMessageFactory.AddShareSnapshot(builder, shareSnapshot);
            builder.Add(Constants.QueryConstants.Component, "forceclosehandles");

            if (token != null && token.NextMarker != null)
            {
                builder.Add(Constants.QueryConstants.Marker, token.NextMarker);
            }

            StorageRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Put, uri, timeout, builder, content, operationContext, canonicalizer, credentials);

            if (handleId != null)
            {
                request.AddOptionalHeader(Constants.HeaderConstants.HandleId, handleId);
            }

            if (recursive.HasValue)
            {
                request.AddOptionalHeader(Constants.HeaderConstants.Recursive, recursive.Value.ToString());
            }

            request.ApplyAccessCondition(accessCondition);

            return(request);
        }
Example #28
0
        private IEnumerable <TransferEntry> EnumerateLocationRecursive(CancellationToken cancellationToken)
        {
            string fullPrefix = null;

            if (null != this.baseDirectory)
            {
                fullPrefix = Uri.UnescapeDataString(this.baseDirectory.SnapshotQualifiedUri.AbsolutePath);
            }
            else
            {
                fullPrefix = Uri.UnescapeDataString(this.location.FileDirectory.SnapshotQualifiedUri.AbsolutePath);
            }

            // Normalize full prefix to end with slash.
            if (!string.IsNullOrEmpty(fullPrefix) && !fullPrefix.EndsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                fullPrefix += '/';
            }

            CloudFileDirectory directory = this.location.FileDirectory;

            Stack <CloudFileDirectory> innerDirList = new Stack <CloudFileDirectory>();

            FileContinuationToken continuationToken = null;
            bool passedContinuationToken            = false;

            if (null == this.listContinuationToken)
            {
                passedContinuationToken = true;
            }

            do
            {
                FileResultSegment resultSegment = null;
                Utils.CheckCancellation(cancellationToken);

                ErrorEntry errorEntry = null;

                try
                {
                    FileRequestOptions requestOptions = Transfer_RequestOptions.DefaultFileRequestOptions;
                    resultSegment = directory.ListFilesAndDirectoriesSegmentedAsync(
                        ListFilesSegmentSize,
                        continuationToken,
                        requestOptions,
                        null,
                        cancellationToken).Result;
                }
                catch (Exception ex)
                {
                    string errorMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.FailedToEnumerateDirectory,
                        directory.SnapshotQualifiedUri.AbsoluteUri,
                        string.Empty);

                    TransferException exception =
                        new TransferException(TransferErrorCode.FailToEnumerateDirectory, errorMessage, ex);
                    errorEntry = new ErrorEntry(exception);
                }

                if (null != errorEntry)
                {
                    yield return(errorEntry);

                    yield break;
                }

                continuationToken = resultSegment.ContinuationToken;

                foreach (IListFileItem fileItem in resultSegment.Results)
                {
                    Utils.CheckCancellation(cancellationToken);

                    if (fileItem is CloudFileDirectory)
                    {
                        CloudFileDirectory cloudDir = fileItem as CloudFileDirectory;

                        if (!passedContinuationToken)
                        {
                            if (string.Equals(cloudDir.Name, this.listContinuationToken.FilePath, StringComparison.Ordinal))
                            {
                                passedContinuationToken = true;
                                continue;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        string fullPath     = Uri.UnescapeDataString(cloudDir.SnapshotQualifiedUri.AbsolutePath);
                        string relativePath = fullPath.Remove(0, fullPrefix.Length);

                        yield return(new AzureFileDirectoryEntry(
                                         relativePath,
                                         cloudDir,
                                         new AzureFileListContinuationToken(cloudDir.Name)));
                    }
                    else if (fileItem is CloudFile)
                    {
                        CloudFile cloudFile = fileItem as CloudFile;

                        if (!passedContinuationToken)
                        {
                            if (string.Equals(cloudFile.Name, this.listContinuationToken.FilePath, StringComparison.Ordinal))
                            {
                                passedContinuationToken = true;
                                continue;
                            }
                            else
                            {
                                continue;
                            }
                        }

                        string fullPath     = Uri.UnescapeDataString(cloudFile.SnapshotQualifiedUri.AbsolutePath);
                        string relativePath = fullPath.Remove(0, fullPrefix.Length);

                        yield return(new AzureFileEntry(
                                         relativePath,
                                         cloudFile,
                                         new AzureFileListContinuationToken(cloudFile.Name)));
                    }
                }
            }while (continuationToken != null);
        }