public static void UploadFile(Stream streamfilename, string filename, string FolderPath, string ShareName) { //read connection string to store data on azure from config file with account name and key CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString")); CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient(); //GetShareReference() take reference from cloud CloudFileShare cloudFileShare = cloudFileClient.GetShareReference(ShareName); //Create share name if not exist cloudFileShare.CreateIfNotExists(); //get all directory reference located in share name CloudFileDirectory cloudFileDirectory = cloudFileShare.GetRootDirectoryReference(); //Specify the nested folder var nestedFolderStructure = FolderPath; var delimiter = new char[] { '/' }; //split all nested folder by delimeter var nestedFolderArray = nestedFolderStructure.Split(delimiter); for (var i = 0; i < nestedFolderArray.Length; i++) { //check directory avilability if not exist then create directory cloudFileDirectory = cloudFileDirectory.GetDirectoryReference(nestedFolderArray[i]); cloudFileDirectory.CreateIfNotExists(); } ////create object of file reference and get all files within directory CloudFile cloudFile = cloudFileDirectory.GetFileReference(filename); //upload file on azure cloudFile.UploadFromStream(streamfilename); Console.WriteLine("File uploaded sucessfully"); Console.ReadLine(); }
/// <summary> /// Gets a file reference from the azure cloud file storage if it exists /// </summary> protected CloudFile GetCloudFileInfo(string fileDirectory, string fileName) { CloudFile cloudFile = null; try { fileDirectory = CleanRelativeCloudDirectoryName(fileDirectory); CloudStorageAccount cloudStorageAccount = GetCloudStorageAccount(); CloudFileClient fileClient = GetCloudFileClient(cloudStorageAccount); CloudFileShare share = GetCloudFileShareReference(fileClient); CloudFileDirectory cloudFileDirectory = GetCloudFileDirectory(share, fileDirectory); if ((cloudFileDirectory != null) && cloudFileDirectory.Exists()) { cloudFile = cloudFileDirectory.GetFileReference(fileName); cloudFile.FetchAttributes(); } } catch (Exception oExeption) { oExeption.Log($"GetCloudFileInfo [{fileDirectory}\\{fileName}]"); } return(cloudFile); }
private void BuildDirNode(CloudFileDirectory cloudDir, DirNode dirNode) { foreach (IListFileItem item in cloudDir.ListFilesAndDirectories(HelperConst.DefaultFileOptions)) { CloudFile cloudFile = item as CloudFile; CloudFileDirectory subCloudDir = item as CloudFileDirectory; if (cloudFile != null) { // Cannot fetch attributes while listing, so do it for each cloud file. cloudFile.FetchAttributes(options: HelperConst.DefaultFileOptions); FileNode fileNode = new FileNode(cloudFile.Name); this.BuildFileNode(cloudFile, fileNode); dirNode.AddFileNode(fileNode); } else if (subCloudDir != null) { DirNode subDirNode = new DirNode(subCloudDir.Name); this.BuildDirNode(subCloudDir, subDirNode); dirNode.AddDirNode(subDirNode); } } }
public static string DownloadFile(string folderPath, string fileName) { try { string localFile = Path.GetTempFileName(); CloudFileDirectory directory = GetFileDirectory(folderPath); CloudFile file = directory.GetFileReference(fileName); file.DownloadToFile(localFile, FileMode.Create); return(localFile); } catch (Exception ex) { if (_feedback != null) { _feedback.OnException(fileClient, ex); } } return(""); }
static void Main(string[] args) { CloudStorageAccount cuentaAlmacenamiento = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("cadenaConexion")); CloudFileClient clienteArchivos = cuentaAlmacenamiento.CreateCloudFileClient(); CloudFileShare archivoCompartido = clienteArchivos.GetShareReference("platzi"); if (archivoCompartido.Exists()) { CloudFileDirectory carpetaRaiz = archivoCompartido.GetRootDirectoryReference(); CloudFileDirectory directorio = carpetaRaiz.GetDirectoryReference("registros"); if (directorio.Exists()) { CloudFile archivo = directorio.GetFileReference("logActividades.txt"); if (archivo.Exists()) { System.Console.WriteLine(archivo.DownloadTextAsync().Result); System.Console.ReadLine(); } } } }
/// <summary> /// Write copy progress /// </summary> /// <param name="file">CloudFile instance</param> /// <param name="progress">Progress record</param> internal void WriteCopyProgress(CloudFile file, ProgressRecord progress) { if (file.CopyState == null) { return; } long bytesCopied = file.CopyState.BytesCopied ?? 0; long totalBytes = file.CopyState.TotalBytes ?? 0; int percent = 0; if (totalBytes != 0) { percent = (int)(bytesCopied * 100 / totalBytes); progress.PercentComplete = percent; } string activity = String.Format(Resources.CopyFileStatus, file.CopyState.Status.ToString(), file.GetFullPath(), file.Share.Name, file.CopyState.Source.ToString()); progress.Activity = activity; string message = String.Format(Resources.CopyPendingStatus, percent, file.CopyState.BytesCopied, file.CopyState.TotalBytes); progress.StatusDescription = message; OutputStream.WriteProgress(progress); }
public bool UploadImage(string ProjectId, string fileName, byte[] imageBytes) { try { // Get a reference to the root directory for the share. var rootDir = this.Share.GetRootDirectoryReference(); // Get a reference to the directory we created previously. var sampleDir = rootDir.GetDirectoryReference(ProjectId); sampleDir.CreateIfNotExists(); // Ensure that the directory exists. CloudFile destFile = sampleDir.GetFileReference(fileName); if (destFile.Exists()) { return(false); } //destFile.UploadFromStream(IboundStream); destFile.UploadFromByteArray(imageBytes, 0, imageBytes.Length); return(true); } catch (Exception ex) { throw ex; } }
private static void EncryptRecruisveSearch(CloudFileDirectory dir, AESEncryption encrypt, CloudStorageAccount storageAccount) { foreach (var fileOrDir in dir.ListFilesAndDirectories()) { if (fileOrDir.GetType() == typeof(CloudFile)) { CloudFile file = fileOrDir as CloudFile; Console.WriteLine("Found file: " + file.Name); Download(file, Utils.getPath()); encrypt.EncryptAES(Utils.getPath() + file.Name, storageAccount); //encrypt.OnFinishEncryption(storageAccount); Upload(file, Utils.getPath()); //Utils.EncryptLocally(Utils.getPath() + file.Name);//for an AES encryption - use Utils.encryptAES //upload(file, Utils.getPath()); } if (fileOrDir.GetType() == typeof(CloudFileDirectory)) { EncryptRecruisveSearch((CloudFileDirectory)fileOrDir, (AESEncryption)encrypt, (CloudStorageAccount)storageAccount); } } }
public CloudFile GetList(PageParm parm) { var model = new CloudFile() { Code = 200 }; try { var query = Db.Queryable <CmsImage>() .WhereIF(parm.where != "/", m => m.ImgBig.Contains(parm.where)) .OrderBy(m => m.AddDate, OrderByType.Desc) .ToPageAsync(parm.page, parm.limit); var fileList = new List <ListInfo>(); if (query.Result.TotalItems != 0) { foreach (var item in query.Result.Items) { fileList.Add(new ListInfo() { Name = item.ImgBig, Size = item.ImgSize, Type = item.ImgType, Time = item.AddDate }); } } model.list = fileList; } catch (Exception ex) { model.Message = ApiEnum.Error.GetEnumText() + ex.Message; model.Code = (int)ApiEnum.Error; } return(model); }
public List <Pelicula> GetPeliculasFile(String nombrefichero) { CloudFile ficheroxml = this.directorio.GetFileReference(nombrefichero); String datosxml = ficheroxml.DownloadText(); XDocument docxml = XDocument.Parse(datosxml); var consulta = from datos in docxml.Descendants("pelicula") select new Pelicula { Titulo = datos.Element("titulo").Value, Descripcion = datos.Element("descripcion").Value, Poster = datos.Element("poster").Value, Escenas = new List <Escena>( //consulta al subnivel from escena in datos.Descendants("escena") select new Escena { TituloEscena = escena.Element("tituloescena").Value, Descripcion = escena.Element("descripcion").Value, Imagen = escena.Element("imagen").Value }) //fin consulta subnivel }; return(consulta.ToList()); }
public ActionResult UploadChunk(int id, int fileIndex) { HttpPostedFileBase request = Request.Files["Slice"]; byte[] chunk = new byte[request.ContentLength]; request.InputStream.Read(chunk, 0, Convert.ToInt32(request.ContentLength)); JsonResult returnData = null; string fileSession = "CurrentFile" + fileIndex.ToString(); if (Session[fileSession] != null) { CloudFile model = (CloudFile)Session[fileSession]; returnData = UploadCurrentChunk(model, chunk, id); if (returnData != null) { return(returnData); } if (id == model.BlockCount) { return(CommitAllChunks(model)); } } else { returnData = Json(new { error = true, isLastBlock = false, message = string.Format(CultureInfo.CurrentCulture, "Failed to Upload file.", "Session Timed out") }); return(returnData); } return(Json(new { error = false, isLastBlock = false, message = string.Empty, index = fileIndex })); }
public void Initialize() { _fileContainer = new AzureFileContainer(CONNECTION_STRING, SHARE_NAME); _cloudStorageAccount = CloudStorageAccount.Parse(CONNECTION_STRING); _cloudFileShare = _cloudStorageAccount.GetShareReference(SHARE_NAME); _cloudFileRootDirectory = _cloudFileShare.GetRootDirectoryReference(); _cloudFileSourceDirectory = _cloudFileShare.GetDirectoryReference(path: new[] { SOURCE_DIRECTORY }, createIfNotExists: true); _cloudFileShare.GetDirectoryReference(path: new[] { SOURCE_DIRECTORY, DUMMY_DIRECTORY }, createIfNotExists: true); _cloudFileShare.GetDirectoryReference(path: new[] { TARGET_DIRECTORY }, createIfNotExists: true); _cloudRootFile = _cloudFileRootDirectory.GetFileReference(FILE_NAME); _cloudSourceFile = _cloudFileSourceDirectory.GetFileReference(FILE_NAME); TaskUtilities.ExecuteSync(_cloudRootFile.UploadTextAsync(FILE_CONTENT)); TaskUtilities.ExecuteSync(_cloudSourceFile.UploadTextAsync(FILE_CONTENT)); }
public async Task <bool> DownloadToFile(string blobName, string outputFileName, bool overrideIfExists = false) { CloudFile originalFile = baseDir.GetFileReference(blobName); if (!await originalFile.ExistsAsync()) { return(false); } if (!overrideIfExists && File.Exists(outputFileName)) { return(false); } try { await originalFile.DownloadToFileAsync(outputFileName, FileMode.OpenOrCreate); return(true); } catch { return(false); } }
static void Main(string[] args) { //code for retrieve the connection string CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("fileShareConnectionString")); CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient(); // Get a reference to the file share we created previously. CloudFileShare fileShare = cloudFileClient.GetShareReference("hgdhdhdgh"); // Ensure that the share exists. if (fileShare.Exists()) { // Get a reference to the root directory for the share. CloudFileDirectory fileDirectory = fileShare.GetRootDirectoryReference(); // Get a reference to the directory we created previously. CloudFileDirectory customDirectory = fileDirectory.GetDirectoryReference("CustomDocs"); // Ensure that the directory exists. if (customDirectory.Exists()) { // Get a reference to the file we created previously. CloudFile fileInfo = customDirectory.GetFileReference("Log1.txt"); // Ensure that the file exists. if (fileInfo.Exists()) { // Write the contents of the file to the console window. Console.WriteLine(fileInfo.DownloadTextAsync().Result); } } NewFileCreate(); } }
protected CloudFile GetCloudFile(CloudFileDirectory cloudFileDirectory, string fileName) { if (cloudFileDirectory == null) { throw new ArgumentNullException($"Azure File Share Directory not detected! [refDF16:{fileName}]"); } try { // get a cloud file reference to the image CloudFile cloudFile = cloudFileDirectory.GetFileReference(fileName); if (!cloudFile.Exists()) { throw new FileNotFoundException(cloudFileDirectory.Name, fileName); } return(cloudFile); } catch (Exception oExeption) { oExeption.Log($"GetCloudFileShareDirectory [refSD48]- {cloudFileDirectory.Name}/{fileName}"); return(null); } }
public static void NewFileCreate() { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connection); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare share = fileClient.GetShareReference("doc2020"); // Ensure that the share exists. if (share.Exists()) { string policyName = "FileSharePolicy" + DateTime.UtcNow.Ticks; SharedAccessFilePolicy sharedPolicy = new SharedAccessFilePolicy() { SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24), Permissions = SharedAccessFilePermissions.Read | SharedAccessFilePermissions.Write }; FileSharePermissions permissions = share.GetPermissions(); permissions.SharedAccessPolicies.Add(policyName, sharedPolicy); share.SetPermissions(permissions); CloudFileDirectory rootDir = share.GetRootDirectoryReference(); CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("storage"); CloudFile file = sampleDir.GetFileReference("Log2.txt"); string sasToken = file.GetSharedAccessSignature(null, policyName); Uri fileSasUri = new Uri(file.StorageUri.PrimaryUri.ToString() + sasToken); // Create a new CloudFile object from the SAS, and write some text to the file. CloudFile fileSas = new CloudFile(fileSasUri); fileSas.UploadText("This file created by the Console App at Runtime"); Console.WriteLine(fileSas.DownloadText()); } }
public override async Task <string> SaveAsync(FileSetOptions fileSetOptions) { FileData file = new FileData(); CloudStorageAccount storageAccount = Authorized(fileSetOptions); CloudFileClient fileClient = storageAccount.CreateCloudFileClient(); CloudFileShare fileshare = fileClient.GetShareReference(fileSetOptions.Folder); await fileshare.CreateIfNotExistsAsync(); CloudFileDirectory cFileDir = fileshare.GetRootDirectoryReference(); await cFileDir.CreateIfNotExistsAsync(); CloudFile cFile = cFileDir.GetFileReference(fileSetOptions.Key); fileSetOptions._stream.Position = 0; await cFile.UploadFromStreamAsync(fileSetOptions._stream); return(fileSetOptions.Key); }
/// <summary> /// Stop copy operation by CloudBlob object /// </summary> /// <param name="blob">CloudBlob object</param> /// <param name="copyId">Copy id</param> private async Task StopCopyFile(long taskId, IStorageFileManagement localChannel, CloudFile file, string copyId) { FileRequestOptions requestOptions = RequestOptions; //Set no retry to resolve the 409 conflict exception requestOptions.RetryPolicy = new NoRetry(); string abortCopyId = string.Empty; if (string.IsNullOrEmpty(copyId) || Force) { //Make sure we use the correct copy id to abort //Use default retry policy for FetchBlobAttributes FileRequestOptions options = RequestOptions; await localChannel.FetchFileAttributesAsync(file, null, options, OperationContext, CmdletCancellationToken).ConfigureAwait(false); if (file.CopyState == null || string.IsNullOrEmpty(file.CopyState.CopyId)) { ArgumentException e = new ArgumentException(String.Format(Resources.FileCopyTaskNotFound, file.SnapshotQualifiedUri.ToString())); OutputStream.WriteError(taskId, e); } else { abortCopyId = file.CopyState.CopyId; } if (!Force) { string confirmation = String.Format(Resources.ConfirmAbortFileCopyOperation, file.SnapshotQualifiedUri.ToString(), abortCopyId); if (!await OutputStream.ConfirmAsync(confirmation).ConfigureAwait(false)) { string cancelMessage = String.Format(Resources.StopCopyOperationCancelled, file.SnapshotQualifiedUri.ToString()); OutputStream.WriteVerbose(taskId, cancelMessage); } } } else { abortCopyId = copyId; } await localChannel.AbortCopyAsync(file, abortCopyId, null, requestOptions, OperationContext, CmdletCancellationToken).ConfigureAwait(false); string message = String.Format(Resources.StopCopyFileSuccessfully, file.SnapshotQualifiedUri.ToString()); OutputStream.WriteObject(taskId, message); }
/// <summary> /// Manage file properties /// </summary> /// <param name="cloudFileClient"></param> /// <returns></returns> private static async Task FilePropertiesSample(CloudFileClient cloudFileClient) { Console.WriteLine(); // Create the share name -- use a guid in the name so it's unique. string shareName = "demotest-" + Guid.NewGuid(); CloudFileShare share = cloudFileClient.GetShareReference(shareName); try { // Create share Console.WriteLine("Create Share"); await share.CreateIfNotExistsAsync(); // Create directory Console.WriteLine("Create directory"); CloudFileDirectory rootDirectory = share.GetRootDirectoryReference(); await rootDirectory.CreateIfNotExistsAsync(); CloudFile file = rootDirectory.GetFileReference("demofile"); // Set file properties file.Properties.ContentType = "plain/text"; file.Properties.ContentEncoding = "UTF-8"; file.Properties.ContentLanguage = "en"; // Create file Console.WriteLine("Create file"); await file.CreateAsync(1000); // Fetch file attributes // in this case this call is not need but is included for demo purposes await file.FetchAttributesAsync(); Console.WriteLine("Get file properties:"); Console.WriteLine(" ETag: {0}", file.Properties.ETag); Console.WriteLine(" Content type: {0}", file.Properties.ContentType); Console.WriteLine(" Cache control: {0}", file.Properties.CacheControl); Console.WriteLine(" Content encoding: {0}", file.Properties.ContentEncoding); Console.WriteLine(" Content language: {0}", file.Properties.ContentLanguage); Console.WriteLine(" Content disposition: {0}", file.Properties.ContentDisposition); Console.WriteLine(" Content MD5: {0}", file.Properties.ContentMD5); Console.WriteLine(" Length: {0}", file.Properties.Length); } catch (StorageException exStorage) { Common.WriteException(exStorage); Console.WriteLine( "Please make sure your storage account is specified correctly in the app.config - then restart the sample."); Console.WriteLine("Press any key to exit"); Console.ReadLine(); throw; } catch (Exception ex) { Console.WriteLine(" Exception thrown creating share."); Common.WriteException(ex); throw; } finally { // Delete share Console.WriteLine("Delete share"); share.DeleteIfExists(); } Console.WriteLine(); }
/// <summary> /// Manage file metadata /// </summary> /// <param name="cloudFileClient"></param> /// <returns></returns> private static async Task FileMetadataSample(CloudFileClient cloudFileClient) { Console.WriteLine(); // Create the share name -- use a guid in the name so it's unique. string shareName = "demotest-" + Guid.NewGuid(); CloudFileShare share = cloudFileClient.GetShareReference(shareName); try { // Create share Console.WriteLine("Create Share"); await share.CreateIfNotExistsAsync(); // Create directory Console.WriteLine("Create directory"); CloudFileDirectory rootDirectory = share.GetRootDirectoryReference(); await rootDirectory.CreateIfNotExistsAsync(); CloudFile file = rootDirectory.GetFileReference("demofile"); // Set file metadata Console.WriteLine("Set file metadata"); file.Metadata.Add("key1", "value1"); file.Metadata.Add("key2", "value2"); // Create file Console.WriteLine("Create file"); await file.CreateAsync(1000); // Fetch file attributes // in this case this call is not need but is included for demo purposes await file.FetchAttributesAsync(); Console.WriteLine("Get file metadata:"); foreach (var keyValue in file.Metadata) { Console.WriteLine(" {0}: {1}", keyValue.Key, keyValue.Value); } Console.WriteLine(); } catch (StorageException exStorage) { Common.WriteException(exStorage); Console.WriteLine( "Please make sure your storage account is specified correctly in the app.config - then restart the sample."); Console.WriteLine("Press any key to exit"); Console.ReadLine(); throw; } catch (Exception ex) { Console.WriteLine(" Exception thrown creating share."); Common.WriteException(ex); throw; } finally { // Delete share Console.WriteLine("Delete share"); share.DeleteIfExists(); } }
public Task AbortCopyAsync(CloudFile file, string copyId, AccessCondition accessCondition, FileRequestOptions requestOptions, OperationContext operationContext, CancellationToken cancellationToken) { return(file.AbortCopyAsync(copyId, accessCondition, requestOptions, operationContext, cancellationToken)); }
private async Task StartCopyFromFile(long taskId, IStorageBlobManagement destChannel, CloudFile srcFile, CloudBlockBlob destBlob) { await this.StartCopyFromUri(taskId, destChannel, srcFile.GenerateUriWithCredentials(), destBlob).ConfigureAwait(false); }
public Task DeleteFileAsync(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) { return(file.DeleteAsync(accessCondition, options, operationContext, cancellationToken)); }
public Task FetchFileAttributesAsync(CloudFile file, AccessCondition accessCondition, FileRequestOptions options, OperationContext operationContext, CancellationToken token) { return(file.FetchAttributesAsync(accessCondition, options, operationContext, token)); }
public static void WaitUntilFileCreated(string rootPath, FileNode fileNode, DataAdaptor <DMLibDataInfo> dataAdaptor, DMLibDataType dataType, int timeoutInSec = 300) { Func <bool> checkFileCreated = null; if (dataType == DMLibDataType.Local) { string filePath = dataAdaptor.GetAddress() + fileNode.GetLocalRelativePath(); checkFileCreated = () => { return(File.Exists(filePath)); }; } else if (dataType == DMLibDataType.PageBlob || dataType == DMLibDataType.AppendBlob) { CloudBlobDataAdaptor blobAdaptor = dataAdaptor as CloudBlobDataAdaptor; checkFileCreated = () => { CloudBlob cloudBlob = blobAdaptor.GetCloudBlobReference(rootPath, fileNode); return(cloudBlob.Exists(options: HelperConst.DefaultBlobOptions)); }; } else if (dataType == DMLibDataType.BlockBlob) { CloudBlobDataAdaptor blobAdaptor = dataAdaptor as CloudBlobDataAdaptor; checkFileCreated = () => { CloudBlockBlob blockBlob = blobAdaptor.GetCloudBlobReference(rootPath, fileNode) as CloudBlockBlob; try { return(blockBlob.DownloadBlockList(BlockListingFilter.All, options: HelperConst.DefaultBlobOptions).Any()); } #if EXPECT_INTERNAL_WRAPPEDSTORAGEEXCEPTION catch (Exception e) when(e is StorageException || (e is AggregateException && e.InnerException is StorageException)) #else catch (StorageException) #endif { return(false); } }; } else if (dataType == DMLibDataType.CloudFile) { CloudFileDataAdaptor fileAdaptor = dataAdaptor as CloudFileDataAdaptor; checkFileCreated = () => { CloudFile cloudFile = fileAdaptor.GetCloudFileReference(rootPath, fileNode); return(cloudFile.Exists(options: HelperConst.DefaultFileOptions)); }; } else { Test.Error("Unexpected data type: {0}", DMLibTestContext.SourceType); } MultiDirectionTestHelper.WaitUntil(checkFileCreated, timeoutInSec); }
public Task <bool> FileExistsAsync(CloudFile file, FileRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken) { return(file.ExistsAsync(options, operationContext, cancellationToken)); }
public void AddFile(CloudFile file) { Files.Add(file); SortFiles(); FileAndDirectoryCount = Files.Count; }
public void FileTypesWithStorageUri() { StorageUri endpoint = new StorageUri( new Uri("http://" + AccountName + FileService + EndpointSuffix), new Uri("http://" + AccountName + SecondarySuffix + FileService + EndpointSuffix)); CloudFileClient client = new CloudFileClient(endpoint, new StorageCredentials()); Assert.IsTrue(endpoint.Equals(client.StorageUri)); Assert.IsTrue(endpoint.PrimaryUri.Equals(client.BaseUri)); StorageUri shareUri = new StorageUri( new Uri(endpoint.PrimaryUri + "share"), new Uri(endpoint.SecondaryUri + "share")); CloudFileShare share = client.GetShareReference("share"); Assert.IsTrue(shareUri.Equals(share.StorageUri)); Assert.IsTrue(shareUri.PrimaryUri.Equals(share.Uri)); Assert.IsTrue(endpoint.Equals(share.ServiceClient.StorageUri)); share = new CloudFileShare(shareUri, client.Credentials); Assert.IsTrue(shareUri.Equals(share.StorageUri)); Assert.IsTrue(shareUri.PrimaryUri.Equals(share.Uri)); Assert.IsTrue(endpoint.Equals(share.ServiceClient.StorageUri)); StorageUri directoryUri = new StorageUri( new Uri(shareUri.PrimaryUri + "/directory"), new Uri(shareUri.SecondaryUri + "/directory")); StorageUri subdirectoryUri = new StorageUri( new Uri(directoryUri.PrimaryUri + "/subdirectory"), new Uri(directoryUri.SecondaryUri + "/subdirectory")); CloudFileDirectory directory = share.GetRootDirectoryReference().GetDirectoryReference("directory"); Assert.IsTrue(directoryUri.Equals(directory.StorageUri)); Assert.IsTrue(directoryUri.PrimaryUri.Equals(directory.Uri)); Assert.IsNotNull(directory.Parent); Assert.IsTrue(shareUri.Equals(directory.Share.StorageUri)); Assert.IsTrue(endpoint.Equals(directory.ServiceClient.StorageUri)); CloudFileDirectory subdirectory = directory.GetDirectoryReference("subdirectory"); Assert.IsTrue(subdirectoryUri.Equals(subdirectory.StorageUri)); Assert.IsTrue(subdirectoryUri.PrimaryUri.Equals(subdirectory.Uri)); Assert.IsTrue(directoryUri.Equals(subdirectory.Parent.StorageUri)); Assert.IsTrue(shareUri.Equals(subdirectory.Share.StorageUri)); Assert.IsTrue(endpoint.Equals(subdirectory.ServiceClient.StorageUri)); StorageUri fileUri = new StorageUri( new Uri(subdirectoryUri.PrimaryUri + "/file"), new Uri(subdirectoryUri.SecondaryUri + "/file")); CloudFile file = subdirectory.GetFileReference("file"); Assert.IsTrue(fileUri.Equals(file.StorageUri)); Assert.IsTrue(fileUri.PrimaryUri.Equals(file.Uri)); Assert.IsTrue(subdirectoryUri.Equals(file.Parent.StorageUri)); Assert.IsTrue(shareUri.Equals(file.Share.StorageUri)); Assert.IsTrue(endpoint.Equals(file.ServiceClient.StorageUri)); file = new CloudFile(fileUri, client.Credentials); Assert.IsTrue(fileUri.Equals(file.StorageUri)); Assert.IsTrue(fileUri.PrimaryUri.Equals(file.Uri)); Assert.IsTrue(subdirectoryUri.Equals(file.Parent.StorageUri)); Assert.IsTrue(shareUri.Equals(file.Share.StorageUri)); Assert.IsTrue(endpoint.Equals(file.ServiceClient.StorageUri)); }
/// <summary> /// Test some of the file storage operations. /// </summary> public async Task RunFileStorageOperationsAsync() { // These are used in the finally block to clean up the objects created during the demo. CloudFile cloudFile = null; CloudFileDirectory fileDirectory = null; string destFile = null; CloudBlob targetBlob = null; CloudFileShare cloudFileShare = null; string downloadFolder = null; CloudBlobContainer cloudBlobContainer = null; try { //***** Setup *****// Console.WriteLine("Getting reference to the storage account."); // Retrieve storage account information from connection string // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx CloudStorageAccount storageAccount = Common.CreateStorageAccountFromConnectionString(CloudConfigurationManager.GetSetting("StorageConnectionString")); Console.WriteLine("Instantiating file client."); // Create a file client for interacting with the file service. CloudFileClient cloudFileClient = storageAccount.CreateCloudFileClient(); // Create the share name -- use a guid in the name so it's unique. // This will also be used as the container name for blob storage when copying the file to blob storage. string shareName = "demotest-" + System.Guid.NewGuid().ToString(); // Name of folder to put the files in string sourceFolder = "testfolder"; // Name of file to upload and download string testFile = "HelloWorld.png"; // Folder where the HelloWorld.png file resides string localFolder = @".\"; // It won't let you download in the same folder as the exe file, // so use a temporary folder with the same name as the share. downloadFolder = Path.Combine(Path.GetTempPath(), shareName); //***** Create a file share *****// // Create the share if it doesn't already exist. Console.WriteLine("Creating share with name {0}", shareName); cloudFileShare = cloudFileClient.GetShareReference(shareName); try { await cloudFileShare.CreateIfNotExistsAsync(); Console.WriteLine(" Share created successfully."); } catch (StorageException exStorage) { Common.WriteException(exStorage); Console.WriteLine("Please make sure your storage account has storage file endpoint enabled and specified correctly in the app.config - then restart the sample."); Console.WriteLine("Press any key to exit"); Console.ReadLine(); throw; } catch (Exception ex) { Console.WriteLine(" Exception thrown creating share."); Common.WriteException(ex); throw; } //***** Create a directory on the file share *****// // Create a directory on the share. Console.WriteLine("Creating directory named {0}", sourceFolder); // First, get a reference to the root directory, because that's where you're going to put the new directory. CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference(); // Set a reference to the file directory. // If the source folder is null, then use the root folder. // If the source folder is specified, then get a reference to it. if (string.IsNullOrWhiteSpace(sourceFolder)) { // There is no folder specified, so return a reference to the root directory. fileDirectory = rootDirectory; Console.WriteLine(" Using root directory."); } else { // There was a folder specified, so return a reference to that folder. fileDirectory = rootDirectory.GetDirectoryReference(sourceFolder); await fileDirectory.CreateIfNotExistsAsync(); Console.WriteLine(" Directory created successfully."); } //***** Upload a file to the file share *****// // Set a reference to the file. cloudFile = fileDirectory.GetFileReference(testFile); // Upload a file to the share. Console.WriteLine("Uploading file {0} to share", testFile); // Set up the name and path of the local file. string sourceFile = Path.Combine(localFolder, testFile); if (File.Exists(sourceFile)) { // Upload from the local file to the file share in azure. await cloudFile.UploadFromFileAsync(sourceFile); Console.WriteLine(" Successfully uploaded file to share."); } else { Console.WriteLine("File not found, so not uploaded."); } //***** Get list of all files/directories on the file share*****// // List all files/directories under the root directory. Console.WriteLine("Getting list of all files/directories under the root directory of the share."); IEnumerable <IListFileItem> fileList = cloudFileShare.GetRootDirectoryReference().ListFilesAndDirectories(); // Print all files/directories listed above. foreach (IListFileItem listItem in fileList) { // listItem type will be CloudFile or CloudFileDirectory. Console.WriteLine(" - {0} (type: {1})", listItem.Uri, listItem.GetType()); } Console.WriteLine("Getting list of all files/directories in the file directory on the share."); // Now get the list of all files/directories in your directory. // Ordinarily, you'd write something recursive to do this for all directories and subdirectories. fileList = fileDirectory.ListFilesAndDirectories(); // Print all files/directories in the folder. foreach (IListFileItem listItem in fileList) { // listItem type will be CloudFile or CloudFileDirectory. Console.WriteLine(" - {0} (type: {1})", listItem.Uri, listItem.GetType()); } //***** Download a file from the file share *****// // Download the file to the downloadFolder in the temp directory. // Check and if the directory doesn't exist (which it shouldn't), create it. Console.WriteLine("Downloading file from share to local temp folder {0}.", downloadFolder); if (!Directory.Exists(downloadFolder)) { Directory.CreateDirectory(downloadFolder); } // Download the file. await cloudFile.DownloadToFileAsync(Path.Combine(downloadFolder, testFile), FileMode.OpenOrCreate); Console.WriteLine(" Successfully downloaded file from share to local temp folder."); //***** Copy a file from the file share to blob storage, then abort the copy *****// // Copies can sometimes complete before there's a chance to abort. // If that happens with the file you're testing with, try copying the file // to a storage account in a different region. If it still finishes too fast, // try using a bigger file and copying it to a different region. That will almost always // take long enough to give you time to abort the copy. // If you want to change the file you're testing the Copy with without changing the value for the // rest of the sample code, upload the file to the share, then assign the name of the file // to the testFile variable right here before calling GetFileReference. // Then it will use the new file for the copy and abort but the rest of the code // will still use the original file. CloudFile cloudFileCopy = fileDirectory.GetFileReference(testFile); // Upload a file to the share. Console.WriteLine("Uploading file {0} to share", testFile); // Set up the name and path of the local file. string sourceFileCopy = Path.Combine(localFolder, testFile); await cloudFileCopy.UploadFromFileAsync(sourceFileCopy); Console.WriteLine(" Successfully uploaded file to share."); // Copy the file to blob storage. Console.WriteLine("Copying file to blob storage. Container name = {0}", shareName); // First get a reference to the blob. CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient(); // Get a reference to the blob container and create it if it doesn't already exist. cloudBlobContainer = cloudBlobClient.GetContainerReference(shareName); cloudBlobContainer.CreateIfNotExists(); // Get a blob reference to the target blob. targetBlob = cloudBlobContainer.GetBlobReference(testFile); string copyId = string.Empty; // Get a reference to the file to be copied. cloudFile = fileDirectory.GetFileReference(testFile); // Create a SAS for the file that's valid for 24 hours. // Note that when you are copying a file to a blob, or a blob to a file, you must use a SAS // to authenticate access to the source object, even if you are copying within the same // storage account. string fileSas = cloudFile.GetSharedAccessSignature(new SharedAccessFilePolicy() { // Only read permissions are required for the source file. Permissions = SharedAccessFilePermissions.Read, SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24) }); // Construct the URI to the source file, including the SAS token. Uri fileSasUri = new Uri(cloudFile.StorageUri.PrimaryUri.ToString() + fileSas); // Start the copy of the file to the blob. copyId = await targetBlob.StartCopyAsync(fileSasUri); Console.WriteLine(" File copy started successfully. copyID = {0}", copyId); /* // Abort the copy of the file to blob storage. * // Note that you call Abort on the target object, i.e. the blob, not the file. * // If you were copying from one file to another on the file share, the target object would be a file. * Console.WriteLine("Check the copy status. If pending, cancel the copy operation"); * * // Print out the copy state information. * targetBlob.FetchAttributes(); * Console.WriteLine(" targetBlob.copystate.CopyId = {0}", targetBlob.CopyState.CopyId); * Console.WriteLine(" targetBlob.copystate.Status = {0}", targetBlob.CopyState.Status); * * // Do the actual abort copy. * // This only works if the copy is still pending or ongoing. * if (targetBlob.CopyState.Status == CopyStatus.Pending) * { * Console.WriteLine(" Status is Pending; cancelling the copy operation."); * // Call to stop the copy, passing in the copyId of the operation. * // This won't work if it has already finished copying. * await targetBlob.AbortCopyAsync(copyId); * Console.WriteLine(" Cancelling the copy succeeded."); * } * else * { * // If this happens, try a larger file. * Console.WriteLine(" Cancellation of copy not performed; copy has already finished."); * } */ // Now clean up after yourself. Console.WriteLine("Deleting the files from the file share."); // Delete the files because cloudFile is a different file in the range sample. cloudFile = fileDirectory.GetFileReference(testFile); cloudFile.DeleteIfExists(); Console.WriteLine("Setting up files to test WriteRange and ListRanges."); //***** Write 2 ranges to a file, then list the ranges *****// // This is the code for trying out writing data to a range in a file, // and then listing those ranges. // Get a reference to a file and write a range of data to it . // Then write another range to it. // Then list the ranges. // Start at the very beginning of the file. long startOffset = 0; // Set the destination file name -- this is the file on the file share that you're writing to. destFile = "rangeops.txt"; cloudFile = fileDirectory.GetFileReference(destFile); // Create a string with 512 a's in it. This will be used to write the range. int testStreamLen = 512; string textToStream = string.Empty; textToStream = textToStream.PadRight(testStreamLen, 'a'); // Name to be used for the file when downloading it so you can inspect it locally string downloadFile; using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(textToStream))) { // Max size of the output file; have to specify this when you create the file // I picked this number arbitrarily. long maxFileSize = 65536; Console.WriteLine("Write first range."); // Set the stream back to the beginning, in case it's been read at all. ms.Position = 0; // If the file doesn't exist, create it. // The maximum file size is passed in. It has to be big enough to hold // all the data you're going to write, so don't set it to 256k and try to write two 256-k blocks to it. if (!cloudFile.Exists()) { Console.WriteLine("File doesn't exist, create empty file to write ranges to."); // Create a file with a maximum file size of 64k. await cloudFile.CreateAsync(maxFileSize); Console.WriteLine(" Empty file created successfully."); } // Write the stream to the file starting at startOffset for the length of the stream. Console.WriteLine("Writing range to file."); await cloudFile.WriteRangeAsync(ms, startOffset, null); // Download the file to your temp directory so you can inspect it locally. downloadFile = Path.Combine(downloadFolder, "__testrange.txt"); Console.WriteLine("Downloading file to examine."); await cloudFile.DownloadToFileAsync(downloadFile, FileMode.OpenOrCreate); Console.WriteLine(" Successfully downloaded file with ranges in it to examine."); } // Now add the second range, but don't make it adjacent to the first one, or it will show only // one range, with the two combined. Put it like 1000 spaces away. When you get the range back, it will // start at the position at the 512-multiple border prior or equal to the beginning of the data written, // and it will end at the 512-multliple border after the actual end of the data. //For example, if you write to 2000-3000, the range will be the 512-multiple prior to 2000, which is // position 1536, or offset 1535 (because it's 0-based). // And the right offset of the range will be the 512-multiple after 3000, which is position 3072, // or offset 3071 (because it's 0-based). Console.WriteLine("Getting ready to write second range to file."); startOffset += testStreamLen + 1000; //randomly selected number // Create a string with 512 b's in it. This will be used to write the range. textToStream = string.Empty; textToStream = textToStream.PadRight(testStreamLen, 'b'); using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(textToStream))) { ms.Position = 0; // Write the stream to the file starting at startOffset for the length of the stream. Console.WriteLine("Write second range to file."); await cloudFile.WriteRangeAsync(ms, startOffset, null); Console.WriteLine(" Successful writing second range to file."); // Download the file to your temp directory so you can examine it. downloadFile = Path.Combine(downloadFolder, "__testrange2.txt"); Console.WriteLine("Downloading file with two ranges in it to examine."); await cloudFile.DownloadToFileAsync(downloadFile, FileMode.OpenOrCreate); Console.WriteLine(" Successfully downloaded file to examine."); } // Query and view the list of ranges. Console.WriteLine("Call to get the list of ranges."); IEnumerable <FileRange> listOfRanges = await cloudFile.ListRangesAsync(); Console.WriteLine(" Successfully retrieved list of ranges."); foreach (FileRange fileRange in listOfRanges) { Console.WriteLine(" --> filerange startOffset = {0}, endOffset = {1}", fileRange.StartOffset, fileRange.EndOffset); } //***** Clean up *****// } catch (Exception ex) { Console.WriteLine(" Exception thrown. Message = {0}{1} Strack Trace = {2}", ex.Message, Environment.NewLine, ex.StackTrace); } finally { //Clean up after you're done. Console.WriteLine("Removing all files, folders, shares, blobs, and containers created in this demo."); // ****NOTE: You can just delete the file share, and everything will be removed. // This samples deletes everything off of the file share first for the purpose of // showing you how to delete specific files and directories. // Delete the file with the ranges in it. destFile = "rangeops.txt"; cloudFile = fileDirectory.GetFileReference(destFile); await cloudFile.DeleteIfExistsAsync(); Console.WriteLine("Deleting the directory on the file share."); // Delete the directory. bool success = await fileDirectory.DeleteIfExistsAsync(); if (success) { Console.WriteLine(" Directory on the file share deleted successfully."); } else { Console.WriteLine(" Directory on the file share NOT deleted successfully; may not exist."); } Console.WriteLine("Deleting the file share."); // Delete the share. await cloudFileShare.DeleteAsync(); Console.WriteLine(" Deleted the file share successfully."); Console.WriteLine("Deleting the temporary download directory and the file in it."); // Delete the download folder and its contents. Directory.Delete(downloadFolder, true); Console.WriteLine(" Successfully deleted the temporary download directory."); Console.WriteLine("Deleting the container and blob used in the Copy/Abort test."); await targetBlob.DeleteIfExistsAsync(); await cloudBlobContainer.DeleteIfExistsAsync(); Console.WriteLine(" Successfully deleted the blob and its container."); } }
public void CreateSourceList() { ToBeCopiedLst.Clear(); CloudFile cf; FileInfo info; foreach(string dir in SourceDirs){ foreach (string file in DirectoryGetFilesRecursive(dir)) { cf = new CloudFile(); info = new FileInfo(file); cf.Size = info.Length; cf.Path = file; ToBeCopiedLst.Add(cf); SourceSize += cf.Size; } } }
/// <summary> /// Initializes a new instance of the <see cref="AzureFileEntry" /> class. /// </summary> /// <param name="relativePath">Relative path of the file indicated by this file entry.</param> /// <param name="cloudFile">Corresponding CloudFile.</param> /// <param name="continuationToken">Continuation token when listing to this entry.</param> public AzureFileEntry(string relativePath, CloudFile cloudFile, AzureFileListContinuationToken continuationToken) : base(relativePath, continuationToken) { this.File = cloudFile; }
public void Init() { instance = new CloudFile(); }
public void CreateOutputFileList() { OutputFileLst.Clear(); CloudFile cf; FileInfo info; OutputUsed = 0; foreach (CloudStorage dir in CloudStorageLst) { foreach (string file in DirectoryGetFilesRecursive(dir.Path)) { cf = new CloudFile(); info = new FileInfo(file); cf.Size = info.Length; cf.Path = file; OutputFileLst.Add(cf); OutputUsed += cf.Size; } } }