Exemple #1
0
        static void Main(string[] args)
        {
            string accountName = "xxx";
            string accountKey  = "xxx";
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);

            CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient();

            //make sure the file share named test1 exists.
            CloudFileShare     fileShare     = cloudFileClient.GetShareReference("test1");
            CloudFileDirectory fileDirectory = fileShare.GetRootDirectoryReference();
            CloudFile          myfile        = fileDirectory.GetFileReference("test123.txt");

            if (!myfile.Exists())
            {
                //if the file does not exists, then create the file and set the file max size to 100kb.
                myfile.Create(100 * 1024 * 1024);
            }

            //upload text to the file
            //Besides using UploadText() method to directly upload text, you can also use UploadFromFile() / UploadFromByteArray() / UploadFromStream() methods as per your need.
            myfile.UploadText("hello, it is using azure storage SDK");

            Console.WriteLine("**completed**");
            Console.ReadLine();
        }
Exemple #2
0
        public override FileUploadResult SaveFile(string fileName, Stream fileContent)
        {
            FileUploadResult result = new FileUploadResult()
            {
                Code = -1
            };

            if (fileContent != null)
            {
                CloudFile file = fileDirectory.GetFileReference(fileName);


                byte[] array;
                using (var ms = new MemoryStream())
                {
                    fileContent.CopyTo(ms);
                    array = ms.ToArray();
                }

                file.Create(array.Length);

                file.UploadFromByteArray(array, 0, array.Length);

                result.Code = 0;
            }

            return(result);
        }
Exemple #3
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.");
        }
        private CloudFile GetCloudFileReference(string path, bool createIfNotExist = false)
        {
            CloudFileDirectory directory = cloudFileClient.GetShareReference(rootDir).GetRootDirectoryReference();
            CloudFile          file      = null;

            string[] folders = GetArrrayOfFolders(path);

            for (int i = 0; i < folders.Length; i++)
            {
                if (i == folders.Length - 1)
                {
                    file = directory.GetFileReference(folders[i]);
                    if (!file.Exists() && createIfNotExist)
                    {
                        file.Create(0L);
                    }
                }
                else
                {
                    directory = directory.GetDirectoryReference(folders[i]);
                    if (!directory.Exists() && createIfNotExist)
                    {
                        directory.Create();
                    }
                }
            }

            return(file);
        }
Exemple #5
0
        private void PrepareFileInternal(CloudFile file, string source)
        {
            FileRequestOptions options = new FileRequestOptions()
            {
                StoreFileContentMD5 = true
            };

            if (source == null)
            {
                int    buffSize = 1024;
                byte[] buffer   = new byte[buffSize];
                rd.NextBytes(buffer);
                file.UploadFromByteArray(buffer, 0, buffSize, options: options);
            }
            else
            {
                Test.Info("Upload source file {0} to destination {1}.", source, file.Uri.OriginalString);

                if (AgentOSType != OSType.Windows)
                {
                    string argument;
                    string directory = string.Empty;
                    var    parent    = file.Parent;
                    while (parent != null && !string.IsNullOrEmpty(parent.Name))
                    {
                        directory = parent.Name + "/" + directory;
                        parent    = parent.Parent;
                    }

                    string fileName = file.Name;
                    if (!string.IsNullOrEmpty(directory))
                    {
                        argument = string.Format("azure storage directory create '{0}' '{1}'", file.Share.Name, directory);
                        argument = AddAccountParameters(argument, AgentConfig);
                        RunNodeJSProcess(argument, true);

                        // when CloudFile is referenced from root directory
                        if (!fileName.Contains("/") || !fileName.StartsWith(directory))
                        {
                            fileName = directory.Trim('/') + '/' + fileName;
                        }
                    }

                    argument = string.Format("azure storage file upload '{0}' {1} {2} -q", source, file.Share.Name, fileName);
                    argument = AddAccountParameters(argument, AgentConfig);
                    RunNodeJSProcess(argument, true);
                }

                if (AgentOSType == OSType.Windows)
                {
                    file.Create(FileUtil.GetFileSize(source));
                    file.UploadFromFile(source, options: options);
                }
            }

            GeneratePropertiesAndMetaData(file);
        }
Exemple #6
0
        private void Initialise(LoggingEvent loggingEvent)
        {
            if (_storageAccount == null || AzureStorageConnectionString != _thisConnectionString)
            {
                _storageAccount       = CloudStorageAccount.Parse(AzureStorageConnectionString);
                _thisConnectionString = AzureStorageConnectionString;
                _client = null;
                _share  = null;
                _folder = null;
                _file   = null;
            }

            if (_client == null)
            {
                _client = _storageAccount.CreateCloudFileClient();
                _share  = null;
                _folder = null;
                _file   = null;
            }

            if (_share == null || _share.Name != ShareName)
            {
                _share = _client.GetShareReference(ShareName);
                _share.CreateIfNotExists();
                _folder = null;
                _file   = null;
            }

            if (_folder == null || Path != _thisFolder)
            {
                var pathElements = Path.Split(new[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);

                _folder = _share.GetRootDirectoryReference();
                foreach (var element in pathElements)
                {
                    _folder = _folder.GetDirectoryReference(element);
                    _folder.CreateIfNotExists();
                }

                _thisFolder = Path;
                _file       = null;
            }

            var filename = Regex.Replace(File, @"\{(.+?)\}", _ => loggingEvent.TimeStamp.ToString(_.Result("$1")));

            if (_file == null || filename != _thisFile)
            {
                _file = _folder.GetFileReference(filename);
                if (!_file.Exists())
                {
                    _file.Create(0);
                }
                _thisFile = filename;
            }
        }
Exemple #7
0
        /// <summary>
        /// Validate the Create permission in the sas token for the the specified file
        /// </summary>
        internal void ValidateFileCreateableWithSasToken(CloudFile file, string sasToken)
        {
            Test.Info("Verify file Create permission");
            file.DeleteIfExists();
            CloudFile sasFile    = new CloudFile(file.Uri, new StorageCredentials(sasToken));
            int       fileLength = 10;

            sasFile.Create(fileLength);
            file.FetchAttributes();
            Test.Assert(file.Exists(), "The file should  exist after Creating with sas token");
            TestBase.ExpectEqual(fileLength, file.Properties.Length, "File Lenth should match.");
        }
Exemple #8
0
        public override Stream FileOpen(FileInfo fileInfo, FileAccess fileAccess, ref string errorMessage, FileShare fileShare, FileMode fileMode, int waitForLockMilliseconds, bool useExternalStorage, bool signalError = true)
        {
            Stream   fStream       = null;
            DateTime dateTimeStart = DateTime.Now;
            int      sleepTime     = Math.Min(300, waitForLockMilliseconds);

            while (true)
            {
                try
                {
                    if (fileMode == FileMode.CreateNew)
                    { // possibly reuse aborted new file (aborted if not exclusively locked)
                        CloudFile cloudFile = m_databaseDir.GetFileReference(fileInfo.Name);
                        if (cloudFile.Exists())
                        {
                            throw new DatabaseAlreadyExistsException(fileInfo.FullName);
                        }
                        cloudFile.Create(s_initialFileSize);
                        FileRequestOptions fileRequestOptions = new FileRequestOptions();
                        fileRequestOptions.ServerTimeout = TimeSpan.FromMilliseconds(waitForLockMilliseconds);
                        fileRequestOptions.ParallelOperationThreadCount = 1;
                        fStream = cloudFile.OpenWrite(s_initialFileSize, null, fileRequestOptions);
                    }
                    else
                    {
                        CloudFile          cloudFile          = m_databaseDir.GetFileReference(fileInfo.Name);
                        FileRequestOptions fileRequestOptions = new FileRequestOptions();
                        fileRequestOptions.ServerTimeout = TimeSpan.FromMilliseconds(waitForLockMilliseconds);
                        if (fileAccess == FileAccess.Read)
                        {
                            fStream = cloudFile.OpenRead(null, fileRequestOptions);
                        }
                        else
                        {
                            fStream = cloudFile.OpenWrite(s_initialFileSize, null, fileRequestOptions);
                        }
                    }
                    return(fStream);
                }
                catch (FileNotFoundException e)
                {
                    fStream?.Dispose();
                    if (signalError)
                    {
                        throw e;
                    }
                    errorMessage += e.Message;
                    return(null);
                }
            }
Exemple #9
0
        static void Main(string[] args)
        {
            string accountname = "xxx";
            string accountkey  = "xxxxxxx";
            CloudStorageAccount storageAccount = new CloudStorageAccount(new StorageCredentials(accountname, accountkey), true);

            // Create a CloudFileClient object for credentialed access to Azure Files.
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

            // Get a reference to the file share.
            CloudFileShare share = fileClient.GetShareReference("s66");

            //if fileshare does not exist, create it.
            share.CreateIfNotExists();

            if (share.Exists())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();

                // Get a reference to the directory.
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("CustomLogs");
                //if the directory does not exist, create it.
                sampleDir.CreateIfNotExists();

                if (sampleDir.Exists())
                {
                    // Get a reference to the file.
                    CloudFile file = sampleDir.GetFileReference("Log1.txt");

                    // if the file exists, read the content of the file.
                    if (file.Exists())
                    {
                        // Write the contents of the file to the console window.
                        Console.WriteLine(file.DownloadTextAsync().Result);
                    }
                    //if the file does not exist, create it with size == 500bytes
                    else
                    {
                        file.Create(500);
                    }
                }
            }

            Console.WriteLine("--file share test--");
            Console.ReadLine();
        }
Exemple #10
0
        /// <summary>
        /// Creates the azure file from a text string using the provided filename. Places in a random directory.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="fileText">The file text.</param>
        /// <param name="fileRecipient">The file recipient.</param>
        /// <returns></returns>
        /// <exception cref="System.Exception"></exception>
        public static bool CreateAzureFileFromText(string fileName, string fileText, string fileRecipient = "")
        {
            //TODO: Replace with code that analyzes the file space of the file to be uploaded.
            var directory = GenerateRandomString();
            var fileSize  = 1000;

            var targetDirectory = GetCloudDirectory(directory, true);

            CloudFile cloudFile = null;

            try
            {
                cloudFile = targetDirectory.GetFileReference(fileName);

                cloudFile.Create(fileSize);
                Console.WriteLine("File created");

                cloudFile.UploadText(fileText);
                Console.WriteLine("File text uploaded");
            }
            catch (StorageException se)
            {
                if (se.InnerException != null)
                {
                    var webException = (System.Net.WebException)se.InnerException;

                    var response = (System.Net.HttpWebResponse)webException.Response;

                    if (response.StatusDescription != null)
                    {
                        Console.WriteLine(response.StatusDescription);
                        throw new Exception(response.StatusDescription);
                    }
                }
                throw se;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

            // Add FileInfo to Database
            return(SqlHelper.TryAddAzureFileInfo(cloudFile, directory, fileName, fileSize, fileRecipient));
        }
Exemple #11
0
        public void ValidateShareWriteableWithSasToken(CloudFileShare share, string sastoken)
        {
            Test.Info("Verify share write permission");
            CloudStorageAccount sasAccount = TestBase.GetStorageAccountWithSasToken(share.ServiceClient.Credentials.AccountName, sastoken);
            CloudFileShare      sasShare   = sasAccount.CreateCloudFileClient().GetShareReference(share.Name);
            string    sasFileName          = Utility.GenNameString("sasFile");
            CloudFile sasFile  = sasShare.GetRootDirectoryReference().GetFileReference(sasFileName);
            long      fileSize = 1024 * 1024;

            byte[] buffer = new byte[fileSize];
            new Random().NextBytes(buffer);
            MemoryStream ms = new MemoryStream(buffer);

            sasFile.Create(fileSize);
            sasFile.UploadFromStream(ms);
            CloudFile retrievedFile = share.GetRootDirectoryReference().GetFileReference(sasFileName);

            retrievedFile.FetchAttributes();
            TestBase.ExpectEqual(fileSize, retrievedFile.Properties.Length, "blob size");
        }
        public void FileOperations(string fileSharename, string Directory, string filePath)
        {
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  fileShare  = fileClient.GetShareReference(fileSharename);

            fileShare.CreateIfNotExists(null, null);
            CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();
            CloudFileDirectory fileDirectory = rootDirectory.GetDirectoryReference(Directory);

            fileDirectory.CreateIfNotExists();
            CloudFile file = fileDirectory.GetFileReference("testfile");

            //Deleting If File Exists
            file.DeleteIfExistsAsync();
            if (file.Exists() == false)
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                file.Create(fs.Length);
                fs.Close();
            }
            file.OpenWrite(null);
            //Upload File Operation
            file.UploadFromFile(filePath, FileMode.Open);
            //Write File Operation
            file.WriteRange(new FileStream(filePath, FileMode.Open), 0);
            Stream azureFile = file.OpenRead();

            //Read File Operation
            azureFile.Position = 0;
            byte[] buffer = new byte[azureFile.Length - 1];
            int    n      = azureFile.Read(buffer, (int)0, 14050);

            for (int i = 0; i < buffer.Length; i++)
            {
                Console.Write(buffer[i].ToString());
            }
            //Download File Operation
            file.DownloadToFile(@"D:\TestFile.pptx", FileMode.Create);
        }
Exemple #13
0
        public TasklistCSVRemote()
        {
            this.storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));
            this.fileClient = storageAccount.CreateCloudFileClient();
            this.share      = fileClient.GetShareReference("tasklistdatabasestore");
            this.root       = this.share.GetRootDirectoryReference();
            // Get a reference to the file we created previously.
            dbFile = this.root.GetFileReference("tasklist.csv");

            // Ensure that the files exists, and create if necessary
            if (!dbFile.Exists())
            {
                dbFile.Create(1000000); // Create 1mb file

                // Write CSV header
                string hdrText = "id,title,isComplete\r\n";
                this.hdrBytes = Encoding.ASCII.GetBytes(hdrText);
                dbFile.WriteRange(new MemoryStream(this.hdrBytes), 0);

                // Populate with a few entries
                this.RebuildList();
            }
        }
Exemple #14
0
    public override Stream FileOpen(FileInfo fileInfo, FileAccess fileAccess, ref string errorMessage, FileShare fileShare, FileMode fileMode, int waitForLockMilliseconds, bool useExternalStorage, bool signalError = true)
    {
      Stream fStream = null;
      DateTime dateTimeStart = DateTime.Now;
      int sleepTime = Math.Min(300, waitForLockMilliseconds);
      while (true)
        try
        {
          if (fileMode == FileMode.CreateNew)
          { // possibly reuse aborted new file (aborted if not exclusively locked)
            CloudFile cloudFile = m_databaseDir.GetFileReference(fileInfo.Name);
            if (cloudFile.Exists())
              throw new DatabaseAlreadyExistsException(fileInfo.FullName);
            cloudFile.Create(s_initialFileSize);
            FileRequestOptions fileRequestOptions = new FileRequestOptions();
            fileRequestOptions.ServerTimeout = TimeSpan.FromMilliseconds(waitForLockMilliseconds);
            fileRequestOptions.ParallelOperationThreadCount = 1;
            fStream = cloudFile.OpenWrite(s_initialFileSize, null, fileRequestOptions);
          }
          else
          {
            CloudFile cloudFile = m_databaseDir.GetFileReference(fileInfo.Name);
            FileRequestOptions fileRequestOptions = new FileRequestOptions();
            fileRequestOptions.ServerTimeout = TimeSpan.FromMilliseconds(waitForLockMilliseconds);
            if (fileAccess == FileAccess.Read)
              fStream = cloudFile.OpenRead(null, fileRequestOptions);
            else
              fStream = cloudFile.OpenWrite(s_initialFileSize, null, fileRequestOptions);
          }
          return fStream;
        }
        catch (FileNotFoundException e)
        {
          fStream?.Dispose();
          if (signalError)
            throw e;
          errorMessage += e.Message;
          return null;
        }
        catch (System.IO.IOException e)
        {
          TimeSpan span = DateTime.Now - dateTimeStart;
          if (span.TotalMilliseconds >= waitForLockMilliseconds)
          {
            if (signalError)
            {
              fStream?.Dispose();
              throw e;
            }
            else
            {
              errorMessage += e.Message;
              return null;
            }
          }
          else
          {
#if WINDOWS_UWP
            System.Threading.Tasks.Task.Delay(sleepTime);
#else
            Thread.Sleep(sleepTime); // give thread that is holding lock a chance to release the lock
#endif
            if (sleepTime > 25)
              --sleepTime;
          }
        }
    }
Exemple #15
0
        /// <summary>
        /// Creates the azure file from file.
        /// </summary>
        /// <param name="fileInfo">The file information.</param>
        /// <param name="recipient">The recipient.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public static bool CreateAzureFileFromFile(FileInfo fileInfo, string fileRecipient = "")
        {
            var directory = GenerateRandomString();
            var fileSize  = fileInfo.Length;

            var targetDirectory = GetCloudDirectory(directory, true);

            CloudFile cloudFile = null;

            try
            {
                cloudFile = targetDirectory.GetFileReference(fileInfo.Name);

                cloudFile.Create(fileInfo.Length);
                Console.WriteLine("File created");

                cloudFile.UploadFromFile(fileInfo.FullName, FileMode.Open);
                Console.WriteLine("File uploaded");
            }
            catch (StorageException se)
            {
                if (se.InnerException != null)
                {
                    var webException = (System.Net.WebException)se.InnerException;

                    var response = (System.Net.HttpWebResponse)webException.Response;

                    if (response.StatusDescription != null)
                    {
                        Console.WriteLine(response.StatusDescription);
                        throw new Exception(response.StatusDescription);
                    }
                }
                throw se;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw e;
            }

            return(SqlHelper.TryAddAzureFileInfo(cloudFile, directory, fileInfo.Name, fileSize, fileRecipient));

            //try
            //{
            //    // Add FileInfo to Database
            //    using (var db = new AzureFileInfoContext())
            //    {
            //        var azureFileInfo = new AzureFileInfo()
            //        {
            //            AzureUri = cloudFile.Uri.ToString(),
            //            AzureDirectory = directory,
            //            FileName = fileInfo.Name,
            //            FileSize = fileSize,
            //            UploadDate = DateTime.Now
            //        };

            //        // Generate the azureFileInfo field and save to DB.
            //        db.AzureFileInfos.Add(azureFileInfo);
            //        db.SaveChanges();

            //        // The newly generated Id value will be used to create the Link that the user will click on: Download\[Id]
            //        azureFileInfo.GenerateLink();

            //        /*
            //         * If a file recipient is specified and is a valid email address, the user of the link will be required to
            //         * enter their email address to identify themselves before downloading.
            //         */
            //        if (!String.IsNullOrWhiteSpace(fileRecipient))
            //        {
            //            azureFileInfo.Recipient = fileRecipient;
            //        }

            //        // Commit additional updates to the FileInfo record.
            //        db.SaveChanges();

            //        return azureFileInfo.Link;
            //    }
            //}
            //catch (SqlException se)
            //{
            //    Console.WriteLine(se.Message);
            //    throw se;
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //    throw e;
            //}
        }