public virtual void DownloadFileToBlob(BlobDownloadParameters parameters) { if (parameters == null || parameters.Credentials == null || string.IsNullOrWhiteSpace(parameters.SasUri.ToString())) { throw new ArgumentNullException(Resources.DownloadCredentialsNull); } CloudBlobContainer sascontainer = new CloudBlobContainer(parameters.SasUri); var bloblist = sascontainer.ListBlobs(null, true); string downloadFolderPath = parameters.Directory.Insert(parameters.Directory.Length, @"\"); foreach (var blob in bloblist) { ICloudBlob destBlob = blob as ICloudBlob; int length = destBlob.Name.Split('/').Length; string blobFileName = destBlob.Name.Split('/')[length - 1]; // the folder structure of run logs changed from flat listing to nesting under time directory string blobFolderPath = String.Empty; if (destBlob.Name.Length > blobFileName.Length) { blobFolderPath = destBlob.Name.Substring(0, destBlob.Name.Length - blobFileName.Length - 1); } if (!Directory.Exists(downloadFolderPath + blobFolderPath)) { Directory.CreateDirectory(downloadFolderPath + blobFolderPath); } // adding _log suffix to differentiate between files and folders of the same name. Azure blob storage only knows about blob files. We could use nested folder structure // as part of the blob file name and thus it is possible to have a file and folder of the same name in the same location which is not acceptable for Windows file system destBlob.DownloadToFile(downloadFolderPath + destBlob.Name + "_log", FileMode.Create); } }
public virtual void DownloadFileToBlob(BlobDownloadParameters parameters) { if (parameters == null || string.IsNullOrWhiteSpace(parameters.SasUri.ToString())) { throw new ArgumentNullException(Resources.DownloadCredentialsNull); } var rli = new PSRunLogInfo(parameters.SasUri); StorageCredentials sc = new StorageCredentials(rli.SasToken); StorageUri suri = new StorageUri(new Uri("https://" + parameters.SasUri.Host)); CloudBlobClient sourceClient = new CloudBlobClient(suri, sc); CloudBlobContainer sascontainer = sourceClient.GetContainerReference(rli.Container); CloudBlobDirectory sourceDirectory = sascontainer.GetDirectoryReference(rli.Directory); // TODO: Remove IfDef #if NETSTANDARD var bloblist = sourceDirectory.ListBlobsSegmentedAsync(true, BlobListingDetails.None, null, null, null, null).Result.Results; #else var bloblist = sourceDirectory.ListBlobs(true); #endif var downloadFolderPath = parameters.Directory.Insert(parameters.Directory.Length, @"\"); foreach (var blob in bloblist) { ICloudBlob destBlob = blob as ICloudBlob; int length = destBlob.Name.Split('/').Length; string blobFileName = destBlob.Name.Split('/')[length - 1]; // the folder structure of run logs changed from flat listing to nesting under time directory var blobFolderPath = String.Empty; if (destBlob.Name.Length > blobFileName.Length) { blobFolderPath = destBlob.Name.Substring(0, destBlob.Name.Length - blobFileName.Length - 1); if (!string.IsNullOrEmpty(rli.Directory)) { blobFolderPath = blobFolderPath.Remove(0, rli.Directory.Length - 1).Trim('/'); } } if (!Directory.Exists(downloadFolderPath + blobFolderPath)) { Directory.CreateDirectory(downloadFolderPath + blobFolderPath); } // adding _log suffix to differentiate between files and folders of the same name. Azure blob storage only knows about blob files. We could use nested folder structure // as part of the blob file name and thus it is possible to have a file and folder of the same name in the same location which is not acceptable for Windows file system string fileToDownload = destBlob.Name.Remove(0, rli.Directory.Length); // TODO: Remove IfDef #if NETSTANDARD Task.Run(() => destBlob.DownloadToFileAsync(downloadFolderPath + fileToDownload + "_log", FileMode.Create)).Wait(); #else destBlob.DownloadToFile(downloadFolderPath + fileToDownload + "_log", FileMode.Create); #endif } }
public virtual void DownloadFileToBlob(BlobDownloadParameters parameters) { if (parameters == null || parameters.Credentials == null || string.IsNullOrWhiteSpace(parameters.SasUri.ToString())) { throw new ArgumentNullException(Resources.DownloadCredentialsNull); } CloudBlobContainer sascontainer = new CloudBlobContainer(parameters.SasUri); var bloblist = sascontainer.ListBlobs(null, true); string downloadFolderPath = parameters.Directory.Insert(parameters.Directory.Length, @"\"); foreach (var blob in bloblist) { ICloudBlob destBlob = blob as ICloudBlob; int length = destBlob.Name.Split('/').Length; string blobFileName = destBlob.Name.Split('/')[length-1]; // the folder structure of run logs changed from flat listing to nesting under time directory string blobFolderPath = String.Empty; if (destBlob.Name.Length > blobFileName.Length) { blobFolderPath = destBlob.Name.Substring(0, destBlob.Name.Length - blobFileName.Length - 1); } if (!Directory.Exists(downloadFolderPath + blobFolderPath)) { Directory.CreateDirectory(downloadFolderPath + blobFolderPath); } // adding _log suffix to differentiate between files and folders of the same name. Azure blob storage only knows about blob files. We could use nested folder structure // as part of the blob file name and thus it is possible to have a file and folder of the same name in the same location which is not acceptable for Windows file system destBlob.DownloadToFile(downloadFolderPath + destBlob.Name + "_log", FileMode.Create); } }
public virtual void DownloadFileToBlob(BlobDownloadParameters parameters) { if (parameters == null || string.IsNullOrWhiteSpace(parameters.SasUri.ToString())) { throw new ArgumentNullException(Resources.DownloadCredentialsNull); } PSRunLogInfo rli = new PSRunLogInfo(parameters.SasUri); StorageCredentials sc = new StorageCredentials(rli.SasToken); StorageUri suri = new StorageUri(new Uri("https://" + parameters.SasUri.Host)); CloudBlobClient sourceClient = new CloudBlobClient(suri, sc); CloudBlobContainer sascontainer = sourceClient.GetContainerReference(rli.Container); CloudBlobDirectory sourceDirectory = sascontainer.GetDirectoryReference(rli.Directory); var bloblist = sourceDirectory.ListBlobs(true); string downloadFolderPath = parameters.Directory.Insert(parameters.Directory.Length, @"\"); foreach (var blob in bloblist) { ICloudBlob destBlob = blob as ICloudBlob; int length = destBlob.Name.Split('/').Length; string blobFileName = destBlob.Name.Split('/')[length - 1]; // the folder structure of run logs changed from flat listing to nesting under time directory string blobFolderPath = String.Empty; if (destBlob.Name.Length > blobFileName.Length) { blobFolderPath = destBlob.Name.Substring(0, destBlob.Name.Length - blobFileName.Length - 1); if (!string.IsNullOrEmpty(rli.Directory)) { blobFolderPath = blobFolderPath.Remove(0, rli.Directory.Length - 1).Trim('/'); } } if (!Directory.Exists(downloadFolderPath + blobFolderPath)) { Directory.CreateDirectory(downloadFolderPath + blobFolderPath); } // adding _log suffix to differentiate between files and folders of the same name. Azure blob storage only knows about blob files. We could use nested folder structure // as part of the blob file name and thus it is possible to have a file and folder of the same name in the same location which is not acceptable for Windows file system string fileToDonwload = destBlob.Name.Remove(0, rli.Directory.Length); destBlob.DownloadToFile(downloadFolderPath + fileToDonwload + "_log", FileMode.Create); } }