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)); }
/// <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( new HttpRange(0, stream.Length), stream); } }
private static ShareDirectoryClient GetShareDirectoryClient(string account, string key, string share, string folder) { ShareClient client = new ShareClient(Client.GetConnectionString(account, key), share); return(string.IsNullOrWhiteSpace(folder) ? client.GetRootDirectoryClient() : client.GetDirectoryClient(folder)); }