public async Task DownloadFile()
        {
            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())
                {
                    do
                    {
                        FileResultSegment resultSegment = await sampleDir.ListFilesAndDirectoriesSegmentedAsync(token);

                        token = resultSegment.ContinuationToken;

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

                        foreach (IListFileItem listResultItem in resultSegment.Results)
                        {
                            var cloudFile = sampleDir.GetFileReference(listResultItem.Uri.ToString());
                            Console.WriteLine(cloudFile.Uri.ToString());
                            //await cloudFile.DownloadToFileAsync(cloudFile.Uri.ToString(), FileMode.Create);
                        }
                    }while (token != null);
                }
            }
        }
        public static async Task <List <CloudFileShare> > ListFileSharesAsync(this CloudFileClient client)
        {
            FileContinuationToken continuationToken = null;
            List <CloudFileShare> results           = new List <CloudFileShare>();

            do
            {
                var response = await client.ListSharesSegmentedAsync(continuationToken);

                continuationToken = response.ContinuationToken;
                results.AddRange(response.Results);
            }while (continuationToken != null);
            return(results);
        }
示例#3
0
        public static async Task <List <CloudFileShare> > ListFileSharesAsync(string account, string key)
        {
            CloudFileClient       fileClient        = Client.GetFileShareClient(account, key);
            FileContinuationToken continuationToken = null;
            List <CloudFileShare> results           = new List <CloudFileShare>();

            do
            {
                var response = await fileClient.ListSharesSegmentedAsync(continuationToken);

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

            return(results);
        }
        public async Task Test_10_QueryForShares()
        {
            var shares = new List <CloudFileShare>();

            FileContinuationToken fct = null;

            do
            {
                var srs = await _client.ListSharesSegmentedAsync(fct);

                fct = srs.ContinuationToken;

                shares.AddRange(srs.Results);
            }while (fct != null);

            Check.That(shares.Count).IsEqualTo(1);
        }
示例#5
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);
            }
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
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}");
        }
示例#9
0
        public async Task RunPermissionsTestFiles(SharedAccessAccountPolicy policy)
        {
            CloudFileClient fileClient = GenerateCloudFileClient();
            string          shareName  = "s" + Guid.NewGuid().ToString("N");

            try
            {
                CloudStorageAccount account           = new CloudStorageAccount(fileClient.Credentials, false);
                string              accountSASToken   = account.GetSharedAccessSignature(policy);
                StorageCredentials  accountSAS        = new StorageCredentials(accountSASToken);
                CloudStorageAccount accountWithSAS    = new CloudStorageAccount(accountSAS, null, null, null, fileClient.StorageUri);
                CloudFileClient     fileClientWithSAS = accountWithSAS.CreateCloudFileClient();
                CloudFileShare      shareWithSAS      = fileClientWithSAS.GetShareReference(shareName);
                CloudFileShare      share             = fileClient.GetShareReference(shareName);

                // General pattern - If current perms support doing a thing with SAS, do the thing with SAS and validate with shared
                // Otherwise, make sure SAS fails and then do the thing with shared key.

                // Things to do:
                // Create the share (Create / Write perms, Container RT)
                // List shares with prefix (List perms, Service RT)
                // Create a new file (Create / Write, Object RT)
                // Add a range to the file (Write, Object RT)
                // Read the data from the file (Read, Object RT)
                // Overwrite a file (Write, Object RT)
                // Delete the file (Delete perms, Object RT)

                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Container) == SharedAccessAccountResourceTypes.Container))
                {
                    await shareWithSAS.CreateAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await shareWithSAS.CreateAsync(), "Creating a share with SAS should fail without Create or Write and Container-level perms.");

                    await share.CreateAsync();
                }
                Assert.IsTrue(await share.ExistsAsync());

                if (((policy.Permissions & SharedAccessAccountPermissions.List) == SharedAccessAccountPermissions.List) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Service) == SharedAccessAccountResourceTypes.Service))
                {
                    Assert.AreEqual(shareName, (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First().Name);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => (await fileClientWithSAS.ListSharesSegmentedAsync(shareName, null)).Results.First(), "Listing shared with SAS should fail without List and Service-level perms.");
                }

                string    filename    = "fileName";
                CloudFile fileWithSAS = shareWithSAS.GetRootDirectoryReference().GetFileReference(filename);
                CloudFile file        = share.GetRootDirectoryReference().GetFileReference(filename);

                //Try creating credentials using SAS Uri directly
                CloudFile fileWithSASUri = new CloudFile(new Uri(share.Uri + accountSASToken));

                byte[] content = new byte[] { 0x1, 0x2, 0x3, 0x4 };
                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.CreateAsync(content.Length);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(content.Length), "Creating a file with SAS should fail without Create or Write and Object-level perms.");

                    await file.CreateAsync(content.Length);
                }
                Assert.IsTrue(await file.ExistsAsync());

                using (MemoryStream stream = new MemoryStream(content))
                {
                    if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) &&
                        ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                    {
                        await fileWithSAS.WriteRangeAsync(stream, 0, null);
                    }
                    else
                    {
                        await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.WriteRangeAsync(stream, 0, null), "Writing a range to a file with SAS should fail without Write and Object-level perms.");

                        stream.Seek(0, SeekOrigin.Begin);
                        await file.WriteRangeAsync(stream, 0, null);
                    }
                }

                byte[] result = new byte[content.Length];
                await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                for (int i = 0; i < content.Length; i++)
                {
                    Assert.AreEqual(content[i], result[i]);
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Read) == SharedAccessAccountPermissions.Read) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    result = new byte[content.Length];
                    await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                    for (int i = 0; i < content.Length; i++)
                    {
                        Assert.AreEqual(content[i], result[i]);
                    }
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length), "Reading a file with SAS should fail without Read and Object-level perms.");
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.CreateAsync(2);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.CreateAsync(2), "Overwriting a file with SAS should fail without Write and Object-level perms.");

                    await file.CreateAsync(2);
                }

                result = new byte[content.Length];
                await file.DownloadRangeToByteArrayAsync(result, 0, 0, content.Length);

                for (int i = 0; i < content.Length; i++)
                {
                    Assert.AreEqual(0, result[i]);
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Delete) == SharedAccessAccountPermissions.Delete) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await fileWithSAS.DeleteAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await fileWithSAS.DeleteAsync(), "Deleting a file with SAS should fail without Delete and Object-level perms.");

                    await file.DeleteAsync();
                }

                Assert.IsFalse(await file.ExistsAsync());
            }
            finally
            {
                fileClient.GetShareReference(shareName).DeleteIfExistsAsync().Wait();
            }
        }