public async Task <string> ReadFile(string fileShare, string fileName)
        {
            string      json  = "";
            ShareClient share = new ShareClient(connectionString, fileShare);

            if (!share.Exists())
            {
                Exception error = new Exception($"Fileshare {fileName} does not exist ");
                throw error;
            }
            ShareDirectoryClient directory = share.GetRootDirectoryClient();
            ShareFileClient      file      = directory.GetFileClient(fileName);

            if (file.Exists())
            {
                using (Stream fileStream = await file.OpenReadAsync())
                {
                    using (StreamReader reader = new StreamReader(fileStream))
                    {
                        json = reader.ReadToEnd();
                    }
                }
            }
            else
            {
                Exception error = new Exception($"File {fileName} does not exist in Azure storage ");
                throw error;
            }
            return(json);
        }
Example #2
0
        public async Task UploadAsync(ConnectorConfig config, string fullFileName, MemoryStream stream)
        {
            var dirName  = Path.GetDirectoryName(fullFileName).ToString();
            var fileName = Path.GetFileName(fullFileName).ToString();

            ShareClient share = new ShareClient(config.connectionString, config.shareName);

            if (!share.Exists())
            {
                await share.CreateAsync();
            }

            ShareDirectoryClient directory = share.GetDirectoryClient(dirName);

            if (!directory.Exists())
            {
                await directory.CreateAsync();
            }

            ShareFileClient file = directory.GetFileClient(fileName);
            await file.CreateAsync(stream.Length);

            await file.UploadAsync(stream);

            /*
             * await file.UploadRange(
             *      ShareFileRangeWriteType.Update,
             *      new HttpRange(0, stream.Length),
             *      stream);*/
        }
        public async Task <string> SaveFileUri(string fileShare, string fileName, string fileContent)
        {
            ShareClient share = new ShareClient(connectionString, fileShare);

            if (!share.Exists())
            {
                share.Create();
            }
            ShareDirectoryClient rootDir = share.GetRootDirectoryClient();
            ShareFileClient      file    = rootDir.GetFileClient(fileName);

            if (!file.Exists())
            {
                file.Create(fileContent.Length);
            }

            byte[] outBuff = Encoding.ASCII.GetBytes(fileContent);
            ShareFileOpenWriteOptions options = new ShareFileOpenWriteOptions {
                MaxSize = outBuff.Length
            };
            var stream = await file.OpenWriteAsync(true, 0, options);

            stream.Write(outBuff, 0, outBuff.Length);
            stream.Flush();
            return(file.Uri.ToString());
        }
        public IActionResult DownloadFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

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

            if (fileClient.Exists())
            {
                //ShareFileDownloadInfo download = file.Download();
                return(File(fileClient.OpenRead(), "application/octet-stream"));
            }
            else
            {
                return(NotFound());
            }
        }
        public IActionResult DeleteFile(string userId, string fileName)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

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

            fileClient.DeleteIfExists();

            return(Ok());
        }
Example #6
0
        public static ShareFileClient GetAzureFile(string folderName, string fileName)
        {
            string shareName = MyWebUtils.Application.ToLower();

            string connectionString = Properties.Settings.Default.StorageConnectionString;

            ShareClient share = new ShareClient(connectionString, shareName);

            // Create the share if it doesn't already exist
            share.CreateIfNotExists();

            if (share.Exists())
            {
                ShareDirectoryClient subFolder = share.GetRootDirectoryClient();

                foreach (string subFolderName in folderName.Split('\\'))
                {
                    subFolder = subFolder.GetSubdirectoryClient(subFolderName);
                    subFolder.CreateIfNotExists();
                }

                return(subFolder.GetFileClient(fileName));
            }

            return(null);
        }
Example #7
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();
                        }
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Azure File Storage constructor
        /// </summary>
        /// <param name="constr">The connection string</param>
        /// <param name="shareName">The name of the Share</param>
        public AzureFileStorage(string constr, string shareName)
        {
            FileShare = new ShareClient(constr, shareName);

            // Ensure that the share exists.
            if (!FileShare.Exists())
            {
                throw new Exception("Cannot access Azure File Share: " + shareName + ".");
            }
        }
        public IActionResult Post(string userId)
        {
            try
            {
                IFormFileCollection files = Request.Form.Files;
                if (files.Count == 0)
                {
                    return(BadRequest());
                }

                // Get a reference to a share and then create it
                ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);
                if (!share.Exists())
                {
                    share.Create();
                }

                // Get a reference to a directory and create it
                ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());
                if (!directory.Exists())
                {
                    directory.Create();
                }

                // Get a reference to a file and upload it
                ShareFileClient fileClient;

                foreach (IFormFile file in files)
                {
                    fileClient = directory.GetFileClient(file.FileName);

                    fileClient.DeleteIfExists();

                    using (Stream stream = file.OpenReadStream())
                    {
                        fileClient.Create(stream.Length);
                        fileClient.UploadRange(
                            new HttpRange(0, stream.Length),
                            stream);
                    }
                }

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
        public async Task <List <string> > ListFiles(string fileShare)
        {
            List <string> files = new List <string>();
            ShareClient   share = new ShareClient(connectionString, fileShare);

            if (!share.Exists())
            {
                Exception error = new Exception($"Fileshare {fileShare} does not exist ");
                throw error;
            }
            ShareDirectoryClient directory = share.GetRootDirectoryClient();

            foreach (ShareFileItem item in directory.GetFilesAndDirectories())
            {
                files.Add(item.Name);
            }
            return(files);
        }
        public async Task DeleteFile(string fileShare, string fileName)
        {
            ShareClient share = new ShareClient(connectionString, fileShare);

            if (!share.Exists())
            {
                Exception error = new Exception($"Fileshare {fileShare} does not exist ");
                throw error;
            }
            ShareDirectoryClient rootDir = share.GetRootDirectoryClient();
            ShareFileClient      file    = rootDir.GetFileClient(fileName);

            if (file.Exists())
            {
                await file.DeleteAsync();
            }
            else
            {
                Exception error = new Exception($"File {fileName} does not exist in Azure storage ");
                throw error;
            }
        }
        public IActionResult GetFileNames(string userId)
        {
            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(Secret.AzureConnectionString, Secret.AzureFileShareName);

            if (!share.Exists())
            {
                share.Create();
            }

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(Auth0Utils.GetUserFolder());

            if (!directory.Exists())
            {
                directory.Create();
            }

            var files = directory.GetFilesAndDirectories();

            return(Ok(files.ToList()));
        }