public static async Task CreateFileAsync(string account, string key, string share, string fileName, Stream fileContent, string folder = null) { ShareFileClient file = GetShareFileClient(account, key, share, fileName, folder); file.Create(fileContent.Length); await file.UploadRangeAsync(new HttpRange(0, fileContent.Length), fileContent); }
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); } }
public static bool WriteStringToAzureFileShare(string storageConnString, string shareName, string fileName, string content) { if (!azureAuthenticated) { return(false); } try { ShareClient share = new ShareClient(storageConnString, shareName); ShareDirectoryClient directory = share.GetRootDirectoryClient(); directory.DeleteFile(fileName); ShareFileClient file = directory.GetFileClient(fileName); byte[] byteArray = Encoding.UTF8.GetBytes(content); using (MemoryStream stream = new MemoryStream(byteArray)) { file.Create(stream.Length); file.UploadRange( new HttpRange(0, stream.Length), stream); } return(true); } catch (Exception _) { // Log Ex.Message here return(false); } }
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 void UploadFile(string name, Stream file) { ShareFileClient shareFile = root.GetRootDirectoryClient().GetFileClient(name); shareFile.Create(file.Length); shareFile.Upload(file); }
public IActionResult UploadAzure() { try { var file = Request.Form.Files[0]; var folderName = Path.Combine("StaticFiles", "Images"); var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName); if (file.Length > 0) { var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"'); var fullPath = Path.Combine(pathToSave, fileName); var dbPath = Path.Combine(folderName, fileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); } // Get a reference to a share and then create it ShareClient share = new ShareClient("DefaultEndpointsProtocol=https;AccountName=faceidentity;AccountKey=N/gO9IA0exmgv4yH+ZISlxc0wxv2haDMWi2fZoL2/ErfqM/KHAePM4qkwMjnWEXesDvDidDVYJqKAtv3FvaXnQ==;EndpointSuffix=core.windows.net", "employeesimages"); // share.Create(); // Get a reference to a directory and create it ShareDirectoryClient directory = share.GetDirectoryClient("auth"); // directory.Create(); // Get a reference to a file and upload it ShareFileClient fileAzure = directory.GetFileClient(fileName); using (FileStream stream = System.IO.File.OpenRead(fullPath)) { fileAzure.Create(stream.Length); fileAzure.UploadRange( new HttpRange(0, stream.Length), stream); } string fileAzurePath = fileAzure.Uri.ToString(); //System.IO.File.Delete(fullPath); return(Ok(new { fileAzurePath })); } else { return(BadRequest()); } } catch (Exception ex) { return(StatusCode(500, $"Internal server error: {ex}")); } }
/// <summary> /// Upload a file to the stream /// </summary> /// <param name="filename">The name of the file to upload</param> /// <param name="stream">The stream associated with file to upload</param> /// <returns>Any exceptions are thrown.</returns> public void FileUpload(string filename, Stream stream) { try { ShareFileClient file = Directory.GetFileClient(filename); file.Create(stream.Length); file.Upload(stream); // OR file.UploadRange(new HttpRange(0, stream.Length), stream); } catch (Exception ex) { throw new Exception("Failed to Upload File: '" + filename + "' Error was: " + ex.Message); } }
public static void CopyToAzureFileShare(string localPath, string azurePath) { ShareFileClient newFile = new ShareFileClient(CONNECTIONSTRING, SHARENAME, azurePath); if (newFile.Exists()) { newFile.Delete(); } using (FileStream stream = File.OpenRead(localPath)) { newFile.Create(stream.Length); if (stream.Length > 0) { newFile.UploadRange(new HttpRange(0, stream.Length), stream); } } }
public static void AppendToAzureFile(ShareFileClient azureFile, byte[] buffer) { int bufferLength = buffer.Length; azureFile.Create(bufferLength); int maxChunkSize = 4 * 1024; for (int offset = 0; offset < bufferLength; offset += maxChunkSize) { int chunkSize = Math.Min(maxChunkSize, bufferLength - offset); MemoryStream chunk = new MemoryStream(); chunk.Write(buffer, offset, chunkSize); chunk.Position = 0; HttpRange httpRange = new HttpRange(offset, chunkSize); var resp = azureFile.UploadRange(httpRange, chunk); } }
/// <summary> /// Create a share and upload a file. /// </summary> /// <param name="connectionString"> /// A connection string to your Azure Storage account. /// </param> /// <param name="shareName"> /// The name of the share to create and upload to. /// </param> /// <param name="localFilePath"> /// Path of the file to upload. /// </param> public static void Upload(string connectionString, string shareName, string localFilePath) { #region Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Upload // Get a connection string to our Azure Storage account. You can // obtain your connection string from the Azure Portal (click // Access Keys under Settings in the Portal Storage account blade) // or using the Azure CLI with: // // az storage account show-connection-string --name <account_name> --resource-group <resource_group> // // And you can provide the connection string to your application // using an environment variable. //@@ string connectionString = "<connection_string>"; // Name of the share, directory, and file we'll create //@@ string shareName = "sample-share"; string dirName = "sample-dir"; string fileName = "sample-file"; // Path to the local file to upload //@@ string localFilePath = @"<path_to_local_file>"; // Get a reference to a share and then create it ShareClient share = new ShareClient(connectionString, shareName); share.Create(); // Get a reference to a directory and create it ShareDirectoryClient directory = share.GetDirectoryClient(dirName); directory.Create(); // Get a reference to a file and upload it ShareFileClient file = directory.GetFileClient(fileName); using (FileStream stream = File.OpenRead(localFilePath)) { file.Create(stream.Length); file.UploadRange( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } #endregion Snippet:Azure_Storage_Files_Shares_Samples_Sample01a_HelloWorld_Upload }
public void Sample01a_HelloWorld_Traverse() { // Create a temporary Lorem Ipsum file on disk that we can upload string originalPath = CreateTempFile(SampleFileContent); // Get a reference to a share named "sample-share" and then create it string shareName = Randomize("sample-share"); ShareClient share = new ShareClient(ConnectionString, shareName); share.Create(); try { // Create a bunch of directories ShareDirectoryClient first = share.CreateDirectory("first"); first.CreateSubdirectory("a"); first.CreateSubdirectory("b"); ShareDirectoryClient second = share.CreateDirectory("second"); second.CreateSubdirectory("c"); second.CreateSubdirectory("d"); share.CreateDirectory("third"); ShareDirectoryClient fourth = share.CreateDirectory("fourth"); ShareDirectoryClient deepest = fourth.CreateSubdirectory("e"); // Upload a file named "file" ShareFileClient file = deepest.GetFileClient("file"); using (FileStream stream = File.OpenRead(originalPath)) { file.Create(stream.Length); file.UploadRange( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } Sample01a_HelloWorld.Traverse(ConnectionString, shareName); } finally { share.Delete(); } }
public void UploadFile(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 and create directory if not exists ShareDirectoryClient directory = share.GetDirectoryClient(directoryName); directory.CreateIfNotExists(); // Get a reference to a file and upload it ShareFileClient file = directory.GetFileClient(fileName); using (FileStream stream = File.OpenRead(localFilePath)) { file.Create(stream.Length); file.UploadRange( new HttpRange(0, stream.Length), stream); } }
public void Sample01a_HelloWorld_Download() { string originalPath = CreateTempFile(SampleFileContent); string localFilePath = CreateTempPath(); string shareName = Randomize("sample-share"); ShareClient share = new ShareClient(ConnectionString, shareName); try { share.Create(); // Get a reference to a directory named "sample-dir" and then create it ShareDirectoryClient directory = share.GetDirectoryClient("sample-dir"); directory.Create(); // Get a reference to a file named "sample-file" in directory "sample-dir" ShareFileClient file = directory.GetFileClient("sample-file"); // Upload the file using (FileStream stream = File.OpenRead(originalPath)) { file.Create(stream.Length); file.UploadRange( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } Sample01a_HelloWorld.Download(ConnectionString, shareName, localFilePath); // Verify the contents Assert.AreEqual(SampleFileContent, File.ReadAllText(localFilePath)); } finally { share.Delete(); File.Delete(originalPath); File.Delete(localFilePath); } }
public async Task <string> AZFileStorer(IFormFile loadedFile, string fileType) { _logger.LogInformation("File Store Task Called"); string dirName = fileType; string fileName = loadedFile.FileName; ShareFileClient shareFile = StorageSetup(loadedFile, fileType); try { using (var stream = new MemoryStream()) { await loadedFile.CopyToAsync(stream); shareFile.Create(stream.Length); } return("Success"); } catch (Exception ex) { return("Fail: Errormessage : " + ex.Message); } }
public async Task <StoreFileInfo> UploadFile(UploadDataInfo uploadInfo) { // Azure request only lowerCase container name string container = uploadInfo.Container.ToLower(); // Get a reference to a share and then create it ShareClient share = new ShareClient(this.connection, container); share.CreateIfNotExists(); // Get a reference to a directory and create it ShareDirectoryClient directory = share.GetDirectoryClient(uploadInfo.DirectoryName); await directory.CreateIfNotExistsAsync(); // Get a reference to a file and upload it ShareFileClient file = directory.GetFileClient(this.GenerateFileName(uploadInfo.FileName)); Response <ShareFileInfo> uploadFile = file.Create(uploadInfo.Stream.Length); file.UploadRange( new HttpRange(0, uploadInfo.Stream.Length), uploadInfo.Stream); return(new StoreFileInfo(string.Empty, uploadFile.Value.SmbProperties.FileId)); }