Ejemplo n.º 1
0
        public void ValidateShareCreateableWithSasToken(CloudFileShare share, string sastoken)
        {
            Test.Info("Verify share create permission");
            string fileName   = Utility.GenNameString("file");
            string dirName    = Utility.GenNameString("dir");
            int    fileLength = 10;

            CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(share.ServiceClient.Credentials.AccountName, sastoken);
            CloudFileShare      sasShare   = sasAccount.CreateCloudFileClient().GetShareReference(share.Name);

            if (!share.Exists())
            {
                sasShare.Create();
                Test.Assert(sasShare.Exists(), "The share should  exist after Creating with sas token");
            }
            CloudFileDirectory dir    = share.GetRootDirectoryReference().GetDirectoryReference(dirName);
            CloudFileDirectory sasDir = sasShare.GetRootDirectoryReference().GetDirectoryReference(dirName);

            sasDir.Create();
            Test.Assert(dir.Exists(), "The directory should  exist after Creating with sas token");
            CloudFile file    = dir.GetFileReference(fileName);
            CloudFile sasFile = sasDir.GetFileReference(fileName);

            sasFile.Create(fileLength);
            Test.Assert(file.Exists(), "The file should  exist after Creating with sas token");
            TestBase.ExpectEqual(fileLength, file.Properties.Length, "File Lenth should match.");
        }
Ejemplo n.º 2
0
        private void WriteLogLine(WriteWay writeWay, string writeLogLine, params string[] logFilePath)
        {
            if (logFilePath.Length < 2)
            {
                Console.WriteLine(invalidExistLogFilePath);
                return;
            }

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                string.Format(@"DefaultEndpointsProtocol=https;AccountName={0};
                AccountKey={1};EndpointSuffix=core.windows.net",
                              Constant.LOGGER_ACCOUNT_NAME, Constant.Instance.StorageAccountKey));
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference(logFilePath[0]);

            if (!share.Exists())
            {
                share.Create();
            }
            CloudFileDirectory sampleDir = share.GetRootDirectoryReference();

            for (int i = 1; i < logFilePath.Length - 1; i++)
            {
                CloudFileDirectory nextLevelDir = sampleDir.GetDirectoryReference("TestLogs");
                if (!sampleDir.Exists())
                {
                    sampleDir.Create();
                }
                sampleDir = nextLevelDir;
            }

            CloudFile file = sampleDir.GetFileReference(logFilePath[logFilePath.Length - 1]);

            string writenLineContent = "";

            if (file.Exists())
            {
                if (writeWay == WriteWay.Cover)
                {
                }
                else if (writeWay == WriteWay.Append)
                {
                    writenLineContent = file.DownloadTextAsync().Result;
                }
            }
            file.UploadText(writenLineContent + writeLogLine + "\n");
        }
Ejemplo n.º 3
0
        /*
         * GetAppendBlobReference is removed from Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer
         * So we cannot release the log function based on the append function from Microsoft.WindowsAzure.Storage.
         * stackoverflow: https://stackoverflow.com/questions/48411359/getappendblobreference-is-removed-from-microsoft-windowsazure-storage-blob-cloud
         */
        public static void MainMethod()
        {
            string accountName = "";
            string accountKey  = "";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(string.Format(@"DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, accountKey));
            // Create a CloudFileClient object for credentialed access to Azure Files.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference("logs");

            // If the share does not exist, create it.
            if (!share.Exists())
            {
                share.Create();
            }
            // Get a reference to the root directory for the share.
            CloudFileDirectory rootDir = share.GetRootDirectoryReference();

            // Get a reference to the directory we created previously.
            CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("TestLogs");

            if (!sampleDir.Exists())
            {
                sampleDir.Create();
            }

            // Get a reference to the file we created previously.
            CloudFile file = sampleDir.GetFileReference("Log1.txt");

            // Ensure that the file exists.
            if (file.Exists())
            {
                // Write the contents of the file to the console window.
                Console.WriteLine(file.DownloadTextAsync().Result);
                file.UploadText("12345");
                Console.WriteLine(file.DownloadTextAsync().Result);
            }
            else
            {
                file.UploadText("1234");
            }

            Console.ReadKey();
        }
Ejemplo n.º 4
0
        public void ValidateShareCreatableWithSasToken(string shareName, string accountName, string sastoken)
        {
            Test.Info("Verify share create permission");
            CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(accountName, sastoken);

            //make sure the share not exist before create
            CloudFileShare sasShareReference = client.GetShareReference(shareName);

            if (sasShareReference.Exists())
            {
                sasShareReference.Delete();
                Thread.Sleep(2 * 60 * 1000); // Sleep 2 minutes to make sure the share can be created successfully
            }

            //Create Share with SAS
            CloudFileShare sasShare = sasAccount.CreateCloudFileClient().GetShareReference(shareName);

            sasShare.Create();

            //Verify and delete share
            Test.Assert(sasShareReference.Exists(), "The Share {0} should exist.", shareName);
            sasShareReference.Delete();
        }
Ejemplo n.º 5
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Music Manager setup app. Setup the file shares and database necessary for MusicManager");
            string input = "";

            while (input != "5")
            {
                Console.WriteLine("1. Setup local file share");
                Console.WriteLine("2. Setup azure file share");
                Console.WriteLine("3. Setup azure sql database");
                Console.WriteLine("4. Setup azure cosmos db");
                Console.WriteLine("5. Exit");
                input = Console.ReadLine();

                if (input == "1")
                {
                    Console.Write("File Path: ");
                    string path = Console.ReadLine();
                    if (File.Exists(path))
                    {
                        Console.WriteLine("File at " + path + " already exists");
                    }
                    else
                    {
                        try
                        {
                            using (FileStream fs = File.Create(path))
                            {
                                byte[] info = new UTF8Encoding(true).GetBytes("[]");
                                // Add some information to the file.
                                fs.Write(info, 0, info.Length);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Failed to create file: " + e.Message);
                        }
                    }
                }
                else if (input == "2")
                {
                    Console.WriteLine("Enter azure files connection string");
                    string connectionString = Console.ReadLine();
                    Console.WriteLine("Enter azure files share name");
                    string shareName = Console.ReadLine();
                    Console.WriteLine("FileName");
                    string fileName = Console.ReadLine();

                    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
                    CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
                    CloudFileShare      share          = fileClient.GetShareReference(shareName);

                    if (share.Exists())
                    {
                        Console.WriteLine("Share already exists");
                    }
                    else
                    {
                        try
                        {
                            share.Create();

                            CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                            CloudFile          file    = rootDir.GetFileReference(fileName);
                            await file.UploadTextAsync("[]");
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Failed to create azure file share: " + e.Message);
                        }
                    }
                }

                else if (input == "3")
                {
                    Console.WriteLine("Enter sql connection string");
                    string connectionString = Console.ReadLine();
                    using (SqlConnection conn = new SqlConnection(connectionString))
                    {
                        using (SqlCommand command = conn.CreateCommand())
                        {
                            string sql = File.ReadAllText(@".\create_db_objects\create_table.sql");
                            command.Connection.Open();
                            command.CommandType = System.Data.CommandType.Text;
                            command.CommandText = sql;

                            await command.ExecuteNonQueryAsync();

                            sql = File.ReadAllText(@".\create_db_objects\addAlbum.sql");
                            command.CommandText = sql;
                            await command.ExecuteNonQueryAsync();

                            sql = File.ReadAllText(@".\create_db_objects\editAlbum.sql");
                            command.CommandText = sql;
                            await command.ExecuteNonQueryAsync();

                            sql = File.ReadAllText(@".\create_db_objects\getAlbum.sql");
                            command.CommandText = sql;
                            await command.ExecuteNonQueryAsync();

                            sql = File.ReadAllText(@".\create_db_objects\getAlbums.sql");
                            command.CommandText = sql;
                            await command.ExecuteNonQueryAsync();

                            sql = File.ReadAllText(@".\create_db_objects\deleteAlbum.sql");
                            command.CommandText = sql;
                            await command.ExecuteNonQueryAsync();
                        }
                    }
                }

                else if (input == "4")
                {
                    Console.WriteLine("Azure cosmos db endpoint");
                    string endpoint = Console.ReadLine();
                    Console.WriteLine("Azure cosmos db Primary Key");
                    string primaryKey = Console.ReadLine();
                    Console.WriteLine("Azure cosmos db database id");
                    string databaseId = Console.ReadLine();
                    Console.WriteLine("Azure cosmos db container id");
                    string containerId = Console.ReadLine();

                    CosmosClient cosmosClient = new CosmosClient(endpoint, primaryKey);

                    //var db = cosmosClient.GetDatabase(databaseId);

                    try
                    {
                        Database db = await cosmosClient.CreateDatabaseIfNotExistsAsync(databaseId);

                        var container = await db.CreateContainerIfNotExistsAsync(containerId, "/Id");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Failed to create cosmos db or container: " + e.Message);
                    }
                }

                else if (input == "5")
                {
                    Console.WriteLine("Bye");
                }
            }
        }
Ejemplo n.º 6
0
        private void WriteLogLine(WriteWay writeWay, string writeLogLine, params string[] logFilePath)
        {
            if (logFilePath.Length < 2)
            {
                Console.WriteLine(invalidExistLogFilePath);
                return;
            }

            string connectionString            = $"DefaultEndpointsProtocol=https;AccountName={Constant.STORAGE_ACCOUNT_NAME};AccountKey={Constant.Instance.StorageAccountKey};EndpointSuffix=core.windows.net";
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();

            CloudFileShare share = fileClient.GetShareReference(logFilePath[0]);

            if (!share.Exists())
            {
                share.Create();
            }
            CloudFileDirectory sampleDir = share.GetRootDirectoryReference();

            for (int i = 1; i < logFilePath.Length - 1; i++)
            {
                CloudFileDirectory nextLevelDir = sampleDir.GetDirectoryReference("TestLogs");
                if (!sampleDir.Exists())
                {
                    sampleDir.Create();
                }
                sampleDir = nextLevelDir;
            }

            CloudFile file = sampleDir.GetFileReference(logFilePath[logFilePath.Length - 1]);

            lock ("")
            {
                CloudBlobClient    blobClient    = storageAccount.CreateCloudBlobClient();
                CloudBlobContainer blobContainer = blobClient.GetContainerReference("logs");
                blobContainer.CreateIfNotExistsAsync();
                CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference("testBlob");

                List <string> blockIds = new List <string>();
                DateTime      before   = DateTime.Now;

                blockIds.AddRange(blockBlob.DownloadBlockList(BlockListingFilter.Committed).Select(b => b.Name));
                DateTime after = DateTime.Now;
                TimeSpan ts    = after.Subtract(before);
                Console.WriteLine(ts.Seconds + "_" + ts.Milliseconds);

                var newId = Convert.ToBase64String(Encoding.Default.GetBytes(blockIds.Count.ToString()));
                blockBlob.PutBlock(newId, new MemoryStream(Encoding.Default.GetBytes(writeLogLine + "\n")), null);
                blockIds.Add(newId);
                blockBlob.PutBlockList(blockIds);

                string writenLineContent = "";
                if (file.Exists())
                {
                    if (writeWay == WriteWay.Cover)
                    {
                    }
                    else if (writeWay == WriteWay.Append)
                    {
                        writenLineContent = file.DownloadTextAsync().Result;
                    }
                }
                file.UploadText(writenLineContent + writeLogLine + "\n");
            }
        }