public async Task UploadFileAsync(String filename, Stream stream) { ShareFileClient file = this.root.GetFileClient(filename); await file.CreateAsync(stream.Length); await file.UploadAsync(stream); }
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);*/ }
private async Task DumpJson <T>(T obj, string fileName) { // Dump to local temp file (need to do this because need to know file size before copying to Azure File Share). var tmpFile = Path.GetTempFileName(); using (StreamWriter sw = new StreamWriter(tmpFile)) using (JsonWriter jtw = new JsonTextWriter(sw)) { var serializer = new JsonSerializer(); serializer.Serialize(jtw, obj); } try { // Copy the temp file to Azure File Share ShareClient share = new ShareClient(DataDumpStorageConnectionString, DataDumpStorageShareName); ShareDirectoryClient directory = share.GetDirectoryClient("/"); ShareFileClient file = directory.GetFileClient(fileName); using (FileStream stream = File.OpenRead(tmpFile)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync(new HttpRange(0, stream.Length), stream); } } finally { // Clean up the temp file File.Delete(tmpFile); } }
public async Task PathClient_CanGetParentDirectoryClient() { // Arrange var parentDirName = SharesClientBuilder.GetNewDirectoryName(); await using DisposingShare test = await SharesClientBuilder.GetTestShareAsync(); ShareDirectoryClient parentDirClient = InstrumentClient(test.Container .GetRootDirectoryClient() .GetSubdirectoryClient(parentDirName)); await parentDirClient.CreateAsync(); ShareFileClient fileClient = InstrumentClient(parentDirClient .GetFileClient(SharesClientBuilder.GetNewFileName())); await fileClient.CreateAsync(Constants.KB); // Act parentDirClient = fileClient.GetParentShareDirectoryClient(); // make sure that client is functional var dirProperties = await parentDirClient.GetPropertiesAsync(); // Assert Assert.AreEqual(fileClient.Path.GetParentPath(), parentDirClient.Path); Assert.AreEqual(fileClient.AccountName, parentDirClient.AccountName); Assert.IsNotNull(dirProperties); }
public async Task UploadFileAsync(String nombre , Stream stream) { ShareFileClient archivo = this.root.GetFileClient(nombre); await archivo.CreateAsync(stream.Length); await archivo.UploadAsync(stream); }
public async Task UploadFileAsync(String filename, Stream stream) { ShareDirectoryClient raiz = this.cliente.GetRootDirectoryClient(); ShareFileClient file = raiz.GetFileClient(filename); await file.CreateAsync(stream.Length); await file.UploadAsync(stream); }
/// <summary> /// Uploads the asynchronous. /// </summary> /// <param name="storageFile">The storage file.</param> /// <param name="overwrite">if set to <c>true</c> [overwrite].</param> /// <exception cref="System.ArgumentNullException">storageFile</exception> /// <exception cref="System.ArgumentException">storageFile</exception> /// <exception cref="CloudFileSystem.Core.V1.FileManagement.Storage.StorageException"> /// rootDirectoryExists - UploadAsync or fileExists - UploadAsync or Path - UploadAsync /// </exception> public async Task UploadAsync(StorageFile storageFile, bool overwrite = false) { if (storageFile is null) { throw new ArgumentNullException(nameof(storageFile)); } if (string.IsNullOrWhiteSpace(storageFile.SafeName) && string.IsNullOrWhiteSpace(storageFile.FileName)) { throw new ArgumentException(nameof(storageFile)); } ShareDirectoryClient rootDirectory = this._shareClient.GetRootDirectoryClient(); bool rootDirectoryExists = await rootDirectory.ExistsAsync(); if (!rootDirectoryExists) { throw new StorageException(nameof(rootDirectoryExists), nameof(this.UploadAsync)); } string safeName = storageFile.SafeName ?? this.RemoveInvalidCharacters(storageFile.FileName); ShareFileClient file = rootDirectory.GetFileClient(safeName); bool fileExists = await file.ExistsAsync(); if (fileExists && !overwrite) { throw new StorageException(nameof(fileExists), nameof(this.UploadAsync)); } await file.CreateAsync(storageFile.Content.Length); using var reader = new BinaryReader(storageFile.Content); int blockSize = 1 * 1024 * 1024; // 1MB long offset = 0; while (true) { byte[] buffer = reader.ReadBytes(blockSize); if (buffer.Length == 0) { break; } using var uploadChunk = new MemoryStream(); uploadChunk.Write(buffer, 0, buffer.Length); uploadChunk.Position = 0; var httpRange = new HttpRange(offset, buffer.Length); await file.UploadRangeAsync(httpRange, uploadChunk); offset += buffer.Length; } if (string.IsNullOrWhiteSpace(file.Path)) { throw new StorageException(nameof(file.Path), nameof(this.UploadAsync)); } }
public async Task UploadRangeFromUriAsync_SourceBearerToken() { // Arrange BlobServiceClient blobServiceClient = InstrumentClient(new BlobServiceClient( new Uri(TestConfigOAuth.BlobServiceEndpoint), GetOAuthCredential(TestConfigOAuth), GetBlobOptions())); BlobContainerClient containerClient = InstrumentClient(blobServiceClient.GetBlobContainerClient(GetNewShareName())); try { await containerClient.CreateIfNotExistsAsync(); AppendBlobClient appendBlobClient = InstrumentClient(containerClient.GetAppendBlobClient(GetNewFileName())); await appendBlobClient.CreateAsync(); byte[] data = GetRandomBuffer(Constants.KB); using Stream stream = new MemoryStream(data); await appendBlobClient.AppendBlockAsync(stream); ShareServiceClient serviceClient = GetServiceClient_OAuth_SharedKey(); await using DisposingShare test = await GetTestShareAsync( service : serviceClient, shareName : GetNewShareName()); ShareDirectoryClient directoryClient = InstrumentClient(test.Share.GetDirectoryClient(GetNewDirectoryName())); await directoryClient.CreateAsync(); ShareFileClient fileClient = InstrumentClient(directoryClient.GetFileClient(GetNewFileName())); await fileClient.CreateAsync(Constants.KB); string sourceBearerToken = await GetAuthToken(); HttpAuthorization sourceAuthHeader = new HttpAuthorization( "Bearer", sourceBearerToken); ShareFileUploadRangeFromUriOptions options = new ShareFileUploadRangeFromUriOptions { SourceAuthentication = sourceAuthHeader }; HttpRange range = new HttpRange(0, Constants.KB); // Act await fileClient.UploadRangeFromUriAsync( sourceUri : appendBlobClient.Uri, range : range, sourceRange : range, options : options); } finally { await containerClient.DeleteIfExistsAsync(); } }
public async Task UploadRangeFromUriAsync_SourceBearerTokenFail() { // Arrange BlobServiceClient blobServiceClient = InstrumentClient(new BlobServiceClient( new Uri(Tenants.TestConfigOAuth.BlobServiceEndpoint), Tenants.GetOAuthCredential(Tenants.TestConfigOAuth), GetBlobOptions())); BlobContainerClient containerClient = InstrumentClient(blobServiceClient.GetBlobContainerClient(GetNewShareName())); try { await containerClient.CreateIfNotExistsAsync(); AppendBlobClient appendBlobClient = InstrumentClient(containerClient.GetAppendBlobClient(GetNewFileName())); await appendBlobClient.CreateAsync(); byte[] data = GetRandomBuffer(Constants.KB); using Stream stream = new MemoryStream(data); await appendBlobClient.AppendBlockAsync(stream); ShareServiceClient serviceClient = SharesClientBuilder.GetServiceClient_OAuthAccount_SharedKey(); await using DisposingShare test = await GetTestShareAsync( service : serviceClient, shareName : GetNewShareName()); ShareDirectoryClient directoryClient = InstrumentClient(test.Share.GetDirectoryClient(GetNewDirectoryName())); await directoryClient.CreateAsync(); ShareFileClient fileClient = InstrumentClient(directoryClient.GetFileClient(GetNewFileName())); await fileClient.CreateAsync(Constants.KB); HttpAuthorization sourceAuthHeader = new HttpAuthorization( "Bearer", "auth token"); ShareFileUploadRangeFromUriOptions options = new ShareFileUploadRangeFromUriOptions { SourceAuthentication = sourceAuthHeader }; HttpRange range = new HttpRange(0, Constants.KB); // Act await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>( fileClient.UploadRangeFromUriAsync( sourceUri: appendBlobClient.Uri, range: range, sourceRange: range, options: options), e => Assert.AreEqual("CannotVerifyCopySource", e.ErrorCode)); } finally { await containerClient.DeleteIfExistsAsync(); } }
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)); } }
public async Task Sample01b_HelloWorldAsync_TraverseAsync() { // 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); await share.CreateAsync(); try { // Create a bunch of directories ShareDirectoryClient first = await share.CreateDirectoryAsync("first"); await first.CreateSubdirectoryAsync("a"); await first.CreateSubdirectoryAsync("b"); ShareDirectoryClient second = await share.CreateDirectoryAsync("second"); await second.CreateSubdirectoryAsync("c"); await second.CreateSubdirectoryAsync("d"); await share.CreateDirectoryAsync("third"); ShareDirectoryClient fourth = await share.CreateDirectoryAsync("fourth"); ShareDirectoryClient deepest = await fourth.CreateSubdirectoryAsync("e"); // Upload a file named "file" ShareFileClient file = deepest.GetFileClient("file"); using (FileStream stream = File.OpenRead(originalPath)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } await Sample01b_HelloWorldAsync.TraverseAsync(ConnectionString, shareName); } finally { await share.DeleteAsync(); } }
public override async Task SetupAsync() { await base.SetupAsync(); // See https://docs.microsoft.com/en-us/rest/api/storageservices/naming-and-referencing-shares--directories--files--and-metadata for // restrictions on file share naming. _shareClient = new ShareClient(PerfTestEnvironment.Instance.FileSharesConnectionString, Guid.NewGuid().ToString()); await _shareClient.CreateIfNotExistsAsync(); ShareDirectoryClient DirectoryClient = _shareClient.GetDirectoryClient(Path.GetRandomFileName()); await DirectoryClient.CreateIfNotExistsAsync(); _fileClient = DirectoryClient.GetFileClient(Path.GetRandomFileName()); await _fileClient.CreateAsync(_stream.Length, cancellationToken : CancellationToken.None); }
// </snippet_GetFileSasUri> // <snippet_UploadFileWithSas> //------------------------------------------------- // Upload a file using a SAS //------------------------------------------------- public async Task UploadFileWithSasAsync() { Uri fileSasUri = GetFileSasUri("logs", "CustomLogs/Log1.txt", DateTime.UtcNow.AddHours(24), ShareFileSasPermissions.Read | ShareFileSasPermissions.Write ); // Get a reference to a local file and upload it using (FileStream stream = File.OpenRead("Log1.txt")) { ShareFileClient file = new ShareFileClient(fileSasUri); await file.CreateAsync(stream.Length); await file.UploadAsync(stream); Console.WriteLine($"File uploaded: {file.Uri}"); } }
private async Task UploadFile(ShareFileClient fileClient, string fileName) { try { var assembly = Assembly.GetExecutingAssembly(); var resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(fileName)); using Stream stream = assembly.GetManifestResourceStream(resourceName); using StreamReader reader = new StreamReader(stream); await fileClient.CreateAsync(stream.Length); await fileClient.UploadAsync(stream); } catch (Exception ex) { _logger.LogError("The upload of the file to the fileshare failed.", ex); } }
public async Task Sample01b_HelloWorldAsync_Download() { string originalPath = CreateTempFile(SampleFileContent); string localFilePath = CreateTempPath(); string shareName = Randomize("sample-share"); ShareClient share = new ShareClient(ConnectionString, shareName); try { await share.CreateAsync(); ShareDirectoryClient directory = share.GetDirectoryClient("sample-dir"); await directory.CreateAsync(); ShareFileClient file = directory.GetFileClient("sample-file"); // Upload the file using (FileStream stream = File.OpenRead(originalPath)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } await Sample01b_HelloWorldAsync.DownloadAsync(ConnectionString, shareName, localFilePath); // Verify the contents Assert.AreEqual(SampleFileContent, File.ReadAllText(localFilePath)); } finally { await share.DeleteAsync(); File.Delete(originalPath); File.Delete(localFilePath); } }
/// <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 async Task UploadAsync(string connectionString, string shareName, string localFilePath) { // 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. // Name of the directory and file we'll create string dirName = "sample-dir"; string fileName = "sample-file"; // Get a reference to a share and then create it ShareClient share = new ShareClient(connectionString, shareName); await share.CreateAsync(); // Get a reference to a directory and create it ShareDirectoryClient directory = share.GetDirectoryClient(dirName); await directory.CreateAsync(); // Get a reference to a file and upload it ShareFileClient file = directory.GetFileClient(fileName); using (FileStream stream = File.OpenRead(localFilePath)) { await file.CreateAsync(stream.Length); await file.UploadRangeAsync( ShareFileRangeWriteType.Update, new HttpRange(0, stream.Length), stream); } }
/// <summary>Uploads a file</summary> /// <param name="fileStream">The file stream to upload.</param> /// <param name="directoryName">The directory of interest or empty/null if you want the root directory</param> /// <param name="fileName">The file name without a path</param> public async Task UploadAsync(Stream fileStream, string directoryName, string fileName) { // Get a reference to a directory and create it necessary ShareDirectoryClient directory; if (string.IsNullOrWhiteSpace(directoryName)) { directory = _share.GetRootDirectoryClient(); } else { directory = _share.GetDirectoryClient(directoryName); if (await directory.ExistsAsync() == false) { await directory.CreateAsync(); } } ShareFileClient file = directory.GetFileClient(fileName); await file.CreateAsync(fileStream.Length); await file.UploadRangeAsync(new HttpRange(0, fileStream.Length), fileStream); }
public static async Task <DisposingFile> CreateAsync(DisposingDirectory test, ShareFileClient file) { await file.CreateAsync(maxSize : Constants.MB); return(new DisposingFile(test, file)); }
/// <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. ShareClient shareClient = null; ShareFileClient shareFileClient = null; ShareDirectoryClient fileDirectory = null; BlobClient targetBlob = null; BlobContainerClient blobContainer = null; string destFile = null; string downloadFolder = null; // Name to be used for the file when downloading it so you can inspect it locally string downloadFile = null; try { //***** Setup *****// Console.WriteLine("Getting reference to the storage account."); // How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx string storageConnectionString = ConfigurationManager.AppSettings.Get("StorageConnectionString"); string storageAccountName = ConfigurationManager.AppSettings.Get("StorageAccountName"); string storageAccountKey = ConfigurationManager.AppSettings.Get("StorageAccountKey"); Console.WriteLine("Instantiating file client."); // Create a share client for interacting with the file service. var shareServiceClient = new ShareServiceClient(storageConnectionString); // 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); shareClient = shareServiceClient.GetShareClient(shareName); try { await shareClient.CreateIfNotExistsAsync(); Console.WriteLine(" Share created successfully."); } catch (RequestFailedException exRequest) { Common.WriteException(exRequest); 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); ShareDirectoryClient rootDirectory = shareClient.GetRootDirectoryClient(); // 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.GetSubdirectoryClient(sourceFolder); await fileDirectory.CreateIfNotExistsAsync(); Console.WriteLine(" Directory created successfully."); } //***** Upload a file to the file share *****// // Get a file client. shareFileClient = fileDirectory.GetFileClient(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)) { using (FileStream stream = File.OpenRead(sourceFile)) { // Upload from the local file to the file share in azure. await shareFileClient.CreateAsync(stream.Length); await shareFileClient.UploadAsync(stream); } 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."); var fileList = rootDirectory.GetFilesAndDirectoriesAsync(); // Print all files/directories listed above. await foreach (ShareFileItem listItem in fileList) { // listItem type will be ShareClient or ShareDirectoryClient. Console.WriteLine(" - {0} (type: {1})", listItem.Name, 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.GetFilesAndDirectoriesAsync(); // Print all files/directories in the folder. await foreach (ShareFileItem listItem in fileList) { // listItem type will be a file or directory Console.WriteLine(" - {0} (IsDirectory: {1})", listItem.Name, listItem.IsDirectory); } //***** 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. ShareFileDownloadInfo download = await shareFileClient.DownloadAsync(); downloadFile = Path.Combine(downloadFolder, testFile); using (FileStream stream = File.OpenWrite(downloadFile)) { await download.Content.CopyToAsync(stream); } 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 GetFileClient. // Then it will use the new file for the copy and abort but the rest of the code // will still use the original file. ShareFileClient shareFileCopy = fileDirectory.GetFileClient(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); using (FileStream stream = File.OpenRead(sourceFile)) { // Upload from the local file to the file share in azure. await shareFileCopy.CreateAsync(stream.Length); await shareFileCopy.UploadAsync(stream); } 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 blob service client. var blobServiceClient = new BlobServiceClient(storageConnectionString); // Get a blob container client and create it if it doesn't already exist. blobContainer = blobServiceClient.GetBlobContainerClient(shareName); await blobContainer.CreateIfNotExistsAsync(); // Get a blob client to the target blob. targetBlob = blobContainer.GetBlobClient(testFile); string copyId = string.Empty; // Get a share file client to be copied. shareFileClient = fileDirectory.GetFileClient(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. var sas = new AccountSasBuilder { // Allow access to Files Services = AccountSasServices.Files, // Allow access to the service level APIs ResourceTypes = AccountSasResourceTypes.All, // Access expires in 1 day! ExpiresOn = DateTime.UtcNow.AddDays(1) }; sas.SetPermissions(AccountSasPermissions.Read); var credential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey); // Build a SAS URI var sasUri = new UriBuilder(shareFileClient.Uri) { Query = sas.ToSasQueryParameters(credential).ToString() }; // Start the copy of the file to the blob. CopyFromUriOperation operation = await targetBlob.StartCopyFromUriAsync(sasUri.Uri); copyId = operation.Id; Console.WriteLine(" File copy started successfully. copyID = {0}", copyId); // 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. shareFileClient = fileDirectory.GetFileClient(testFile); await shareFileClient.DeleteIfExistsAsync(); 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"; shareFileClient = fileDirectory.GetFileClient(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'); 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 (!shareFileClient.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 shareFileClient.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."); var range = new HttpRange(startOffset, textToStream.Length); await shareFileClient.UploadRangeAsync(range, ms); // 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."); download = await shareFileClient.DownloadAsync(); using (FileStream stream = File.OpenWrite(downloadFile)) { await download.Content.CopyToAsync(stream); } 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."); var range = new HttpRange(startOffset, textToStream.Length); await shareFileClient.UploadRangeAsync(range, ms); 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."); download = await shareFileClient.DownloadAsync(); using (FileStream stream = File.OpenWrite(downloadFile)) { await download.Content.CopyToAsync(stream); } Console.WriteLine(" Successfully downloaded file to examine."); } // Query and view the list of ranges. Console.WriteLine("Call to get the list of ranges."); var listOfRanges = await shareFileClient.GetRangeListAsync(new HttpRange()); Console.WriteLine(" Successfully retrieved list of ranges."); foreach (HttpRange range in listOfRanges.Value.Ranges) { Console.WriteLine(" --> filerange startOffset = {0}, endOffset = {1}", range.Offset, range.Offset + range.Length); } //***** 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"; shareFileClient = fileDirectory.GetFileClient(destFile); await shareFileClient.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 shareClient.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 blobContainer.DeleteIfExistsAsync(); Console.WriteLine(" Successfully deleted the blob and its container."); } }
public override void ExecuteCmdlet() { if (AsJob.IsPresent) { DoBeginProcessing(); } string filePath = this.Source; FileInfo localFile = new FileInfo(filePath); if (!localFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, Resources.SourceFileNotFound, this.Source)); } long fileSize = localFile.Length; // if FIPS policy is enabled, must use native MD5 for DMlib. if (fipsEnabled) { if (fileSize < sizeTB) { CloudStorageAccount.UseV1MD5 = false; } else // use Track2 SDK { WriteWarning("The uploaded file won't have Content MD5 hash, since caculate MD5 hash fail, most possiblly caused by FIPS is enabled on this machine."); } } bool isDirectory; string[] path = NamingUtil.ValidatePath(this.Path, out isDirectory); var cloudFileToBeUploaded = BuildCloudFileInstanceFromPathAsync(localFile.Name, path, isDirectory).ConfigureAwait(false).GetAwaiter().GetResult(); if (ShouldProcess(cloudFileToBeUploaded.Name, "Set file content")) { // Step 2: Build the CloudFile object which pointed to the // destination cloud file. this.RunTask(async taskId => { var progressRecord = new ProgressRecord( this.OutputStream.GetProgressId(taskId), string.Format(CultureInfo.CurrentCulture, Resources.SendAzureFileActivity, localFile.Name, cloudFileToBeUploaded.GetFullPath(), cloudFileToBeUploaded.Share.Name), Resources.PrepareUploadingFile); if (fileSize <= sizeTB) { await DataMovementTransferHelper.DoTransfer(() => this.TransferManager.UploadAsync( localFile.FullName, cloudFileToBeUploaded, new UploadOptions { PreserveSMBAttributes = context is null ? false : context.PreserveSMBAttribute.IsPresent }, this.GetTransferContext(progressRecord, localFile.Length), this.CmdletCancellationToken), progressRecord, this.OutputStream).ConfigureAwait(false); } else // use Track2 SDK { //Create File ShareFileClient fileClient = AzureStorageFile.GetTrack2FileClient(cloudFileToBeUploaded, Channel.StorageContext); // confirm overwrite if file exist if (!this.Force.IsPresent && fileClient.Exists(this.CmdletCancellationToken) && !await this.OutputStream.ConfirmAsync(string.Format(CultureInfo.CurrentCulture, Resources.OverwriteConfirmation, Util.ConvertToString(cloudFileToBeUploaded)))) { return; } await fileClient.CreateAsync(fileSize, cancellationToken: this.CmdletCancellationToken).ConfigureAwait(false); //Prepare progress Handler IProgress <long> progressHandler = new Progress <long>((finishedBytes) => { if (progressRecord != null) { // Size of the source file might be 0, when it is, directly treat the progress as 100 percent. progressRecord.PercentComplete = 0 == fileSize ? 100 : (int)(finishedBytes * 100 / fileSize); progressRecord.StatusDescription = string.Format(CultureInfo.CurrentCulture, Resources.FileTransmitStatus, progressRecord.PercentComplete); this.OutputStream.WriteProgress(progressRecord); } }); long blockSize = 4 * 1024 * 1024; int maxWorkers = 4; List <Task> runningTasks = new List <Task>(); IncrementalHash hash = null; if (!fipsEnabled) { hash = IncrementalHash.CreateHash(HashAlgorithmName.MD5); } using (FileStream stream = File.OpenRead(localFile.FullName)) { byte[] buffer = null; long lastBlockSize = 0; for (long offset = 0; offset < fileSize; offset += blockSize) { long currentBlockSize = offset + blockSize < fileSize ? blockSize : fileSize - offset; // Only need to create new buffer when chunk size change if (currentBlockSize != lastBlockSize) { buffer = new byte[currentBlockSize]; lastBlockSize = currentBlockSize; } await stream.ReadAsync(buffer: buffer, offset: 0, count: (int)currentBlockSize); if (!fipsEnabled && hash != null) { hash.AppendData(buffer); } Task task = UploadFileRangAsync(fileClient, new HttpRange(offset, currentBlockSize), new MemoryStream(buffer), progressHandler); runningTasks.Add(task); // Check if any of upload range tasks are still busy if (runningTasks.Count >= maxWorkers) { await Task.WhenAny(runningTasks).ConfigureAwait(false); // Clear any completed blocks from the task list for (int i = 0; i < runningTasks.Count; i++) { Task runningTask = runningTasks[i]; if (!runningTask.IsCompleted) { continue; } await runningTask.ConfigureAwait(false); runningTasks.RemoveAt(i); i--; } } } // Wait for all upload range tasks finished await Task.WhenAll(runningTasks).ConfigureAwait(false); } // Need set file properties if ((!fipsEnabled && hash != null) || (context != null && context.PreserveSMBAttribute.IsPresent)) { ShareFileHttpHeaders header = null; if (!fipsEnabled && hash != null) { header = new ShareFileHttpHeaders(); header.ContentHash = hash.GetHashAndReset(); } FileSmbProperties smbProperties = null; if (context != null && context.PreserveSMBAttribute.IsPresent) { FileInfo sourceFileInfo = new FileInfo(localFile.FullName); smbProperties = new FileSmbProperties(); smbProperties.FileCreatedOn = sourceFileInfo.CreationTimeUtc; smbProperties.FileLastWrittenOn = sourceFileInfo.LastWriteTimeUtc; smbProperties.FileAttributes = Util.LocalAttributesToAzureFileNtfsAttributes(File.GetAttributes(localFile.FullName)); } // set file header and attributes to the file fileClient.SetHttpHeaders(httpHeaders: header, smbProperties: smbProperties); } if (this.PassThru) { // fetch latest file properties for output cloudFileToBeUploaded.FetchAttributes(); } } if (this.PassThru) { WriteCloudFileObject(taskId, this.Channel, cloudFileToBeUploaded); } });
/// <summary> /// Manage file metadata /// </summary> /// <param name="shareServiceClient"></param> /// <returns></returns> private static async Task FileMetadataSample(ShareServiceClient shareServiceClient) { Console.WriteLine(); // Create the share name -- use a guid in the name so it's unique. string shareName = "demotest-" + Guid.NewGuid(); ShareClient shareClient = shareServiceClient.GetShareClient(shareName); try { // Create share Console.WriteLine("Create Share"); await shareClient.CreateIfNotExistsAsync(); // Create directory Console.WriteLine("Create directory"); ShareDirectoryClient rootDirectory = shareClient.GetRootDirectoryClient(); ShareFileClient file = rootDirectory.GetFileClient("demofile"); // Create file Console.WriteLine("Create file"); await file.CreateAsync(1000); // Set file metadata Console.WriteLine("Set file metadata"); var metadata = new Dictionary <string, string> { { "key1", "value1" }, { "key2", "value2" } }; await file.SetMetadataAsync(metadata); // Fetch file attributes ShareFileProperties properties = await file.GetPropertiesAsync(); Console.WriteLine("Get file metadata:"); foreach (var keyValue in properties.Metadata) { Console.WriteLine(" {0}: {1}", keyValue.Key, keyValue.Value); } Console.WriteLine(); } catch (RequestFailedException exRequest) { Common.WriteException(exRequest); 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"); await shareClient.DeleteIfExistsAsync(); } }
/// <summary> /// Manage file properties /// </summary> /// <param name="shareServiceClient"></param> /// <returns></returns> private static async Task FilePropertiesSample(ShareServiceClient shareServiceClient) { Console.WriteLine(); // Create the share name -- use a guid in the name so it's unique. string shareName = "demotest-" + Guid.NewGuid(); ShareClient shareClient = shareServiceClient.GetShareClient(shareName); try { // Create share Console.WriteLine("Create Share"); await shareClient.CreateIfNotExistsAsync(); // Create directory Console.WriteLine("Create directory"); ShareDirectoryClient rootDirectory = shareClient.GetRootDirectoryClient(); ShareFileClient file = rootDirectory.GetFileClient("demofile"); // Create file Console.WriteLine("Create file"); await file.CreateAsync(1000); // Set file properties var headers = new ShareFileHttpHeaders { ContentType = "plain/text", ContentEncoding = new string[] { "UTF-8" }, ContentLanguage = new string[] { "en" } }; await file.SetHttpHeadersAsync(httpHeaders : headers); // Fetch file attributes ShareFileProperties shareFileProperties = await file.GetPropertiesAsync(); Console.WriteLine("Get file properties:"); Console.WriteLine(" Content type: {0}", shareFileProperties.ContentType); Console.WriteLine(" Content encoding: {0}", string.Join("", shareFileProperties.ContentEncoding)); Console.WriteLine(" Content language: {0}", string.Join("", shareFileProperties.ContentLanguage)); Console.WriteLine(" Length: {0}", shareFileProperties.ContentLength); } catch (RequestFailedException exRequest) { Common.WriteException(exRequest); 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"); await shareClient.DeleteIfExistsAsync(); } Console.WriteLine(); }