Example #1
0
        public static void WriteFile(string fullPath, string output, Boolean append)
        {
            ShareFileClient file = new ShareFileClient(CONNECTIONSTRING, SHARENAME, fullPath);

            string original = "";

            if (!append && file.Exists())
            {
                file.Delete();
            }
            if (append)
            {
                if (file.Exists())
                {
                    ShareFileDownloadInfo download = file.Download();
                    original = GetStreamContents(download.Content) + "\n";
                }
            }

            using (Stream outStream = GenerateStreamFromString(original + output)) {
//                if (!file.Exists())
                file.Create(outStream.Length);
                file.UploadRange(new HttpRange(0, outStream.Length), outStream);
            }
        }
Example #2
0
        public List <Metadata> GetMetadatas()
        {
            List <Metadata> metadatas = new List <Metadata>();

            MetadataParser parser =
                new MetadataParser(
                    new MetadataConverter());

            try
            {
                var remaining = new Queue <ShareDirectoryClient>();

                remaining.Enqueue(shareClient.GetDirectoryClient(this.path));
                while (remaining.Count > 0)
                {
                    ShareDirectoryClient dir = remaining.Dequeue();
                    foreach (ShareFileItem item in dir.GetFilesAndDirectories())
                    {
                        if (item.IsDirectory)
                        {
                            remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
                        }
                        else if (item.Name.Contains(FILE_EXTENSION))
                        {
                            Console.WriteLine($"  In {dir.Uri.AbsolutePath} found {item.Name}");

                            ShareFileClient file = dir.GetFileClient(item.Name);

                            ShareFileDownloadInfo fileContents = file.Download();
                            string json;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                fileContents.Content.CopyTo(ms);
                                json = Encoding.UTF8.GetString(ms.ToArray());
                            }

                            // Parse json string
                            Metadata metadata = parser.Parse(json);

                            // Sets the dataset path relative to the Uri and Path specified in constructor
                            var filePath =
                                Path.GetRelativePath(this.path, file.Path)
                                .Replace(FILE_EXTENSION, "");

                            metadata.Dataset.DatasetPath = filePath;

                            metadatas.Add(metadata);
                        }
                    }
                }

                Console.WriteLine($"Found a total of {metadatas.Count} files");
            }
            catch (Exception e)
            {
                Console.WriteLine($"An error ocurred: {e}");
            }

            return(metadatas);
        }
        /// <summary>
        /// Download a file.
        /// </summary>
        /// <param name="connectionString">
        /// A connection string to your Azure Storage account.
        /// </param>
        /// <param name="shareName">
        /// The name of the share to download from.
        /// </param>
        /// <param name="localFilePath">
        /// Path to download the local file.
        /// </param>
        public static void Download(string connectionString, string shareName, string localFilePath)
        {
            #region Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Download
            //@@ string connectionString = "<connection_string>";

            // Name of the share, directory, and file we'll download from
            //@@ string shareName = "sample-share";
            string dirName  = "sample-dir";
            string fileName = "sample-file";

            // Path to the save the downloaded file
            //@@ string localFilePath = @"<path_to_local_file>";

            // Get a reference to the file
            ShareClient          share     = new ShareClient(connectionString, shareName);
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            ShareFileClient      file      = directory.GetFileClient(fileName);

            // Download the file
            ShareFileDownloadInfo download = file.Download();
            using (FileStream stream = File.OpenWrite(localFilePath))
            {
                download.Content.CopyTo(stream);
            }
            #endregion Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Download
        }
Example #4
0
        /// <summary>
        /// Download the file from the current directory to a byte array.
        /// </summary>
        /// <param name="filename">The name of the file to download</param>
        /// <returns>The byte array. Any exceptions are thrown.</returns>
        public byte[] FileDownload(string filename)
        {
            byte[] fileContents = new byte[this.FileLength(filename)];

            if (!this.FileExists(filename))
            {
                throw new Exception("Failed to Download File: '" + filename + "' from '" + Directory.Name + ". File was missing.");
            }

            try
            {
                using (var stream = new MemoryStream(fileContents, true))
                {
                    ShareFileClient       file     = Directory.GetFileClient(filename);
                    ShareFileDownloadInfo download = file.Download();
                    download.Content.CopyTo(stream);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to Download File: '" + filename + "' from '" + Directory.Name + "' Error was: " + ex.Message);
            }

            return(fileContents);
        }
Example #5
0
        public string Download(string connectionString, string shareName, string directory, string fileName)
        {
            // Get a reference to the file
            ShareClient          share           = new ShareClient(connectionString, shareName);
            ShareDirectoryClient directoryClient = share.GetDirectoryClient(directory);
            ShareFileClient      file            = directoryClient.GetFileClient(fileName);

            string localFilePath = @"C:\audio\sales\trusted\Sales\" + file.Name;

            try
            {
                // Download the file
                ShareFileDownloadInfo download = file.Download();

                using (FileStream stream = File.OpenWrite(localFilePath))
                {
                    download.Content.CopyTo(stream);
                }
            }
            catch (Exception e)
            {
                _errors.Add(fileName + ": " + e.Message);
            }

            return(localFilePath);
        }
Example #6
0
        static void Main(string[] args)
        {
            string downloadDirectory = args[0];

            // Create a BlobServiceClient object which will be used to create a container client
            BlobServiceClient blobServiceClient = new BlobServiceClient(AZURE_STORAGE_CONNECTION_STRING);

            // Create a unique name for the container
            string containerName = "startupfiles";

            // Create the container and return a container client object
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName);

            List <BlobItem> blobs = containerClient.GetBlobs().ToList();

            // Download the blob files in parallel - maximum of 10 at a time }
            Parallel.ForEach(blobs, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 10
            }, blob =>
            {
                string downloadFilePath   = Path.Combine(downloadDirectory, blob.Name);
                BlobClient blobClient     = containerClient.GetBlobClient(blob.Name);
                BlobDownloadInfo download = blobClient.Download();

                using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
                {
                    download.Content.CopyTo(downloadFileStream);
                    downloadFileStream.Close();
                }
            });

            string shareName = "startupfiles";

            ShareClient shareClient = new ShareClient(AZURE_FILES_CONNECTION_STRING, shareName);

            if (shareClient.Exists())
            {
                ShareDirectoryClient shareDirectoryClient = shareClient.GetRootDirectoryClient();

                List <ShareFileItem> items = shareDirectoryClient.GetFilesAndDirectories().ToList();
                foreach (ShareFileItem item in items)
                {
                    if (!item.IsDirectory)
                    {
                        string                downloadFilePath = Path.Combine(downloadDirectory, item.Name);
                        ShareFileClient       shareFileClient  = shareDirectoryClient.GetFileClient(item.Name);
                        ShareFileDownloadInfo download         = shareFileClient.Download();
                        using (FileStream downloadFileStream = File.OpenWrite(downloadFilePath))
                        {
                            download.Content.CopyTo(downloadFileStream);
                            downloadFileStream.Close();
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Downloads a file from the Azure Shares files storage by calling <see cref="ShareFileClient.Download(HttpRange, bool, CancellationToken)"/>.
        /// </summary>
        /// <param name="cancellationToken">The token used to signal cancellation request.</param>
        public override void Run(CancellationToken cancellationToken)
        {
            Models.ShareFileDownloadInfo fileDownloadInfo = _fileClient.Download(cancellationToken: cancellationToken);

            // Copy the stream so it is actually downloaded. We use a memory stream as destination to avoid the cost of copying to a file on disk.
            fileDownloadInfo.Content.CopyTo(Stream.Null);

#if DEBUG
            Console.WriteLine($"Downloaded file from {_fileClient.Path}. Content length: {fileDownloadInfo.ContentLength}");
#endif
        }
Example #8
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string connectionString = "DefaultEndpointsProtocol=https;AccountName=m10l2;AccountKey=jKxmGhpm9FWua8lWReXyt9gft5xConbXInXr8IRQj2Z1lWVL6YIWNl/pmqm3EQkdh7YgJlTRPakW8w2GiSfswg==;EndpointSuffix=core.windows.net";
            string containerName    = "test";
            string queueName        = "test";
            string shareName        = "test";
            string blobName         = $"{Guid.NewGuid().ToString()}.json";
            string fileName         = $"{Guid.NewGuid().ToString()}.json";

            BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
            BlobClient          blob      = container.GetBlobClient(blobName);
            Stream uploadStream           = new MemoryStream();

            req.Body.CopyTo(uploadStream);
            uploadStream.Position = 0;
            blob.Upload(uploadStream);
            Stream      result = (await blob.DownloadAsync()).Value.Content;
            QueueClient queue  = new QueueClient(connectionString, queueName);

            Stream messageStream = new MemoryStream();

            req.Body.Position = 0;
            req.Body.CopyTo(messageStream);
            using (StreamReader sr = new StreamReader(messageStream))
            {
                queue.SendMessage(sr.ReadToEnd());
            }

            QueueMessage[] messages = (await queue.ReceiveMessagesAsync()).Value;

            ShareClient          share     = new ShareClient(connectionString, shareName);
            ShareDirectoryClient directory = share.GetRootDirectoryClient();

            Stream fileStream = new MemoryStream();

            req.Body.Position = 0;
            req.Body.CopyTo(fileStream);
            fileStream.Position = 0;
            ShareFileClient file = directory.GetFileClient(fileName);
            await file.CreateAsync(fileStream.Length);

            file.UploadRange(new HttpRange(0, fileStream.Length), fileStream);
            var downloadedFile = file.Download();

            using (StreamReader sr = new StreamReader(downloadedFile.Value.Content))
            {
                string uploadedFile = await sr.ReadToEndAsync();

                return(new OkObjectResult(uploadedFile));
            }
        }
Example #9
0
        /// <summary>
        /// Download the file from the current directory to the stream.
        /// </summary>
        /// <param name="filename">The name of the file to download</param>
        /// <param name="toStream">The stream to copy the file to</param>
        /// <returns>Any exceptions are thrown.</returns>
        public void FileDownload(string filename, Stream toStream)
        {
            if (!this.FileExists(filename))
            {
                throw new Exception("Failed to Download File: '" + filename + "' from '" + Directory.Name + ". File was missing.");
            }

            try
            {
                ShareFileClient       file     = Directory.GetFileClient(filename);
                ShareFileDownloadInfo download = file.Download();
                download.Content.CopyTo(toStream);
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to Download File: '" + filename + "' from '" + Directory.Name + "' Error was: " + ex.Message);
            }
        }
Example #10
0
        public void DownloadFile(string localFilePath)
        {
            // Create a client to a file share
            ShareClient share = new ShareClient(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString, shareName);

            // Get a directory client on file share
            ShareDirectoryClient directory = share.GetDirectoryClient(directoryName);

            // Get a reference to a file and upload it
            ShareFileClient file = directory.GetFileClient(fileName);

            // Download the file
            ShareFileDownloadInfo download = file.Download();

            using (FileStream stream = File.OpenWrite(localFilePath))
            {
                download.Content.CopyTo(stream);
            }
        }
        /// <summary>
        /// Mérodo que descarga el archivo CSV desde una cuenta de storage de Azure Files
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task <ProcessStatus> ReadFrom()
        {
            try
            {
                //Obtenemos el directorio local donde se descargará el archivo .CSV
                string localDirectoryPath = configuration["LocalDirectoryPath"];

                //Creamos el path final donde se guardará el archivo .CSV descargado
                string localFilePath = string.Format("{0}\\{1}", localDirectoryPath, FileName);

                //Obtenemos la cadena de conexión del storage en azure
                string connectionString = configuration["StorageConnectionString"];

                //Creamos un share client a ser usado para manipular el archivo .CSV
                ShareClient share = new ShareClient(connectionString, configuration["ShareClientName"]);

                //Especificamos el directorio donde esta almacenado el archivo en azure
                ShareDirectoryClient shareDirectoryClient = share.GetDirectoryClient(configuration["ShareClientDirectory"]);

                //Generamos la solicitud de acceso al archivo CSV dentro de azure
                ShareFileClient shareFileClient = shareDirectoryClient.GetFileClient(FileName);

                //Cremos el stream de descarga del archivo
                ShareFileDownloadInfo shareFileDownloadInfo = shareFileClient.Download();

                //Generamos un stream para el guardado del archivo a descargar, se reemplazará si ya existe una copia previa
                using (FileStream stream = File.OpenWrite(localFilePath))
                {
                    //Copiamos el stream de descarga al archivo local
                    shareFileDownloadInfo.Content.CopyTo(stream);
                    //Liberamos el recurso del stream
                    await stream.FlushAsync();

                    stream.Close();
                    return(ProcessStatus.Success);
                }
            }
            catch (Exception ex)
            {
                return(ProcessStatus.Fail);
            }
        }
Example #12
0
        private static void DownloadFile(ShareFileClient file, string localFilePath)
        {
            long fileLength = 0;

            using (Stream stream = file.OpenRead()) {
                var properties = file.GetProperties();
                fileLength = stream.Length;
            }
            if (fileLength == 0)    // handle 0 length files
            {
                FileStream fs = File.Create(localFilePath);
                fs.Close();
                return;
            }
            ShareFileDownloadInfo download = file.Download();

            using (FileStream stream = File.OpenWrite(localFilePath)) {
                download.Content.CopyTo(stream);
            }
        }
        public static void DownloadAndCopy(string connectionString, string shareName, string fileName, ILogger log)
        {
            string dirName = "prueba-dir";

            // Get a reference to the file
            ShareClient          share     = new ShareClient(connectionString, shareName);
            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);
            ShareFileClient      file      = directory.GetFileClient(fileName);

            var uriFile = file.Uri;
            //Parametrizar
            var containerName = "destino";

            BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
            BlobClient          blob      = container.GetBlobClient(fileName);

            // Upload local file

            blob.Upload(file.Download().Value.Content);
            log.LogInformation($"Archivo {fileName} copiado en container {containerName}");
        }
        public List <Project> GetProjects(
            ProjectReader reader)
        {
            List <Project> projects = new List <Project>();

            try
            {
                var remaining = new Queue <ShareDirectoryClient>();

                remaining.Enqueue(shareClient.GetDirectoryClient(this.path));
                while (remaining.Count > 0)
                {
                    ShareDirectoryClient dir = remaining.Dequeue();
                    foreach (ShareFileItem item in dir.GetFilesAndDirectories())
                    {
                        if (item.IsDirectory)
                        {
                            remaining.Enqueue(dir.GetSubdirectoryClient(item.Name));
                        }
                        else if (item.Name.Contains(MIPPEN_FILE_SEARCH_TERM))
                        {
                            Console.WriteLine($"  In {dir.Uri.AbsolutePath} found {item.Name}");

                            ShareFileClient file = dir.GetFileClient(item.Name);

                            ShareFileDownloadInfo fileContents = file.Download();
                            Project project;
                            using (var stream = fileContents.Content)
                            {
                                project = reader.Read(stream);
                            }

                            if (project is not null)
                            {
                                projects.Add(project);
                            }
                            //string fileString;
                            //using (MemoryStream ms = new MemoryStream())
                            //{
                            //    fileContents.Content.CopyTo(ms);
                            //    fileString = Encoding.UTF8.GetString(ms.ToArray());
                            //}
                            //
                            //Project project = new Project()
                            //{
                            //    Name = file.Name.Replace(MIPPEN_FILE_EXTENSION, ""),
                            //    Description = fileString
                            //};
                        }
                    }
                }

                Console.WriteLine($"Found a total of {projects.Count} files");
            }
            catch (Exception e)
            {
                Console.WriteLine($"An error ocurred: {e}");
            }

            return(projects);
        }