Ejemplo n.º 1
0
        /*
         * BlobRoot = blob root name
         * BlobDirectoryReference = "directory name"
         * BlockBlobRef = "File name"
         *
         */
        private void DownloadFileFromBlob(string BlobRoot, string BlobDirectoryReference, string BlockBlobRef, string LocalFileName)
        {
            try
            {
                if (!AZStorage.CloudStorageAccount.TryParse(ConfigurationManager.ConnectionStrings["AzureBlobStorageConfigConnection"].ConnectionString, out AZStorage.CloudStorageAccount StorageAccountAZ))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);

                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobDirectoryReference);

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlockBlobRef);

                BlobBlock.DownloadToFile(LocalFileName, FileMode.OpenOrCreate);
            }
            catch (Exception ex)
            {
                throw new AzureTableBackupException(String.Format("Error downloading file '{0}'.", LocalFileName), ex);
            }
            finally
            {
            }
        }
Ejemplo n.º 2
0
        public static async Task <string> UploadFileToStorage(Stream fileStream, string fileName, TypeTable typeTable)
        {
            try
            {
                // Create storagecredentials object by reading the values from the configuration (appsettings.json)
                StorageCredentials storageCredentials = new StorageCredentials(accountName: CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountName,
                                                                               CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountKey);

                // Create cloudstorage account by passing the storagecredentials
                CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

                // Create the blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
                CloudBlobContainer container = blobClient.GetContainerReference(CloudStorage.Instance.GetAccountCloudStorage(typeTable).Container);
                container.CreateIfNotExists();

                // Get the reference to the block blob from the container
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
                string         ext       = System.IO.Path.GetExtension(fileName).ToLower();
                blockBlob.Properties.ContentType = MimeTypes.GetMimeType(ext);
                // Upload the file
                await blockBlob.UploadFromStreamAsync(fileStream);

                string sas = blockBlob.GetSharedAccessSignature(
                    new SharedAccessBlobPolicy()
                {
                    Permissions            = SharedAccessBlobPermissions.Read,
                    SharedAccessStartTime  = DateTime.Now.AddMinutes(-5),
                    SharedAccessExpiryTime = DateTime.Now.AddYears(999)
                });

                var url = blockBlob.Uri + sas;

                return(url);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
Ejemplo n.º 3
0
        public static void ClearStorage(TypeTable typeTable)
        {
            // Create storagecredentials object by reading the values from the configuration (appsettings.json)
            StorageCredentials storageCredentials = new StorageCredentials(accountName: CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountName,
                                                                           CloudStorage.Instance.GetAccountCloudStorage(typeTable).AccountKey);

            // Create cloudstorage account by passing the storagecredentials
            CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
            CloudBlobContainer container = blobClient.GetContainerReference(CloudStorage.Instance.GetAccountCloudStorage(typeTable).Container);

            if (container.Exists())
            {
                container.Delete();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create a blob file copy of the Azure Table specified.
        /// </summary>
        /// <param name="TableName">Name of Azure Table to backup.</param>
        /// <param name="BlobRoot">Name to use as blob root folder.</param>
        /// <param name="OutFileDirectory">Local directory (path) with authority to create/write a file.</param>
        /// <param name="Compress">True to compress the file.</param>
        /// <param name="Validate">True to validate the written record count matches what was queried.</param>
        /// <param name="RetentionDays">Process will age files in blob created more than x days ago.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <param name="filters">A list of Filter objects to be applied to table values extracted.</param>
        /// <returns>A string indicating the name of the blob file created as well as a count of how many files were aged.</returns>
        public string BackupTableToBlob(string TableName, string BlobRoot, string OutFileDirectory, bool Compress = false, bool Validate = false, int RetentionDays = 30, int TimeoutSeconds = 30, List <Filter> filters = default(List <Filter>))
        {
            string OutFileName     = "";
            string OutFileNamePath = "";
            int    BackupsAged     = 0;

            if (String.IsNullOrWhiteSpace(TableName))
            {
                throw new ParameterSpecException("TableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(BlobRoot))
            {
                throw new ParameterSpecException("BlobRoot is missing.");
            }

            if (String.IsNullOrWhiteSpace(OutFileDirectory))
            {
                throw new ParameterSpecException("OutFileDirectory is missing.");
            }

            try
            {
                OutFileName     = this.BackupTableToFile(TableName, OutFileDirectory, Compress, Validate, TimeoutSeconds, filters);
                OutFileNamePath = Path.Combine(OutFileDirectory, OutFileName);

                if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);
                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + TableName.ToLower());

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(OutFileName);
                BlobBlock.UploadFromFile(OutFileNamePath);

                DateTimeOffset OffsetTimeNow    = System.DateTimeOffset.Now;
                DateTimeOffset OffsetTimeRetain = System.DateTimeOffset.Now.AddDays(-1 * RetentionDays);

                //Cleanup old versions
                var BlobList = directory.ListBlobs().OfType <AZBlob.CloudBlockBlob>().ToList();;
                foreach (var blob in BlobList)
                {
                    if (blob.Properties.Created < OffsetTimeRetain)
                    {
                        try
                        {
                            blob.Delete();
                            BackupsAged++;
                        }
                        catch (Exception ex)
                        {
                            throw new AgingException(String.Format("Error aging file '{0}'.", blob.Name), ex);
                        }
                    }
                }

                return(String.Format("Table '{0}' backed up as '{2}' under blob '{3}\\{4}'; {1} files aged.", TableName, BackupsAged, OutFileName, BlobRoot, directory.ToString()));
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (Exception ex)
            {
                throw new BackupFailedException(String.Format("Table '{0}' backup failed.", TableName), ex);
            }
            finally
            {
                if ((!String.IsNullOrEmpty(OutFileNamePath)) && (File.Exists(OutFileNamePath)))
                {
                    try
                    {
                        File.Delete(OutFileNamePath);
                    }
                    catch (Exception ex)
                    {
                        throw new AzureTableBackupException(String.Format("Error cleaning up files '{0}'.", OutFileNamePath), ex);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Restore file created in blob storage by BackupAzureTables to the destination table name specified.
        /// Blob file will be downloaded to local storage before reading.  If compressed, it will be decompressed on local storage before reading.
        /// </summary>
        /// <param name="DestinationTableName">Name of the Azure Table to restore to -  may be different than name backed up originally.</param>
        /// <param name="OriginalTableName">Name of the Azure Table originally backed (required for determining blob directory to use)</param>
        /// <param name="BlobRoot">Name to use as blob root folder.</param>
        /// <param name="WorkingDirectory">Local directory (path) with authority to create/write a file.</param>
        /// <param name="BlobFileName">Name of the blob file to restore.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <returns>A string indicating the table restored and record count.</returns>
        public string RestoreTableFromBlob(string DestinationTableName, string OriginalTableName, string BlobRoot, string WorkingDirectory, string BlobFileName, int TimeoutSeconds = 30)
        {
            string result = "Error";

            if (String.IsNullOrWhiteSpace(DestinationTableName))
            {
                throw new ParameterSpecException("DestinationTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(OriginalTableName))
            {
                throw new ParameterSpecException("OriginalTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(WorkingDirectory))
            {
                throw new ParameterSpecException("WorkingDirectory is missing.");
            }

            if (!Directory.Exists(WorkingDirectory))
            {
                throw new ParameterSpecException("WorkingDirectory does not exist.");
            }

            if (String.IsNullOrWhiteSpace(BlobFileName))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobFileName '{0}' specified.", BlobFileName));
            }
            bool Decompress = BlobFileName.EndsWith(".7z");

            if (String.IsNullOrWhiteSpace(BlobRoot))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobRoot '{0}' specified.", BlobRoot));
            }

            if (Path.GetFullPath(WorkingDirectory) != WorkingDirectory)
            {
                throw new ParameterSpecException(String.Format("Invalid WorkingDirectory '{0}' specified.", WorkingDirectory));
            }

            if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ))
            {
                throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
            }

            try
            {
                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);
                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + OriginalTableName.ToLower());

                string WorkingFileNamePath           = Path.Combine(WorkingDirectory, BlobFileName);
                string WorkingFileNamePathCompressed = Path.Combine(WorkingDirectory, BlobFileName);

                /*
                 * If file is compressed, WorkingFileNamePath will be set to .txt
                 * If file is not compressed WorkingFileNamePathCompressed will be left as .txt
                 */
                if (Decompress)
                {
                    WorkingFileNamePath = WorkingFileNamePath.Replace(".7z", ".txt");
                }
                else
                {
                    //WorkingFileNamePathCompressed = WorkingFileNamePathCompressed.Replace(".txt", ".7z");
                }

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlobFileName);
                BlobBlock.DownloadToFile(WorkingFileNamePathCompressed, FileMode.Create);

                //https://www.tutorialspoint.com/compressing-and-decompressing-files-using-gzip-format-in-chash
                if (Decompress)
                {
                    FileStream FileToDeCompress = File.OpenRead(WorkingFileNamePathCompressed);
                    using (FileStream OutFileDecompressed = new FileStream(WorkingFileNamePath, FileMode.Create))
                    {
                        using (var zip = new GZipStream(FileToDeCompress, CompressionMode.Decompress, true))
                        {
                            byte[] buffer = new byte[FileToDeCompress.Length];
                            while (true)
                            {
                                int count = zip.Read(buffer, 0, buffer.Length);
                                if (count != 0)
                                {
                                    OutFileDecompressed.Write(buffer, 0, count);
                                }
                                if (count != buffer.Length)
                                {
                                    break;
                                }
                            }
                        }
                        FileToDeCompress.Close();
                        OutFileDecompressed.Close();
                    }
                }

                result = RestoreTableFromFile(DestinationTableName, WorkingFileNamePath, TimeoutSeconds);
                // Cleanup files
                if (File.Exists(WorkingFileNamePath))
                {
                    File.Delete(WorkingFileNamePath);
                }
                if (File.Exists(WorkingFileNamePathCompressed))
                {
                    File.Delete(WorkingFileNamePathCompressed);
                }
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (Exception ex)
            {
                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
            }
            finally
            {
            }
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Restore file from blob storage by BackupAzureTables to the destination table name specified.
        /// File will be read directly from blob storage.  If the file is compressed, it will be decompressed to blob storage and then read.
        /// </summary>
        /// <param name="DestinationTableName">Name of the Azure Table to restore to -  may be different than name backed up originally.</param>
        /// <param name="OriginalTableName">Name of the Azure Table originally backed (required for determining blob directory to use)</param>
        /// <param name="BlobRoot">Name to use as blob root folder.</param>
        /// <param name="BlobFileName">Name of the blob file to restore.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <returns>A string indicating the table restored and record count.</returns>
        public string RestoreTableFromBlobDirect(string DestinationTableName, string OriginalTableName, string BlobRoot, string BlobFileName, int TimeoutSeconds = 30)
        {
            if (String.IsNullOrWhiteSpace(DestinationTableName))
            {
                throw new ParameterSpecException("DestinationTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(OriginalTableName))
            {
                throw new ParameterSpecException("OriginalTableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(BlobFileName))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobFileName '{0}' specified.", BlobFileName));
            }
            bool   Decompress   = BlobFileName.EndsWith(".7z");
            string TempFileName = String.Format("{0}.temp", BlobFileName);

            if (String.IsNullOrWhiteSpace(BlobRoot))
            {
                throw new ParameterSpecException(String.Format("Invalid BlobRoot '{0}' specified.", BlobRoot));
            }

            try
            {
                if (!CosmosTable.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureTableConnectionSpec).Password, out CosmosTable.CloudStorageAccount StorageAccount))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);
                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + OriginalTableName.ToLower());

                // If file is compressed, Decompress to a temp file in the blob
                if (Decompress)
                {
                    AZBlob.CloudBlockBlob BlobBlockTemp = directory.GetBlockBlobReference(TempFileName);
                    AZBlob.CloudBlockBlob BlobBlockRead = directory.GetBlockBlobReference(BlobFileName);

                    using (AZBlob.CloudBlobStream decompressedStream = BlobBlockTemp.OpenWrite())
                    {
                        using (Stream readstream = BlobBlockRead.OpenRead())
                        {
                            using (var zip = new GZipStream(readstream, CompressionMode.Decompress, true))
                            {
                                zip.CopyTo(decompressedStream);
                            }
                        }
                    }
                    BlobFileName = TempFileName;
                }

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(BlobFileName);

                CosmosTable.CloudTableClient client    = CosmosTable.CloudStorageAccountExtensions.CreateCloudTableClient(StorageAccount, new CosmosTable.TableClientConfiguration());
                CosmosTable.CloudTable       TableDest = client.GetTableReference(DestinationTableName);
                TableDest.ServiceClient.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 0, TimeoutSeconds);
                TableDest.CreateIfNotExists();

                using (Stream BlobStream = BlobBlock.OpenRead())
                {
                    using (StreamReader InputFileStream = new StreamReader(BlobStream))
                    {
                        string result = RestoreFromStream(InputFileStream, TableDest, DestinationTableName);
                        if (Decompress)
                        {
                            AZBlob.CloudBlockBlob BlobBlockTemp = directory.GetBlockBlobReference(TempFileName);
                            BlobBlockTemp.DeleteIfExists();
                        }
                        return(result);
                    }
                }
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (RestoreFailedException rex)
            {
                throw rex;
            }
            catch (Exception ex)
            {
                throw new RestoreFailedException(String.Format("Table '{0}' restore failed.", DestinationTableName), ex);
            }
            finally
            {
            }
        } // RestoreTableFromBlobDirect
        /// <summary>
        /// Backup table directly to Blob.
        ///
        /// </summary>
        /// <param name="TableName">Name of Azure Table to backup.</param>
        /// <param name="BlobRoot">Name to use as blob root folder.</param>
        /// <param name="Compress">True to compress the file.</param>
        /// <param name="RetentionDays">Process will age files in blob created more than x days ago.</param>
        /// <param name="TimeoutSeconds">Set timeout for table client.</param>
        /// <param name="filters">A list of Filter objects to be applied to table values extracted.</param>
        /// <returns>A string containing the name of the file created.</returns>
        public string BackupTableToBlobDirect(string TableName, string BlobRoot, bool Compress = false, int RetentionDays = 30, int TimeoutSeconds = 30, List <Filter> filters = default(List <Filter>))
        {
            string OutFileName = "";
            int    RecordCount = 0;
            int    BackupsAged = 0;

            if (String.IsNullOrWhiteSpace(TableName))
            {
                throw new ParameterSpecException("TableName is missing.");
            }

            if (String.IsNullOrWhiteSpace(BlobRoot))
            {
                throw new ParameterSpecException("BlobRoot is missing.");
            }

            try
            {
                if (Compress)
                {
                    OutFileName = String.Format(TableName + "_Backup_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt.7z");
                }
                else
                {
                    OutFileName = String.Format(TableName + "_Backup_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt");
                }

                if (!CosmosTable.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureTableConnectionSpec).Password, out CosmosTable.CloudStorageAccount StorageAccount))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                if (!AZStorage.CloudStorageAccount.TryParse(new System.Net.NetworkCredential("", AzureBlobConnectionSpec).Password, out AZStorage.CloudStorageAccount StorageAccountAZ))
                {
                    throw new ConnectionException("Can not connect to CloudStorage Account.  Verify connection string.");
                }

                AZBlob.CloudBlobClient ClientBlob = AZBlob.BlobAccountExtensions.CreateCloudBlobClient(StorageAccountAZ);
                var container = ClientBlob.GetContainerReference(BlobRoot);
                container.CreateIfNotExists();
                AZBlob.CloudBlobDirectory directory = container.GetDirectoryReference(BlobRoot.ToLower() + "-table-" + TableName.ToLower());

                AZBlob.CloudBlockBlob BlobBlock = directory.GetBlockBlobReference(OutFileName);

                // start upload from stream, iterate through table, possible inline compress
                try
                {
                    CosmosTable.CloudTableClient client = CosmosTable.CloudStorageAccountExtensions.CreateCloudTableClient(StorageAccount, new CosmosTable.TableClientConfiguration());
                    CosmosTable.CloudTable       table  = client.GetTableReference(TableName);
                    table.ServiceClient.DefaultRequestOptions.ServerTimeout = new TimeSpan(0, 0, TimeoutSeconds);

                    CosmosTable.TableContinuationToken token = null;
                    var entities           = new List <CosmosTable.DynamicTableEntity>();
                    var entitiesSerialized = new List <string>();
                    DynamicTableEntityJsonSerializer serializer = new DynamicTableEntityJsonSerializer();

                    TableSpec TableSpecStart = new TableSpec(TableName);
                    var       NewLineAsBytes = Encoding.UTF8.GetBytes("\n");

                    var tempTableSpecStart     = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(TableSpecStart));
                    AZBlob.CloudBlobStream bs2 = BlobBlock.OpenWrite();
                    Stream bs = BlobBlock.OpenWrite();

                    if (Compress)
                    {
                        bs = new GZipStream(bs2, CompressionMode.Compress);
                    }
                    else
                    {
                        bs = bs2;
                    }

                    bs.Write(tempTableSpecStart, 0, tempTableSpecStart.Length);
                    bs.Flush();
                    bs.Write(NewLineAsBytes, 0, NewLineAsBytes.Length);
                    bs.Flush();

                    CosmosTable.TableQuery <CosmosTable.DynamicTableEntity> tq;
                    if (default(List <Filter>) == filters)
                    {
                        tq = new CosmosTable.TableQuery <CosmosTable.DynamicTableEntity>();
                    }
                    else
                    {
                        tq = new CosmosTable.TableQuery <CosmosTable.DynamicTableEntity>().Where(Filter.BuildFilterSpec(filters));
                    }
                    do
                    {
                        var queryResult = table.ExecuteQuerySegmented(tq, token);
                        foreach (CosmosTable.DynamicTableEntity dte in queryResult.Results)
                        {
                            var tempDTE = Encoding.UTF8.GetBytes(serializer.Serialize(dte));
                            bs.Write(tempDTE, 0, tempDTE.Length);
                            bs.Flush();
                            bs.Write(NewLineAsBytes, 0, NewLineAsBytes.Length);
                            bs.Flush();
                            RecordCount++;
                        }
                        token = queryResult.ContinuationToken;
                    } while (token != null);

                    TableSpec TableSpecEnd     = new TableSpec(TableName, RecordCount);
                    var       tempTableSpecEnd = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(TableSpecEnd));
                    bs.Write(tempTableSpecEnd, 0, tempTableSpecEnd.Length);
                    bs.Flush();
                    bs.Write(NewLineAsBytes, 0, NewLineAsBytes.Length);
                    bs.Flush();
                    bs.Close();
                }
                catch (Exception ex)
                {
                    throw new BackupFailedException(String.Format("Table '{0}' backup failed.", TableName), ex);
                }

                DateTimeOffset OffsetTimeNow    = System.DateTimeOffset.Now;
                DateTimeOffset OffsetTimeRetain = System.DateTimeOffset.Now.AddDays(-1 * RetentionDays);

                //Cleanup old versions
                var BlobList = directory.ListBlobs().OfType <AZBlob.CloudBlockBlob>().ToList();;
                foreach (var blob in BlobList)
                {
                    if (blob.Properties.Created < OffsetTimeRetain)
                    {
                        try
                        {
                            blob.Delete();
                            BackupsAged++;
                        }
                        catch (Exception ex)
                        {
                            throw new AgingException(String.Format("Error aging file '{0}'.", blob.Name), ex);
                        }
                    }
                }

                return(String.Format("Table '{0}' backed up as '{2}' under blob '{3}\\{4}'; {1} files aged.", TableName, BackupsAged, OutFileName, BlobRoot, directory.ToString()));
            }
            catch (ConnectionException cex)
            {
                throw cex;
            }
            catch (Exception ex)
            {
                throw new BackupFailedException(String.Format("Table '{0}' backup failed.", TableName), ex);
            }
            finally
            {
            }
        } // BackupTableToBlobDirect
Ejemplo n.º 8
0
        public void CloudBlobDirectoryFlatListingAPM()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    container.Create();
                    if (CloudBlobDirectorySetupWithDelimiter(container, delimiter))
                    {
                        using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                        {
                            IAsyncResult          result;
                            BlobContinuationToken token     = null;
                            CloudBlobDirectory    directory = container.GetDirectoryReference("TopDir1");
                            List <IListBlobItem>  list1     = new List <IListBlobItem>();
                            do
                            {
                                result = directory.BeginListBlobsSegmented(token, ar => waitHandle.Set(), null);
                                waitHandle.WaitOne();
                                BlobResultSegment result1 = directory.EndListBlobsSegmented(result);
                                list1.AddRange(result1.Results);
                                token = result1.ContinuationToken;
                            }while (token != null);

                            Assert.IsTrue(list1.Count == 3);

                            IListBlobItem item11 = list1.ElementAt(0);
                            Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                            IListBlobItem item12 = list1.ElementAt(1);
                            Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                            IListBlobItem item13 = list1.ElementAt(2);
                            Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));

                            CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13;

                            List <IListBlobItem> list2 = new List <IListBlobItem>();
                            do
                            {
                                result = midDir2.BeginListBlobsSegmented(true, BlobListingDetails.None, null, token, null, null, ar => waitHandle.Set(), null);
                                waitHandle.WaitOne();
                                BlobResultSegment result2 = midDir2.EndListBlobsSegmented(result);
                                list2.AddRange(result2.Results);
                                token = result2.ContinuationToken;
                            }while (token != null);

                            Assert.IsTrue(list2.Count == 2);

                            IListBlobItem item41 = list2.ElementAt(0);
                            Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                            IListBlobItem item42 = list2.ElementAt(1);
                            Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2"));
                        }
                    }
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }
Ejemplo n.º 9
0
        public void CloudBlobDirectoryFlatListing()
        {
            foreach (String delimiter in Delimiters)
            {
                CloudBlobClient client = GenerateCloudBlobClient();
                client.DefaultDelimiter = delimiter;
                string             name      = GetRandomContainerName();
                CloudBlobContainer container = client.GetContainerReference(name);

                try
                {
                    container.Create();
                    if (CloudBlobDirectorySetupWithDelimiter(container, delimiter))
                    {
                        IEnumerable <IListBlobItem> list1 = container.ListBlobs("TopDir1" + delimiter, false, BlobListingDetails.None, null, null);

                        List <IListBlobItem> simpleList1 = list1.ToList();
                        ////Check if for 3 because if there were more than 3, the previous assert would have failed.
                        ////So the only thing we need to make sure is that it is not less than 3.
                        Assert.IsTrue(simpleList1.Count == 3);

                        IListBlobItem item11 = simpleList1.ElementAt(0);
                        Assert.IsTrue(item11.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "Blob1"));

                        IListBlobItem item12 = simpleList1.ElementAt(1);
                        Assert.IsTrue(item12.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter));

                        IListBlobItem item13 = simpleList1.ElementAt(2);
                        Assert.IsTrue(item13.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter));
                        CloudBlobDirectory midDir2 = (CloudBlobDirectory)item13;

                        IEnumerable <IListBlobItem> list2 = container.ListBlobs("TopDir1" + delimiter + "MidDir1", true, BlobListingDetails.None, null, null);

                        List <IListBlobItem> simpleList2 = list2.ToList();
                        Assert.IsTrue(simpleList2.Count == 2);

                        IListBlobItem item21 = simpleList2.ElementAt(0);
                        Assert.IsTrue(item21.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                        IListBlobItem item22 = simpleList2.ElementAt(1);
                        Assert.IsTrue(item22.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter + "EndBlob2"));

                        IEnumerable <IListBlobItem> list3 = container.ListBlobs("TopDir1" + delimiter + "MidDir1" + delimiter, false, BlobListingDetails.None, null, null);

                        List <IListBlobItem> simpleList3 = list3.ToList();
                        Assert.IsTrue(simpleList3.Count == 2);

                        IListBlobItem item31 = simpleList3.ElementAt(0);
                        Assert.IsTrue(item31.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir1" + delimiter));

                        IListBlobItem item32 = simpleList3.ElementAt(1);
                        Assert.IsTrue(item32.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir1" + delimiter + "EndDir2" + delimiter));

                        IEnumerable <IListBlobItem> list4 = midDir2.ListBlobs(true);

                        List <IListBlobItem> simpleList4 = list4.ToList();
                        Assert.IsTrue(simpleList4.Count == 2);

                        IListBlobItem item41 = simpleList4.ElementAt(0);
                        Assert.IsTrue(item41.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir1" + delimiter + "EndBlob1"));

                        IListBlobItem item42 = simpleList4.ElementAt(1);
                        Assert.IsTrue(item42.Uri.Equals(container.Uri + "/TopDir1" + delimiter + "MidDir2" + delimiter + "EndDir2" + delimiter + "EndBlob2"));
                    }
                }
                finally
                {
                    container.DeleteIfExists();
                }
            }
        }