Ejemplo n.º 1
0
        /// <summary>
        /// Validate the delete permission in the sas token for the the specified file
        /// </summary>
        internal void ValidateFileDeleteableWithSasToken(CloudFile file, string sasToken)
        {
            Test.Info("Verify file delete permission");
            Test.Assert(file.Exists(), "The file should exist");
            CloudFile sasFile = new CloudFile(file.Uri, new StorageCredentials(sasToken));

            sasFile.Delete();
            Test.Assert(!file.Exists(), "The file should not exist after deleting with sas token");
        }
Ejemplo n.º 2
0
 /// <inheritdoc />
 protected override void MoveFile(Database db, string newPath)
 {
   FileInfo newFileInfo = new FileInfo(newPath);
   m_databaseDir = m_rootDir.GetDirectoryReference(db.FileInfo.Directory.Name);
   CloudFile cloudFile = m_databaseDir.GetFileReference(db.FileInfo.Name);
   CloudFile destFile = m_databaseDir.GetFileReference(newFileInfo.Name);
   destFile.StartCopy(cloudFile); // Azure provides NO rename/move functionality !!!
   while (destFile.CopyState.Status == Microsoft.WindowsAzure.Storage.Blob.CopyStatus.Pending)
     Thread.Sleep(100);
   cloudFile.Delete();
   db.FileInfo = newFileInfo;
 }
Ejemplo n.º 3
0
        public void ValidateShareDeleteableWithSasToken(CloudFileShare share, string sastoken)
        {
            Test.Info("Verify share delete permission");
            string    fileName = Utility.GenNameString("file");
            CloudFile file     = CreateFile(share, fileName);

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

            CloudFile sasFile = sasShare.GetRootDirectoryReference().GetFileReference(fileName);

            sasFile.Delete();

            Test.Assert(!file.Exists(), "blob should not exist");
        }
Ejemplo n.º 4
0
        public void DownloadFileFromShareSnapshot_dir()
        {
            string shareName = CloudFileUtil.GenerateUniqueFileShareName();
            string dirName   = CloudFileUtil.GenerateUniqueDirectoryName();
            string fileName  = CloudFileUtil.GenerateUniqueFileName();

            try
            {
                CloudFileShare     share          = fileUtil.EnsureFileShareExists(shareName);
                CloudFileShare     shareSnapshot1 = share.Snapshot();
                CloudFileDirectory dir            = fileUtil.EnsureDirectoryExists(share, dirName);
                CloudFile          file           = fileUtil.CreateFile(dir, fileName);
                CloudFileShare     shareSnapshot2 = share.Snapshot();
                file.Delete();
                dir.Delete();

                //Get File content
                string StorageConnectionString = Test.Data.Get("StorageConnectionString");
                Test.Assert((CommandAgent as PowerShellAgent).InvokePSScript(string.Format(",(New-AzureStorageContext -ConnectionString \"{5}\" | Get-AzureStorageShare -Name {0} -SnapshotTime \"{1}\").GetRootDirectoryReference().GetDirectoryReference(\"{4}\") | Get-AzureStorageFileContent -Path {2} -Destination {3} -Force",
                                                                                           shareName,
                                                                                           shareSnapshot2.SnapshotTime.Value,
                                                                                           fileName,
                                                                                           fileName,
                                                                                           dirName,
                                                                                           StorageConnectionString)),
                            string.Format("Download File {0} from share snapshot {1}, {2} should success.", dirName + "\\" + fileName, shareName, shareSnapshot2.SnapshotTime.Value));

                //validate MD5
                CloudFile file2 = shareSnapshot2.GetRootDirectoryReference().GetDirectoryReference(dirName).GetFileReference(fileName);
                file2.FetchAttributes();
                Test.Assert(file2.Properties.ContentMD5 == FileUtil.GetFileContentMD5(fileName), "Expected MD5: {0}, real MD5: {1}", file2.Properties.ContentMD5, FileUtil.GetFileContentMD5(fileName));
            }
            finally
            {
                try
                {
                    fileUtil.DeleteFileShareIfExists(shareName);
                }
                catch (Exception e)
                {
                    Test.Warn("Unexpected exception when cleanup file share {0}: {1}", shareName, e);
                }
            }
        }
Ejemplo n.º 5
0
 public void CleanupDirectory(CloudFileDirectory directory)
 {
     foreach (var item in directory.ListFilesAndDirectories())
     {
         CloudFile file = item as CloudFile;
         if (file != null)
         {
             file.Delete();
         }
         else
         {
             CloudFileDirectory dir = item as CloudFileDirectory;
             if (dir != null)
             {
                 this.CleanupDirectory(dir);
                 dir.Delete();
             }
         }
     }
 }
Ejemplo n.º 6
0
        public bool DeleteFile(string fileDirectory, string fileName)
        {
            try
            {
                fileDirectory = CleanRelativeCloudDirectoryName(fileDirectory);

                CloudStorageAccount cloudStorageAccount = GetCloudStorageAccount();
                CloudFileClient     fileClient          = GetCloudFileClient(cloudStorageAccount);
                CloudFileShare      share    = GetCloudFileShareReference(fileClient);
                CloudFileDirectory  shareDir = GetCloudFileDirectory(share, fileDirectory);
                CloudFile           file     = GetCloudFile(shareDir, fileName);
                if (file.Exists())
                {
                    file.Delete();
                    return(true);
                }
            }
            catch (Exception oExeption)
            {
                oExeption.Log($"DeleteFile - [{fileDirectory}\\{fileName}]");
            }
            return(false);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Method to get the File from Azure File Service Using FileGuid and Directory Name
        /// </summary>
        /// <param name="directoryname"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        private byte[] getFileFromAzureFileService(int ModuleID, string filename, bool operDelFile)
        {
            var docconfigdet = GetDocumentShareDetails(ModuleID);

            string[]            directorydet   = docconfigdet.ChildDirectory.Split('/');
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
            CloudFileShare      share          = fileClient.GetShareReference(docconfigdet.RootDirectory.ToString());

            if (share.Exists())
            {
                CloudFileDirectory[] filedirectory = new CloudFileDirectory[directorydet.Length];
                var i = 0;
                CloudFileDirectory rootdir = share.GetRootDirectoryReference();

                foreach (var dir in directorydet)
                {
                    if (i == 0)
                    {
                        filedirectory[i] = rootdir.GetDirectoryReference(dir);
                    }
                    else
                    {
                        filedirectory[i] = filedirectory[i - 1].GetDirectoryReference(dir);
                    }


                    if (!filedirectory[i].Exists())
                    {
                        throw new ArgumentOutOfRangeException("File Share Does not Exists");
                    }
                    i++;
                }

                i--;
                try
                {
                    CloudFile downloadfile = filedirectory[i].GetFileReference(filename);
                    if (operDelFile)
                    {
                        downloadfile.Delete();
                        return(null);
                    }

                    if (downloadfile.Exists())
                    {
                        if (downloadfile.Properties.Length <= 0)
                        {
                            throw new ArgumentOutOfRangeException("File Does not Exists...");
                        }
                        byte[] filebytes = new byte[downloadfile.Properties.Length];
                        downloadfile.DownloadToByteArray(filebytes, 0);
                        return(filebytes);
                    }
                    else
                    {
                        throw new Exception("File Not Found...");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("File Share Does not Exists");
            }
        }