file management
4 TopicsSeamless File Management in ASP.NET Core: Azure Blob Storage with Configurable Local Mode
Setting up the Project Before we dive into the implementation, let’s set up the basic structure: Create an ASP.NET Core Web API project. Add the Azure.Storage.BlobsNuGet Package for Azure Blob Storage integration. Update appsettings.json to include storage configurations: "Storage": { "Mode": "Local", "BlobStorage": { "ConnectionString": "YourAzureBlobStorageConnectionString", "ContainerName": "YourContainerName" } } Mode: Determines whether to use Local or Azure Blob Storage. ConnectionString and ContainerName: Azure Blob Storage details. Implementing the Storage Service Here is a custom StorageService that handles file uploads, deletions, and downloads. It dynamically decides the storage location based on the configuration mode. public class StorageService : IStorageService { private readonly IConfiguration _configuration; private readonly IHttpContextAccessor _httpContextAccessor; private readonly string _storageMode; public StorageService(IConfiguration configuration, IHttpContextAccessor httpContextAccessor) { _configuration = configuration; _httpContextAccessor = httpContextAccessor; _storageMode = _configuration["Storage:Mode"]; } public async Task<string> UploadFile(IFormFile file) { try { if (_storageMode == "Local") { var uploads = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads"); var fileName = $"{DateTime.Now:MMddss}-{file.FileName}"; var filePath = Path.Combine(uploads, fileName); if (!Directory.Exists(uploads)) Directory.CreateDirectory(uploads); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); } var host = string.Format("{0}{1}", _httpContextAccessor.HttpContext!.Request.IsHttps ? "https://" : "http://", _httpContextAccessor.HttpContext.Request.Host); return $"{host}/uploads/{fileName}"; } else { var container = new BlobContainerClient(_configuration["Storage:BlobStorage:ConnectionString"], _configuration["Storage:BlobStorage:ContainerName"]); await container.CreateIfNotExistsAsync(PublicAccessType.Blob); var blob = container.GetBlobClient(file.FileName); using (var fileStream = file.OpenReadStream()) { await blob.UploadAsync(fileStream, new BlobHttpHeaders { ContentType = file.ContentType }); } return blob.Uri.ToString(); } } catch (Exception ex) { throw; } } public async Task<bool> DeleteFile(string fileName) { try { if (_storageMode == "Local") { var uploads = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads"); var filePath = Path.Combine(uploads, fileName); if (File.Exists(filePath)) { File.Delete(filePath); return true; } } else { var container = new BlobContainerClient(_configuration["Storage:BlobStorage:ConnectionString"],_configuration["Storage:BlobStorage:ContainerName"]); var blob = container.GetBlobClient(fileName); await blob.DeleteIfExistsAsync(); return true; } } catch (Exception ex) { return false; } } public byte[] DownloadFile(string fileName) { var blobServiceClient = new BlobServiceClient(_configuration["Storage:BlobStorage:ConnectionString"]); var container = blobServiceClient.GetBlobContainerClient(_configuration["Storage:BlobStorage:ContainerName"]); var blobClient = container.GetBlobClient(fileName); using (var memoryStream = new MemoryStream()) { blobClient.DownloadTo(memoryStream); return memoryStream.ToArray(); } } } Static File Middleware Add app.UseStaticFiles(); in Program.cs to serve local files. Usage Example Here’s how to use the UploadFile method in a Web API: public async Task<ServiceResponse<List<ProductPicture>>> UploadImages(List<IFormFile> images) { ServiceResponse<List<ProductPicture>> response = new(); try { List<ProductPicture> pictures = new(); foreach (var file in images) { var imageUrl = await _storageService.UploadFile(file); pictures.Add(new ProductPicture { Url = imageUrl, FileName = Path.GetFileName(imageUrl) }); } response.Data = pictures; } catch (Exception ex) { response.Success = false; response.Message = ex.Message; } return response; } Conclusion By integrating Azure Blob Storage and providing a configurable local storage mode, this approach ensures flexibility for both development and production environments. It reduces development friction while leveraging Azure’s scalability when deployed. For further details, explore the Azure Blob Storage Documentation and Quickstart: Azure Blob Storage.150Views0likes0CommentsHow to Detect Files of the Same Size on your Computer via PowerShell
Learn how to use PowerShell to detect files of the same size on your computer quickly and easily. This article provides a script to automate the process and save storage space. Follow the step-by-step instructions and improve your file management skills with PowerShell automation.6.2KViews3likes1Commentmodifying an existing vb script - for moving certain files (list.txt) to a new location.
This script below ' The list of files to copy. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "\\whitewalker2018\Users\bdogr\Desktop\list.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = FALSE Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objShell Set objShell = CreateObject("Shell.Application") Dim objFolder, objFolderItem ' Get the source path for the copy operation. Dim strSourceFolder Set objFolder = objShell.BrowseForFolder(0, "Select source folder", 0 , "\\whitewalker2018\W4tb on Wv\!_CgTorrents") If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strSourceFolder = objFolderItem.Path ' Get the target path for the copy operation. Dim strTargetFolder Set objFolder = objShell.BrowseForFolder(0, "Select target folder", 0 , "\\whitewalker2018\W4tb on Wv\!_CgTorrents") If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strTargetFolder = objFolderItem.Path Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToCopy, strSourceFilePath, strTargetFilePath Dim strResults, iSuccess, iFailure iSuccess = 0 iFailure = 0 On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToCopy = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, "*" & strFileToCopy & "*") strTargetFilePath = strTargetFolder ' Copy file to specified target folder. Err.Clear objFSO.CopyFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then ' File copied successfully iSuccess = iSuccess + 1 If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, output text to screen Wscript.Echo strFileToCopy & " copied successfully" End If Else ' Error copying file iFailure = iFailure + 1 TextOut "Error " & Err.Number & " (" & Err.Description & ") trying to copy " & strFileToCopy End If Loop strResults = strResults & vbCrLf strResults = strResults & iSuccess & " files copied successfully." & vbCrLf strResults = strResults & iFailure & " files generated errors" & vbCrLf Wscript.Echo strResults Sub TextOut(strText) If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, use direct output Wscript.Echo strText Else strResults = strResults & strText & vbCrLf End If End Sub basically, lets you pick up target and destination folders, then by using a "list.txt" it copies all similarly named files to destination. It has been useful to many people that was written by a user @perhof (original post: https://social.technet.microsoft.com/Forums/en-US/59eed295-a18f-4c78-9b87-cf211c2e58b2/a-script-for-copying-certain-files-to-a-new-location?forum=ITCG) My idea to replace all "copy" word to "move" did fail, i already checkd if a function "objFSO.MoveFile" exists so i dont know the problem here nor the knowledge to read through the code and fix. i am a 3d interior designer. so help me and a thousand more people like me that needs this; thank you ' The list of files to Move. Should be a text file with one file on each row. No paths - just file name. Const strFileList = "\\whitewalker2018\Users\bdogr\Desktop\list.txt" ' Should files be overwriten if they already exist? TRUE or FALSE. Const blnOverwrite = FALSE Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objShell Set objShell = CreateObject("Shell.Application") Dim objFolder, objFolderItem ' Get the source path for the Move operation. Dim strSourceFolder Set objFolder = objShell.BrowseForFolder(0, "Select source folder", 0 , "\\whitewalker2018\W4tb on Wv\!_CgTorrents") If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strSourceFolder = objFolderItem.Path ' Get the target path for the Move operation. Dim strTargetFolder Set objFolder = objShell.BrowseForFolder(0, "Select target folder", 0 , "\\whitewalker2018\W4tb on Wv\!_CgTorrents") If objFolder Is Nothing Then Wscript.Quit Set objFolderItem = objFolder.Self strTargetFolder = objFolderItem.Path Const ForReading = 1 Dim objFileList Set objFileList = objFSO.OpenTextFile(strFileList, ForReading, False) Dim strFileToMove, strSourceFilePath, strTargetFilePath Dim strResults, iSuccess, iFailure iSuccess = 0 iFailure = 0 On Error Resume Next Do Until objFileList.AtEndOfStream ' Read next line from file list and build filepaths strFileToMove = objFileList.Readline strSourceFilePath = objFSO.BuildPath(strSourceFolder, "*" & strFileToMove & "*") strTargetFilePath = strTargetFolder ' Move file to specified target folder. Err.Clear objFSO.MoveFile strSourceFilePath, strTargetFilePath, blnOverwrite If Err.Number = 0 Then ' File copied successfully iSuccess = iSuccess + 1 If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, output text to screen Wscript.Echo strFileToMove & " copied successfully" End If Else ' Error Moveing file iFailure = iFailure + 1 TextOut "Error " & Err.Number & " (" & Err.Description & ") trying to Move " & strFileToMove End If Loop strResults = strResults & vbCrLf strResults = strResults & iSuccess & " files copied successfully." & vbCrLf strResults = strResults & iFailure & " files generated errors" & vbCrLf Wscript.Echo strResults Sub TextOut(strText) If Instr(1, Wscript.Fullname, "cscript.exe", 1) > 0 Then ' Running cscript, use direct output Wscript.Echo strText Else strResults = strResults & strText & vbCrLf End If End Sub1.4KViews0likes3CommentsHow to Management file
Please suggest me how to organize my file using modern site (office 365 group) and how to control the permission Here is my scenario; I have four distinct team in my organization Each different team has different projects underneath Each project holds several documents including versioning/Which are pertinent to respective project leader/ Each project leader expects to upload different report/document which are approved by team leaders I want to have home page (Organization level) which comprises the final version of all document from all projects including the approved one//this site is open to all end users1.3KViews0likes1Comment