// Gets a cursor from container or creates a new cursor if it does not exist private static string GetCursor(BlobClient blobClient, BlobChangeFeedClient changeFeedClient, ILogger log) { string continuationToken = null; if (blobClient.Exists()) { // If the continuationToken exists in blob, download and use it var stream = new MemoryStream(); blobClient.DownloadTo(stream); continuationToken = Encoding.UTF8.GetString(stream.ToArray()); } else { // If the continuationToken does not exist in the blob, get the continuationToken from the first item Page <BlobChangeFeedEvent> page = changeFeedClient.GetChanges().AsPages(pageSizeHint: 1).First <Page <BlobChangeFeedEvent> >(); BlobChangeFeedEvent changeFeedEvent = page.Values.First <BlobChangeFeedEvent>(); if (containerCheck.Invoke(changeFeedEvent) && eventTypeCheck.Invoke(changeFeedEvent) && blobCheck.Invoke(changeFeedEvent)) { log.LogInformation($"event: {changeFeedEvent.EventType} at {changeFeedEvent.Subject.Replace("/blobServices/default", "")} on {changeFeedEvent.EventTime}"); } continuationToken = page.ContinuationToken; } return(continuationToken); }
// // Execute GetBlob // public string Execute(string blobURL, string filePath, string identity = null, bool ifNewer = false, bool deleteAfterCopy = false) { // method start // Connection var Cred = new ManagedIdentityCredential(identity); var blobClient = new BlobClient(new Uri(blobURL), Cred); if (ifNewer && File.Exists(filePath) && !IsNewer(blobClient, filePath)) { return("Skipped. Blob is not newer than file."); } try { string absolutePath = Path.GetFullPath(filePath); string dirName = Path.GetDirectoryName(absolutePath); Directory.CreateDirectory(dirName); blobClient.DownloadTo(filePath); if (deleteAfterCopy) { blobClient.Delete(); } return("Success"); } catch (Azure.RequestFailedException) { throw; } catch (Exception ex) { throw AzmiException.IDCheck(identity, ex); } }
public static void DownloadFile(string uriString, string localPath) { Uri uri = new Uri(uriString); BlobClient client = new BlobClient(uri); client.DownloadTo(localPath); }
protected void RecieveFile(BlobClient blob, string fileNameAndPath, FileMode mode) { using (StreamWriter sw = new StreamWriter(File.Open(fileNameAndPath, mode))) { blob.DownloadTo(sw.BaseStream); } }
public void Download() { // Create a temporary Lorem Ipsum file on disk that we can upload string originalPath = CreateTempFile(SampleFileContent); // Get a temporary path on disk where we can download the file string downloadPath = CreateTempPath(); // Get a connection string to our Azure Storage account. string connectionString = ConnectionString; // Get a reference to a container named "sample-container" and then create it BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container")); container.Create(); try { // Get a reference to a blob named "sample-file" BlobClient blob = container.GetBlobClient(Randomize("sample-file")); // First upload something the blob so we have something to download blob.Upload(originalPath); // Download the blob's contents and save it to a file blob.DownloadTo(downloadPath); // Verify the contents Assert.AreEqual(SampleFileContent, File.ReadAllText(downloadPath)); } finally { // Clean up after the test when we're finished container.Delete(); } }
public void DownloadCatalog(bool force = false) { var catalogPath = GetCatalogPath(); var url = ApiCatalogModel.Url; var blobClient = new BlobClient(new Uri(url)); if (!force && File.Exists(catalogPath)) { Console.WriteLine("Checking catalog..."); var localTimetamp = File.GetLastWriteTimeUtc(catalogPath); var properties = blobClient.GetProperties(); var blobTimestamp = properties.Value.LastModified.UtcDateTime; var blobIsNewer = blobTimestamp > localTimetamp; if (!blobIsNewer) { Console.WriteLine("Catalog is up-to-date."); return; } } try { Console.WriteLine("Downloading catalog..."); blobClient.DownloadTo(catalogPath); var properties = blobClient.GetProperties(); File.SetLastWriteTimeUtc(catalogPath, properties.Value.LastModified.UtcDateTime); } catch (Exception ex) { Console.Error.WriteLine($"error: can't download catalog: {ex.Message}"); Environment.Exit(1); } }
public async Task ZipIt([ActivityTrigger] BlobModel input, Binder binder) { SemaphoreSlim semaphore = GetSemaphoreForContainer(input.Analysis.Category); await semaphore.WaitAsync(); try { string name = $"{input.Analysis.Category}.zip"; using Stream zipStream = await binder.BindAsync <Stream>(FunctionUtils.GetBindingAttributes("zip-collection", name)); using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create, true); BlobContainerClient containerClient = GetCloudBlobContainer(input.Analysis.Category); containerClient.DeleteBlobIfExists(name); Pageable <BlobItem> blobs = containerClient.GetBlobs(); foreach (BlobItem blob in blobs) { BlobClient client = containerClient.GetBlobClient(blob.Name); using var blobStream = new MemoryStream(); client.DownloadTo(blobStream); using Stream entryStream = archive.CreateEntry(blob.Name).Open(); blobStream.Seek(0, SeekOrigin.Begin); blobStream.CopyTo(entryStream); } } finally { semaphore.Release(); } }
/// <summary> /// Read the JSON status file to update the history of the given grabbers /// </summary> private static void UpdateGrabberHistory(Grabber[] grabbers, BlobContainerClient container, ILogger log) { log.LogInformation($"reading information from stored grabber file: {GRABBERS_JSON_FILENAME}"); BlobClient blob = container.GetBlobClient(GRABBERS_JSON_FILENAME); if (!blob.Exists()) { return; } using MemoryStream stream = new MemoryStream(); blob.DownloadTo(stream); string json = Encoding.UTF8.GetString(stream.ToArray()); Grabber[] oldGrabbers = GrabberIO.GrabbersFromJson(json); Dictionary <string, Grabber> oldGrabberDictionary = new Dictionary <string, Grabber>(); foreach (Grabber oldGrabber in oldGrabbers) { if (oldGrabberDictionary.ContainsKey(oldGrabber.Info.ID)) { oldGrabberDictionary.Remove(oldGrabber.Info.ID); } oldGrabberDictionary.Add(oldGrabber.Info.ID, oldGrabber); } foreach (Grabber grabber in grabbers.Where(x => oldGrabberDictionary.ContainsKey(x.Info.ID))) { grabber.History.Update(oldGrabberDictionary[grabber.Info.ID].History); } log.LogInformation($"read information about {grabbers.Length} grabbers"); }
//Downloads and decrypts client side encrypted blob, then reuploads blob with server side encryption using an encryption scope with a customer managed key private static void EncryptWithCustomerManagedKey( string connectionString, string containerName, string blobName, string blobNameAfterMigration, string filePath, ClientSideEncryptionOptions clientSideOption, string encryptionScopeName) { //Download and decrypt Client Side Encrypted blob using BlobClient with Client Side Encryption Options string downloadFilePath = filePath + ".download"; BlobClient blobClient = new BlobClient( connectionString, containerName, blobName).WithClientSideEncryptionOptions(clientSideOption); blobClient.DownloadTo(downloadFilePath); //Set Blob Client Options with the created Encryption Scope BlobClientOptions blobClientOptions = new BlobClientOptions() { EncryptionScope = encryptionScopeName }; //Reupload Blob with Server Side Encryption blobClient = new BlobClient( connectionString, containerName, blobNameAfterMigration, blobClientOptions); blobClient.Upload(downloadFilePath, true); }
//Downloads and decrypts client side encrypted blob, then reuploads blob with server side encryption using a customer provided key private static void EncryptWithCustomerProvidedKey( string connectionString, string containerName, string blobName, string blobNameAfterMigration, string filePath, ClientSideEncryptionOptions clientSideOption, byte[] keyBytes) { //Download and decrypt Client Side Encrypted blob using BlobClient with Client Side Encryption Options string downloadFilePath = filePath + "download"; BlobClient blobClient = new BlobClient( connectionString, containerName, blobName).WithClientSideEncryptionOptions(clientSideOption); blobClient.DownloadTo(downloadFilePath); //Set Blob Client Options with the given Customer Provided Key CustomerProvidedKey customerProvidedKey = new CustomerProvidedKey(keyBytes); BlobClientOptions blobClientOptions = new BlobClientOptions() { CustomerProvidedKey = customerProvidedKey, }; //Reupload Blob with Server Side Encryption blobClient = new BlobClient( connectionString, containerName, blobNameAfterMigration, blobClientOptions); blobClient.Upload(downloadFilePath, true); }
static string ReadBlob() { string connString = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING"); BlobServiceClient blobServiceClient = new BlobServiceClient(connString); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); BlobClient blobClient = containerClient.GetBlobClient(blobName); if (blobClient.Exists()) { logger.LogInformation("ReadUpdateBlobStorage; it exists"); string text; using (var memoryStream = new MemoryStream()) { blobClient.DownloadTo(memoryStream); text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); } return(text); } else { // return an empty JSON array if the blob doesn't exist return("[]"); } }
public async Task AuthWithConnectionStringDirectToBlob() { // setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var container = new BlobContainerClient(ConnectionString, containerName); try { await container.CreateAsync(); await container.GetBlobClient(blobName).UploadAsync(new MemoryStream(Encoding.UTF8.GetBytes("hello world"))); string connectionString = this.ConnectionString; #region Snippet:SampleSnippetsBlobMigration_ConnectionStringDirectBlob BlobClient blob = new BlobClient(connectionString, containerName, blobName); #endregion var stream = new MemoryStream(); blob.DownloadTo(stream); Assert.Greater(stream.Length, 0); } finally { await container.DeleteAsync(); } }
public static void Run([BlobTrigger("audio/{name}.wav", Connection = "AudioStorage")] Stream myBlob, string name, ILogger log) { log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); // 環境変数から値を取得 string tempfile = System.Environment.GetEnvironmentVariable("TMP") + "\\" + name; string connectStr = System.Environment.GetEnvironmentVariable("AudioStorage"); // Step 1. Blob から audio ファイルをダウンロード BlobServiceClient blobServiceClient = new BlobServiceClient(connectStr); BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("audio"); BlobClient blobClient = containerClient.GetBlobClient(name + ".wav"); using (FileStream downloadFileStream = File.OpenWrite(tempfile + ".wav")) { blobClient.DownloadTo(downloadFileStream); downloadFileStream.Close(); } // Step 2. audio ファイルから文字起こし Audio2Text(tempfile, log); // Step 3. 抽出したテキストファイルを Blob へ格納 containerClient = blobServiceClient.GetBlobContainerClient("text"); blobClient = containerClient.GetBlobClient(name + ".txt"); using (FileStream uploadFileStream = File.OpenRead(tempfile + ".txt")) { blobClient.Upload(uploadFileStream, true); uploadFileStream.Close(); } // 一時ファイルの削除 File.Delete(tempfile + ".wav"); File.Delete(tempfile + ".txt"); }
public void AuthWithSasCredential() { //setup blob string containerName = Randomize("sample-container"); string blobName = Randomize("sample-file"); var container = new BlobContainerClient(ConnectionString, containerName); try { container.Create(); BlobClient blobClient = container.GetBlobClient(blobName); blobClient.Upload(BinaryData.FromString("hello world")); // build SAS URI for sample Uri sasUri = blobClient.GenerateSasUri(BlobSasPermissions.All, DateTimeOffset.UtcNow.AddHours(1)); #region Snippet:SampleSnippetsBlobMigration_SasUri BlobClient blob = new BlobClient(sasUri); #endregion var stream = new MemoryStream(); blob.DownloadTo(stream); Assert.Greater(stream.Length, 0); } finally { container.Delete(); } }
private void InflateStream(string fileName) { // then we will get it fresh into local deflatedName // StreamOutput deflatedStream = new StreamOutput(CacheDirectory.CreateOutput(deflatedName)); using (var deflatedStream = new MemoryStream()) { // get the deflated blob _blob.DownloadTo(deflatedStream); Debug.WriteLine(string.Format("GET {0} RETREIVED {1} bytes", _name, deflatedStream.Length)); // seek back to begininng deflatedStream.Seek(0, SeekOrigin.Begin); // open output file for uncompressed contents using (var fileStream = _azureDirectory.CreateCachedOutputAsStream(fileName)) using (var decompressor = new DeflateStream(deflatedStream, CompressionMode.Decompress)) { var bytes = new byte[65535]; var nRead = 0; do { nRead = decompressor.Read(bytes, 0, 65535); if (nRead > 0) { fileStream.Write(bytes, 0, nRead); } } while (nRead == 65535); } } }
/// <summary> /// Method that downloads a file from azure blob storage /// </summary> /// <param name="fileName"> /// String with the file name /// </param> /// <param name="downloadPath"> /// String with the download path /// </param> /// <param name="connectionString"> /// String with the connection parameters /// </param> /// <param name="containerName"> /// String with the container name /// </param> private void DownloadFile(string fileName, string downloadPath, string connectionString, string containerName) { BlobContainerClient container = new BlobContainerClient(connectionString, containerName); BlobClient blob = container.GetBlobClient(fileName); blob.DownloadTo(downloadPath); return; }
/* * Uploads a single blob (or updates it in the case where blob already exists in container), checks replication completion, * then demonstrates that source and destination blobs have identical contents */ public static void BlobUpdate(BlobContainerClient sourceContainerClient, BlobContainerClient destContainerClient, String blobName, String blobContent, int timeInterval) { // Uploading blobs Console.WriteLine("Demonstrating replication of blob in source has same contents in destination container"); BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blobName); Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(blobContent)); sourceBlob.Upload(stream, true); Console.WriteLine("Added blob " + blobName + " containing content: " + blobContent); // Check if replication in dest container is finished in interval of 1 min Console.WriteLine("Checking to see if replication finished"); while (true) { Thread.Sleep(timeInterval); Response <BlobProperties> source_response = sourceBlob.GetProperties(); IList <ObjectReplicationPolicy> policyList = source_response.Value.ObjectReplicationSourceProperties; // If policyList is null, then replication still in progress if (policyList != null) { foreach (ObjectReplicationPolicy policy in policyList) { foreach (ObjectReplicationRule rule in policy.Rules) { if (rule.ReplicationStatus.ToString() != "Complete") { Console.WriteLine("Blob replication failed"); return; } // Comparing source and dest blobs Console.WriteLine("Blob successfully replicated"); String sourceContent = ""; String destContent = ""; using (MemoryStream sourceStream = new MemoryStream()) { sourceBlob.DownloadTo(sourceStream); sourceContent = Encoding.UTF8.GetString(sourceStream.ToArray()); } Console.WriteLine("Source Blob Content: " + sourceContent); using (MemoryStream destStream = new MemoryStream()) { BlobClient destBlob = destContainerClient.GetBlobClient(blobName); destBlob.DownloadTo(destStream); destContent = Encoding.UTF8.GetString(destStream.ToArray()); } Console.WriteLine("Destination Blob Content: " + destContent); return; } } } } }
public AzureIndexInput(AzureDirectory azureDirectory, string name, BlobClient blob) : base(name) { this._name = name; this._azureDirectory = azureDirectory; #if FULLDEBUG Debug.WriteLine($"{_azureDirectory.Name} opening {name} "); #endif _fileMutex = BlobMutexManager.GrabMutex(name); _fileMutex.WaitOne(); try { _blobContainer = azureDirectory.BlobContainer; _blob = blob; bool fileNeeded = false; if (!CacheDirectory.FileExists(name)) { fileNeeded = true; } else { long cachedLength = CacheDirectory.FileLength(name); var properties = blob.GetProperties(); long blobLength = properties.Value?.ContentLength ?? 0; if (cachedLength != blobLength) { fileNeeded = true; } } // if the file does not exist // or if it exists and it is older then the lastmodified time in the blobproperties (which always comes from the blob storage) if (fileNeeded) { using (StreamOutput fileStream = _azureDirectory.CreateCachedOutputAsStream(name)) { // get the blob _blob.DownloadTo(fileStream); fileStream.Flush(); Debug.WriteLine($"{_azureDirectory.Name} GET {_name} RETREIVED {fileStream.Length} bytes"); } } #if FULLDEBUG Debug.WriteLine($"{_azureDirectory.Name} Using cached file for {name}"); #endif // and open it as our input, this is now available forevers until new file comes along _indexInput = CacheDirectory.OpenInput(name, IOContext.DEFAULT); } finally { _fileMutex.ReleaseMutex(); } }
private static void DownloadAllBlobFromContainer(BlobContainerClient containerClient) { foreach (BlobItem blobItems in containerClient.GetBlobs()) { Console.WriteLine($"Blob named {blobItems.Name} is found"); BlobClient blobClient = containerClient.GetBlobClient(blobItems.Name); blobClient.DownloadTo(@"E:\" + blobItems.Name); } }
public static Stream DownloadFile(string blobName) { BlobContainerClient container = GetContainer(); BlobClient blob = container.GetBlobClient(blobName); MemoryStream memoryStream = new MemoryStream(); blob.DownloadTo(memoryStream); memoryStream.Position = 0; return(memoryStream); }
public async Task <byte[]> ReadBlobFromContainer(string blobName, string containerName) { BlobContainerClient containerClient = BlobServiceClient.GetBlobContainerClient(containerName); BlobClient blobClient = containerClient.GetBlobClient(blobName); using (var memoryStream = new MemoryStream()) { blobClient.DownloadTo(memoryStream); var length = memoryStream.Length; return(memoryStream.ToArray()); } }
private static BlobClient AccessBlobUsingAppObject() { string appId = "a36d46c9-9335-4137-9e2f-d0079fc15ff5"; string tenantId = "9a0c6406-e26c-4288-a20c-4df2a8eb78a7"; string secretId = "pbg-ZlEbO_.4GxC7.023jthP-oV0U_8o6s"; string blobUrl = "https://storageclitest2021.blob.core.windows.net/mycontainer2021/Course0.json"; BlobClient _blob = new BlobClient(new Uri(blobUrl), new ClientSecretCredential(tenantId, appId, secretId)); _blob.DownloadTo(@"d:\" + _blob.Name); return(_blob); }
protected byte[] RecieveBytes(BlobClient blob, out long size) { size = blob.GetProperties().Value.ContentLength; byte[] buffer = new byte[size]; using (MemoryStream ms = new MemoryStream(buffer)) { blob.DownloadTo(ms); return(ms.ToArray()); } }
public static Stream DownloadFile(string blobName) { BlobContainerClient containerClient = GetContainerClient(); // Get a reference to a blob BlobClient blobClient = containerClient.GetBlobClient(blobName); MemoryStream memoryStream = new MemoryStream(); blobClient.DownloadTo(memoryStream); memoryStream.Position = 0; return(memoryStream); }
private async Task <FollowVM> GetItemsAsync(ApplicationUser user) { string currentUser = _userManager.GetUserId(HttpContext.User); // 2. Connect to Azure Storage account. var connectionString = _configuration.GetConnectionString("AccessKey"); BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); // 3. Use container for users profile photos. string containerName = "profilephotos"; BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); // 4. Create new blob and upload to azure storage account. BlobClient blobClient = containerClient.GetBlobClient("profile-" + user.Id + ".png"); if (!blobClient.Exists()) { blobClient = containerClient.GetBlobClient("profiledefault.png"); } //BlobDownloadInfo download = await blobClient.DownloadAsync(); byte[] result = null; using (var ms = new MemoryStream()) { blobClient.DownloadTo(ms); result = ms.ToArray(); } string base64String = Convert.ToBase64String(result); string image = String.Format("data:image/png;base64,{0}", base64String); FollowVM followVM = new FollowVM(); var userFollow = await(from uf in _context.UserFollows where uf.UserParentId.Equals(currentUser) && uf.UserChildId.Equals(user.Id) select uf).FirstOrDefaultAsync(); followVM.DisplayName = user.DisplayName; followVM.UserName = user.UserName; followVM.ProfileBiography = user.Biography; followVM.AzurePhoto = image; followVM.UserFollow = userFollow; return(followVM); }
private Stream DownloadContentItem(BlobClient blobClient, BlobItem blobItem) { var stream = new MemoryStream(); if (string.IsNullOrEmpty(blobItem.Snapshot)) { blobClient.DownloadTo(stream); } else { blobClient.WithSnapshot(blobItem.Snapshot).DownloadTo(stream); } return(stream); }
private async Task <List <ProfileVM> > GetItemsAsync(string userName) { var usersDropDown = await(from user in _context.ApplicationUsers where user.UserName.Contains(userName) select user).ToListAsync(); List <ProfileVM> profileVMs = new List <ProfileVM>(); foreach (var usersDropDow in usersDropDown) { ProfileVM profileVM = new ProfileVM(); // 2. Connect to Azure Storage account. var connectionString = _configuration.GetConnectionString("AccessKey"); BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString); // 3. Use container for users profile photos. string containerName = "profilephotos"; BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(containerName); // 4. Create new blob and upload to azure storage account. BlobClient blobClient = containerClient.GetBlobClient("profile-" + usersDropDow.Id + ".png"); if (!blobClient.Exists()) { blobClient = containerClient.GetBlobClient("profiledefault.png"); } //BlobDownloadInfo download = await blobClient.DownloadAsync(); byte[] result = null; using (var ms = new MemoryStream()) { blobClient.DownloadTo(ms); result = ms.ToArray(); } string base64String = Convert.ToBase64String(result); string image = String.Format("data:image/png;base64,{0}", base64String); profileVM.AzurePhoto = image; profileVM.UserName = usersDropDow.UserName; profileVM.DisplayName = usersDropDow.DisplayName; profileVMs.Add(profileVM); } return(profileVMs); }
private static void DownloadBlobUsingSasURI(BlobContainerClient containerClient) { string blobName = "Course1.json"; Uri sasURL = GenerateSasURL(containerClient.Name, blobName, containerClient); BlobClient _clientBlob = new BlobClient(sasURL); Azure.Storage.Blobs.Models.BlobProperties props = _clientBlob.GetProperties(); IDictionary <string, string> metadata = props.Metadata; metadata.Add("test", "test"); _clientBlob.SetMetadata(metadata); _clientBlob.DownloadTo(@"E:\sasJSONDemo.json"); }
public async Task <IActionResult> GetImage([FromQuery] string imageName) { BlobServiceClient blobServiceClient = new(_configuration.GetConnectionString("saMyKitchen")); BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("fileuploads"); BlobClient blobClient = blobContainerClient.GetBlobClient(imageName); if (await blobClient.ExistsAsync()) { var memoryStream = new MemoryStream(); blobClient.DownloadTo(memoryStream); return(File(memoryStream.GetBuffer(), "image/jpg")); } return(NotFound($"Image {imageName} not found")); }
private static List <DownloadRecord> LoadRecords(BlobContainerClient container, string packageName) { string fileName = $"logs/{packageName.ToLower()}.json"; BlobClient blob = container.GetBlobClient(fileName); if (!blob.Exists()) { throw new InvalidOperationException($"file not found: {fileName}"); } using MemoryStream stream = new(); blob.DownloadTo(stream); string json = Encoding.UTF8.GetString(stream.ToArray()); return(IO.FromJson(json)); }