public AzureOperations(string connectionString, string shareName)
 {
     var account = CloudStorageAccount.Parse(connectionString);
     client = account.CreateCloudFileClient();
     share = client.GetShareReference(shareName);
     share.CreateIfNotExists();
     root = share.GetRootDirectoryReference();
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="AzureFileDirectoryLocation"/> class.
        /// </summary>
        /// <param name="fileDir">CloudFileDirectory instance as a location in a transfer job. 
        /// It could be a source, a destination.</param>
        public AzureFileDirectoryLocation(CloudFileDirectory fileDir)
        { 
            if (null == fileDir)
            {
                throw new ArgumentNullException("fileDir");
            }

            this.fileDirectorySerializer = new SerializableCloudFileDirectory(fileDir);
        }
示例#3
0
 public static CloudFile GetFileReference(CloudFileDirectory parent, string path)
 {
     var filename = Path.GetFileName(path);
     var fullPath = Path.GetDirectoryName(path);
     if (fullPath == string.Empty)
     {
         return parent.GetFileReference(filename);
     }
     var dirReference = GetDirectoryReference(parent, fullPath);
     return dirReference.GetFileReference(filename);
 }
示例#4
0
 public static CloudFileDirectory GetDirectoryReference(CloudFileDirectory parent, string path)
 {
     if (path.Contains(@"\"))
     {
         var paths = path.Split('\\');
         return GetDirectoryReference(parent.GetDirectoryReference(paths.First()), string.Join(@"\", paths.Skip(1)));
     }
     else
     {
         return parent.GetDirectoryReference(path);
     }
 }
示例#5
0
 public static async Task<IEnumerable<IListFileItem>> ListFilesAndDirectoriesAsync(CloudFileDirectory directory, int? maxResults, FileRequestOptions options, OperationContext operationContext)
 {
     List<IListFileItem> results = new List<IListFileItem>();
     FileContinuationToken token = null;
     do
     {
         FileResultSegment resultSegment = await directory.ListFilesAndDirectoriesSegmentedAsync(maxResults, token, options, operationContext);
         results.AddRange(resultSegment.Results);
         token = resultSegment.ContinuationToken;
     }
     while (token != null);
     return results;
 }
        public async Task EnumerateFilesAndDirectoriesAsync(CloudFileDirectory directory, Action<IListFileItem> enumerationAction, FileRequestOptions options, OperationContext operationContext, CancellationToken token)
        {
            FileContinuationToken continuationToken = null;
            do
            {
                var segment = await directory.ListFilesAndDirectoriesSegmentedAsync(null, continuationToken, options, operationContext, token);
                foreach (var item in segment.Results)
                {
                    enumerationAction(item);
                }

                continuationToken = segment.ContinuationToken;
            }
            while (continuationToken != null);
        }
示例#7
0
 public AzureSession(string connectionString, string shareName, string systemDir, int waitForLockMilliseconds = 5000, bool optimisticLocking = true,
   bool enableCache = true, CacheEnum objectCachingDefaultPolicy = CacheEnum.Yes)
   : base(systemDir, waitForLockMilliseconds, optimisticLocking, enableCache, objectCachingDefaultPolicy)
 {
   m_cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
   if (Path.IsPathRooted(systemDir) == false)
     SystemDirectory = systemDir;
   m_shareName = shareName;
   m_cloudFileClient = m_cloudStorageAccount.CreateCloudFileClient();
   m_cloudShare = m_cloudFileClient.GetShareReference(shareName);
   if (m_cloudShare.Exists())
   {
     m_rootDir = m_cloudShare.GetRootDirectoryReference();
     m_databaseDir = m_rootDir.GetDirectoryReference(systemDir);
     m_databaseDir.CreateIfNotExists();
   }
 }
        public Task EnumerateFilesAndDirectoriesAsync(CloudFileDirectory directory, Action<IListFileItem> enumerationAction, FileRequestOptions options, OperationContext operationContext, CancellationToken token)
        {
            IListFileItem[] enumerationItems;
            if (this.enumerationResults.TryGetValue(directory.Name, out enumerationItems))
            {
                foreach (var item in enumerationItems)
                {
                    enumerationAction(item);
                }

                return TaskEx.FromResult(true);
            }
            else
            {
                throw new MockupException("DirectoryNotFound");
            }
        }
示例#9
0
        // TODO: extract the common pattern(Upload and Download)
        private static void UploadFiles(Dictionary<string, DataSyncFileInfo> localOnly, CloudFileClient fileClient)
        {
            List<Task> waitTasks = new List<Task>();
            foreach (var file in localOnly)
            {
                var path = file.Key.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (path.Length < 2)
                {
                    throw new Exception("share is needed, so you should create a flod in local.");
                }
                string shareString = path[0];
                fileClient.GetShareReference(shareString).CreateIfNotExists();
                string dirString = path[0] + "/";
                for (int i = 1; i < path.Length - 1; i++)
                {
                    dirString += path[i] + "/";
                    CloudFileDirectory fileDir = new CloudFileDirectory(
                    new Uri(fileClient.BaseUri.AbsoluteUri + dirString), fileClient.Credentials);
                    fileDir.CreateIfNotExists();
                }

                var uploadTask = Task.Run(() =>
                {
                    Console.WriteLine($"upload {file.Key} ...");
                    CloudFile cloudFile = new CloudFile(new Uri(fileClient.BaseUri.AbsoluteUri + file.Key.Substring(1)), fileClient.Credentials);
                    cloudFile.UploadFromFile(file.Value.LocalFile.FullName);
                    Console.WriteLine($"{file.Key} is uploaded!");
                });

                waitTasks.Add(uploadTask);
                // TODO: config the number of running task
                while (waitTasks.Count(task =>
                task.Status == TaskStatus.Running || task.Status == TaskStatus.WaitingToRun) >= 8)
                {
                    Task.WaitAny(waitTasks.ToArray());
                }
            }

            Task.WaitAll(waitTasks.ToArray());
        }
示例#10
0
 private static void ListCloudFilesByDir(CloudFileDirectory dir)
 {
     var files = dir.ListFilesAndDirectories();
     foreach (var file in files)
     {
         if (file is CloudFile)
         {
             cloudFiles.Add(file.Uri.LocalPath, new DataSyncFileInfo { CloudFile = (CloudFile)file });
         }
         else if (file is CloudFileDirectory)
         {
             ListCloudFilesByDir((CloudFileDirectory)file);
         }
         else
         {
             throw new Exception($"unknown type:{file.StorageUri}");
         }
     }
 }
        public async Task CloudFileSetPropertiesAsync()
        {
            CloudFileShare share = GetRandomShareReference();

            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                await file.CreateAsync(1024);

                string         eTag         = file.Properties.ETag;
                DateTimeOffset lastModified = file.Properties.LastModified.Value;

                await Task.Delay(1000);

                file.Properties.CacheControl    = "no-transform";
                file.Properties.ContentEncoding = "gzip";
                file.Properties.ContentLanguage = "tr,en";
                file.Properties.ContentMD5      = "MDAwMDAwMDA=";
                file.Properties.ContentType     = "text/html";
                await file.SetPropertiesAsync();

                Assert.IsTrue(file.Properties.LastModified > lastModified);
                Assert.AreNotEqual(eTag, file.Properties.ETag);

                CloudFile file2 = share.GetRootDirectoryReference().GetFileReference("file1");
                await file2.FetchAttributesAsync();

                Assert.AreEqual("no-transform", file2.Properties.CacheControl);
                Assert.AreEqual("gzip", file2.Properties.ContentEncoding);
                Assert.AreEqual("tr,en", file2.Properties.ContentLanguage);
                Assert.AreEqual("MDAwMDAwMDA=", file2.Properties.ContentMD5);
                Assert.AreEqual("text/html", file2.Properties.ContentType);

                CloudFile file3 = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream stream = new MemoryStream())
                {
                    FileRequestOptions options = new FileRequestOptions()
                    {
                        DisableContentMD5Validation = true,
                    };
                    await file3.DownloadToStreamAsync(stream, null, options, null);
                }
                AssertAreEqual(file2.Properties, file3.Properties);

                CloudFileDirectory          rootDirectory = share.GetRootDirectoryReference();
                IEnumerable <IListFileItem> results       = await ListFilesAndDirectoriesAsync(rootDirectory, null, null, null, null);

                CloudFile file4 = (CloudFile)results.First();
                Assert.AreEqual(file2.Properties.Length, file4.Properties.Length);

                CloudFile file5 = share.GetRootDirectoryReference().GetFileReference("file1");
                Assert.IsNull(file5.Properties.ContentMD5);
                byte[] target = new byte[4];
                await file5.DownloadRangeToByteArrayAsync(target, 0, 0, 4);

                Assert.AreEqual("MDAwMDAwMDA=", file5.Properties.ContentMD5);
            }
            finally
            {
                share.DeleteIfExistsAsync().Wait();
            }
        }
 public Task CreateDirectoryAsync(CloudFileDirectory directory, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return directory.CreateAsync(options, operationContext, cancellationToken);
 }
 public Task DeleteDirectoryAsync(CloudFileDirectory directory, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return TaskEx.FromResult(true);
 }
        private void StartCopyFromFile(IStorageBlobManagement destChannel, CloudFileDirectory srcDir, string srcFilePath, string destContainerName, string destBlobName)
        {
            string[] path = NamingUtil.ValidatePath(srcFilePath, true);
            CloudFile file = srcDir.GetFileReferenceByPath(path);

            this.StartCopyFromFile(destChannel, file, destContainerName, destBlobName);
        }
        private bool IsLastDirEqualsOrSubDirOf(CloudFileDirectory dir)
        {
            if (null == dir)
            {
                // Both null, equals
                return null == this.lastAzureFileDirectory;
            }
            else if (null != this.lastAzureFileDirectory)
            {
                string absoluteUri1 = AppendSlash(this.lastAzureFileDirectory.Uri.AbsoluteUri);
                string absoluteUri2 = AppendSlash(dir.Uri.AbsoluteUri);

                if (absoluteUri1.StartsWith(absoluteUri2, StringComparison.Ordinal))
                {
                    return true;
                }
            }

            return false;
        }
        public void CloudFileDirectoryListFilesAndDirectoriesAPM()
        {
            CloudFileClient client = GenerateCloudFileClient();
            string          name   = GetRandomShareName();
            CloudFileShare  share  = client.GetShareReference(name);

            try
            {
                share.Create();
                if (CloudFileDirectorySetup(share))
                {
                    CloudFileDirectory topDir1 = share.GetRootDirectoryReference().GetDirectoryReference("TopDir1");
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        FileContinuationToken token       = null;
                        List <IListFileItem>  simpleList1 = new List <IListFileItem>();
                        do
                        {
                            IAsyncResult result = topDir1.BeginListFilesAndDirectoriesSegmented(
                                null,
                                null,
                                null,
                                null,
                                ar => waitHandle.Set(),
                                null);
                            waitHandle.WaitOne();
                            FileResultSegment segment = topDir1.EndListFilesAndDirectoriesSegmented(result);
                            simpleList1.AddRange(segment.Results);
                            token = segment.ContinuationToken;
                        }while (token != null);

                        ////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);

                        IListFileItem item11 = simpleList1.ElementAt(0);
                        Assert.IsTrue(item11.Uri.Equals(share.Uri + "/TopDir1/File1"));
                        Assert.AreEqual("File1", ((CloudFile)item11).Name);

                        IListFileItem item12 = simpleList1.ElementAt(1);
                        Assert.IsTrue(item12.Uri.Equals(share.Uri + "/TopDir1/MidDir1"));
                        Assert.AreEqual("MidDir1", ((CloudFileDirectory)item12).Name);

                        IListFileItem item13 = simpleList1.ElementAt(2);
                        Assert.IsTrue(item13.Uri.Equals(share.Uri + "/TopDir1/MidDir2"));
                        Assert.AreEqual("MidDir2", ((CloudFileDirectory)item13).Name);
                        CloudFileDirectory midDir2 = (CloudFileDirectory)item13;

                        List <IListFileItem> simpleList2 = new List <IListFileItem>();
                        do
                        {
                            IAsyncResult result = midDir2.BeginListFilesAndDirectoriesSegmented(
                                token,
                                ar => waitHandle.Set(),
                                null);
                            waitHandle.WaitOne();
                            FileResultSegment segment = midDir2.EndListFilesAndDirectoriesSegmented(result);
                            simpleList2.AddRange(segment.Results);
                            token = segment.ContinuationToken;
                        }while (token != null);
                        Assert.IsTrue(simpleList2.Count == 2);

                        IListFileItem item21 = simpleList2.ElementAt(0);
                        Assert.IsTrue(item21.Uri.Equals(share.Uri + "/TopDir1/MidDir2/EndDir1"));
                        Assert.AreEqual("EndDir1", ((CloudFileDirectory)item21).Name);

                        IListFileItem item22 = simpleList2.ElementAt(1);
                        Assert.IsTrue(item22.Uri.Equals(share.Uri + "/TopDir1/MidDir2/EndDir2"));
                        Assert.AreEqual("EndDir2", ((CloudFileDirectory)item22).Name);
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
示例#17
0
        public static async Task <IEnumerable <IListFileItem> > ListFilesAndDirectoriesAsync(CloudFileDirectory directory, int?maxResults, FileRequestOptions options, OperationContext operationContext)
        {
            List <IListFileItem>  results = new List <IListFileItem>();
            FileContinuationToken token   = null;

            do
            {
                FileResultSegment resultSegment = await directory.ListFilesAndDirectoriesSegmentedAsync(maxResults, token, options, operationContext);

                results.AddRange(resultSegment.Results);
                token = resultSegment.ContinuationToken;
            }while (token != null);
            return(results);
        }
 public void CloudFileDirectoryConstructor()
 {
     CloudFileShare share = GetRandomShareReference();
     CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference("directory1");
     CloudFileDirectory directory2 = new CloudFileDirectory(directory.StorageUri, null);
     Assert.AreEqual(directory.Name, directory2.Name);
     Assert.AreEqual(directory.StorageUri, directory2.StorageUri);
     Assert.AreEqual(directory.Share.StorageUri, directory2.Share.StorageUri);
     Assert.AreEqual(directory.ServiceClient.StorageUri, directory2.ServiceClient.StorageUri);
 }
示例#19
0
 /// <inheritdoc />
 public override List<Database> OpenAllDatabases(bool update = false)
 {
   SortedSet<Database> dbSet = new SortedSet<Database>();
   foreach (Database db in NewDatabases)
     dbSet.Add(db);
   foreach (DatabaseLocation location in (IEnumerable<DatabaseLocation>)DatabaseLocations)
   {
     m_databaseDir = m_rootDir.GetDirectoryReference(location.DirectoryPath);
     foreach (CloudFile file in m_databaseDir.ListFilesAndDirectories())
     {
       string fileName = file.Name;
       int indexEnd = fileName.IndexOf('.');
       string dbNumStr = fileName.Substring(0, indexEnd);
       UInt32 dbNum;
       if (UInt32.TryParse(dbNumStr, out dbNum) && dbNum >= location.StartDatabaseNumber && dbNum <= location.EndDatabaseNumber)
       {
         Database db = OpenDatabase(dbNum, update);
         dbSet.Add(db);
       }
     }
   }
   return dbSet.ToList<Database>();
 }
示例#20
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;
 }
示例#21
0
 /// <inheritdoc />
 public override bool CreateDirectory(string path)
 {
   var share = m_rootDir.GetDirectoryReference(path);
   if (!share.Exists())
   {
     m_rootDir = m_cloudShare.GetRootDirectoryReference();
     m_databaseDir = m_rootDir.GetDirectoryReference(path);
     m_databaseDir.CreateIfNotExists();
     return true;
   }
   return false;
 }
        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;
            }
        }
        public void CloudFileDirectorySAS()
        {
            CloudFileDirectory dir = this.testShare.GetRootDirectoryReference().GetDirectoryReference("dirfile");
            CloudFile file = dir.GetFileReference("dirfile");

            dir.Create();
            file.Create(512);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List
            };

            string sasToken = file.GetSharedAccessSignature(policy);
            CloudFileDirectory sasDir = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            TestHelper.ExpectedException(
                () => sasDir.FetchAttributes(),
                "Fetching attributes of a directory using a file SAS should fail",
                HttpStatusCode.Forbidden);

            sasToken = this.testShare.GetSharedAccessSignature(policy);
            sasDir = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            sasDir.FetchAttributes();
        }
        private static void CreateCloudFileDirectoryRecursively(CloudFileDirectory dir)
        {
            if (null == dir)
            {
                return;
            }

            CloudFileDirectory parent = dir.Parent;

            // null == parent means dir is root directory, 
            // we should not call CreateIfNotExists in that case
            if (null != parent)
            {
                CreateCloudFileDirectoryRecursively(parent);
                dir.CreateIfNotExists(Transfer_RequestOptions.DefaultFileRequestOptions);
            }
        }
        private void CreateParentDirectoryIfNotExists(CloudFile file)
        {
            FileRequestOptions fileRequestOptions = Transfer_RequestOptions.DefaultFileRequestOptions;
            CloudFileDirectory parent = file.Parent;
            if (!this.IsLastDirEqualsOrSubDirOf(parent))
            {
                if (!parent.Exists(fileRequestOptions))
                {
                    CreateCloudFileDirectoryRecursively(parent);
                }

                this.lastAzureFileDirectory = parent;
            }
        }
 public Task DeleteDirectoryAsync(CloudFileDirectory directory, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return directory.DeleteAsync(accessCondition, options, operationContext, cancellationToken);
 }
 public Task<bool> DirectoryExistsAsync(CloudFileDirectory directory, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return TaskEx.FromResult(this.availableDirectoryNames.Contains(directory.Name));
 }
        /// <summary>
        /// Download an Azure file directory from Azure File Storage.
        /// </summary>
        /// <param name="sourceFileDir">The <see cref="CloudFileDirectory"/> that is the source Azure file directory.</param>
        /// <param name="destPath">Path to the destination directory</param>
        /// <param name="options">A <see cref="DownloadOptions"/> object that specifies additional options for the operation.</param>
        /// <param name="context">A <see cref="TransferContext"/> object that represents the context for the current operation.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> object to observe while waiting for a task to complete.</param>
        /// <returns>A <see cref="Task"/> object that represents the asynchronous operation.</returns>
        public static Task DownloadDirectoryAsync(CloudFileDirectory sourceFileDir, string destPath, DownloadDirectoryOptions options, TransferContext context, CancellationToken cancellationToken)
        {
            AzureFileDirectoryLocation sourceLocation = new AzureFileDirectoryLocation(sourceFileDir);
            DirectoryLocation destLocation = new DirectoryLocation(destPath);
            AzureFileEnumerator sourceEnumerator = new AzureFileEnumerator(sourceLocation);
            if (options != null)
            {
                TransferManager.CheckSearchPatternOfAzureFileSource(options);

                sourceEnumerator.SearchPattern = options.SearchPattern;
                sourceEnumerator.Recursive = options.Recursive;

                FileRequestOptions requestOptions = Transfer_RequestOptions.DefaultFileRequestOptions;
                requestOptions.DisableContentMD5Validation = options.DisableContentMD5Validation;
                sourceLocation.FileRequestOptions = requestOptions;
            }

            return DownloadDirectoryInternalAsync(sourceLocation, destLocation, sourceEnumerator, context, cancellationToken);
        }
 public Task FetchDirectoryAttributesAsync(CloudFileDirectory directory, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     if (this.availableDirectoryNames.Contains(directory.Name))
     {
         return TaskEx.FromResult(true);
     }
     else
     {
         throw new MockupException("DirectoryNotFound");
     }
 }
 public Task FetchDirectoryAttributesAsync(CloudFileDirectory directory, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken token)
 {
     return directory.FetchAttributesAsync(accessCondition, options, operationContext, token);
 }
 public Task<bool> DirectoryExistsAsync(CloudFileDirectory directory, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return directory.ExistsAsync(options, operationContext, cancellationToken);
 }
示例#32
0
        public async Task CloudFileClientMaximumExecutionTimeoutShouldNotBeHonoredForStreamsAsync()
        {
            CloudFileClient    fileClient    = GenerateCloudFileClient();
            CloudFileShare     share         = fileClient.GetShareReference(Guid.NewGuid().ToString("N"));
            CloudFileDirectory rootDirectory = share.GetRootDirectoryReference();

            byte[] buffer = FileTestBase.GetRandomBuffer(1024 * 1024);

            try
            {
                await share.CreateAsync();

                fileClient.DefaultRequestOptions.MaximumExecutionTime = TimeSpan.FromSeconds(30);
                CloudFile file = rootDirectory.GetFileReference("file");
                file.StreamMinimumReadSizeInBytes = 1024 * 1024;

                using (var fileStream = await file.OpenWriteAsync(8 * 1024 * 1024))
                {
                    Stream fos = fileStream;

                    DateTime start = DateTime.Now;
                    for (int i = 0; i < 7; i++)
                    {
                        await fos.WriteAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last write
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    await fos.WriteAsync(buffer, 0, buffer.Length);

                    await fileStream.CommitAsync();
                }

                using (var fileStream = await file.OpenReadAsync())
                {
                    Stream fis = fileStream;

                    DateTime start = DateTime.Now;
                    int      total = 0;
                    while (total < 7 * 1024 * 1024)
                    {
                        total += await fis.ReadAsync(buffer, 0, buffer.Length);
                    }

                    // Sleep to ensure we are over the Max execution time when we do the last read
                    int msRemaining = (int)(fileClient.DefaultRequestOptions.MaximumExecutionTime.Value - (DateTime.Now - start)).TotalMilliseconds;

                    if (msRemaining > 0)
                    {
                        await Task.Delay(msRemaining);
                    }

                    while (true)
                    {
                        int count = await fis.ReadAsync(buffer, 0, buffer.Length);

                        total += count;
                        if (count == 0)
                        {
                            break;
                        }
                    }
                }
            }
            finally
            {
                fileClient.DefaultRequestOptions.MaximumExecutionTime = null;
                share.DeleteIfExistsAsync().Wait();
            }
        }
示例#33
0
        public async Task CloudFileDirectorySASAsync()
        {
            CloudFileDirectory dir = this.testShare.GetRootDirectoryReference().GetDirectoryReference("dirfile");
            CloudFile file = dir.GetFileReference("dirfile");

            await dir.CreateAsync();
            await file.CreateAsync(512);

            SharedAccessFilePolicy policy = new SharedAccessFilePolicy()
            {
                SharedAccessStartTime = DateTimeOffset.UtcNow.AddMinutes(-5),
                SharedAccessExpiryTime = DateTimeOffset.UtcNow.AddMinutes(30),
                Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.List
            };

            string sasToken = file.GetSharedAccessSignature(policy);
            CloudFileDirectory sasDir = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            OperationContext context = new OperationContext();
            await TestHelper.ExpectedExceptionAsync(
                async () => await sasDir.FetchAttributesAsync(null /* accessCondition */, null /* options */, context),
                context,
                "Fetching attributes of a directory using a file SAS should fail",
                HttpStatusCode.Forbidden,
                "");

            sasToken = this.testShare.GetSharedAccessSignature(policy);
            sasDir = new CloudFileDirectory(new Uri(dir.Uri.AbsoluteUri + sasToken));
            await sasDir.FetchAttributesAsync();
        }
 /// <summary>
 /// Download an Azure file directory from Azure File Storage.
 /// </summary>
 /// <param name="sourceFileDir">The <see cref="CloudFileDirectory"/> that is the source Azure file directory.</param>
 /// <param name="destPath">Path to the destination directory</param>
 /// <param name="options">A <see cref="DownloadOptions"/> object that specifies additional options for the operation.</param>
 /// <param name="context">A <see cref="TransferContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="Task"/> object that represents the asynchronous operation.</returns>
 public static Task DownloadDirectoryAsync(CloudFileDirectory sourceFileDir, string destPath, DownloadDirectoryOptions options, TransferContext context)
 {
     return DownloadDirectoryAsync(sourceFileDir, destPath, options, context, CancellationToken.None);
 }