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 <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 <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); }
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}"); }