public async Task EditBlobWithMetadata() { string data = "hello world"; var initialMetadata = new Dictionary <string, string> { { "fizz", "buzz" } }; string containerName = Randomize("sample-container"); var containerClient = new BlobContainerClient(ConnectionString, containerName); try { containerClient.Create(); BlobClient blobClient = containerClient.GetBlobClient(Randomize("sample-blob")); await blobClient.UploadAsync(BinaryData.FromString(data), new BlobUploadOptions { Metadata = initialMetadata }); #region Snippet:SampleSnippetsBlobMigration_EditBlobWithMetadata // download blob content and metadata BlobDownloadResult blobData = blobClient.DownloadContent(); // modify blob content string modifiedBlobContent = blobData.Content + "FizzBuzz"; // reupload modified blob content while preserving metadata // not adding metadata is a metadata clear blobClient.Upload( BinaryData.FromString(modifiedBlobContent), new BlobUploadOptions() { Metadata = blobData.Details.Metadata }); #endregion var actualMetadata = (await blobClient.GetPropertiesAsync()).Value.Metadata; Assert.AreEqual(initialMetadata.Count, actualMetadata.Count); foreach (var expectedKvp in initialMetadata) { Assert.IsTrue(actualMetadata.TryGetValue(expectedKvp.Key, out var actualValue)); Assert.AreEqual(expectedKvp.Value, actualValue); } } finally { await containerClient.DeleteIfExistsAsync(); } }
public string ReadHash(string Hash, string Collection) { string sResult = ""; try { Collection = Collection.ToLower(); Collection = RemoveInvalidChars(Collection); Hash = RemoveInvalidChars(Hash); string sColl = Collection; blobClient = container.GetBlobClient(sColl + "/" + Hash + ".json"); sResult = blobClient.DownloadContent().Value.Content.ToString(); } catch { } return(sResult); }
public async IAsyncEnumerable <JObject> GetRawAssetsAsync(string paths) { foreach (var bAsset in container.GetBlobs(Azure.Storage.Blobs.Models.BlobTraits.None, Azure.Storage.Blobs.Models.BlobStates.None, "_assets")) { blobClient = container.GetBlobClient(bAsset.Name); JObject jObj = new JObject(); if (paths.Contains("*") || paths.Contains("..")) { try { string jRes = blobClient.DownloadContent().Value.Content.ToString(); jObj = new JObject(jRes); jObj = await jDB.GetFullAsync(jObj["#id"].Value <string>(), jObj["_index"].Value <int>()); } catch { } } else { var oAsset = await jDB.ReadHashAsync(bAsset.Name.Split('/')[1], "_assets"); if (!string.IsNullOrEmpty(paths)) { jObj = await jDB.GetRawAsync(oAsset, paths); //load only the path } else { jObj = JObject.Parse(oAsset); //if not paths, we only return the raw data } } if (jObj["_hash"] == null) { jObj.Add(new JProperty("_hash", bAsset.Name.Split('/')[1])); } yield return(jObj); } }
/// <summary> /// Tests a blob SAS to determine which operations it allows. /// </summary> /// <param name="sasUri">A string containing a URI with a SAS appended.</param> /// <param name="blobContent">A string content content to write to the blob.</param> static void TestBlobSAS(Uri sasUri, string blobContent) { //Try performing blob operations using the SAS provided. //Return a reference to the blob using the SAS URI. BlobClient blob = new BlobClient(sasUri); //Create operation: Upload a blob with the specified name to the container. //If the blob does not exist, it will be created. If it does exist, it will be overwritten. try { //string blobContent = "This blob was created with a shared access signature granting write permissions to the blob. "; blob.Upload(BinaryData.FromString(blobContent)); Console.WriteLine("Create operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Create operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } // Write operation: Add metadata to the blob try { IDictionary <string, string> metadata = new Dictionary <string, string>(); metadata.Add("name", "value"); blob.SetMetadata(metadata); Console.WriteLine("Write operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Write operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Read operation: Read the contents of the blob. try { BlobDownloadResult download = blob.DownloadContent(); string content = download.Content.ToString(); Console.WriteLine(content); Console.WriteLine(); Console.WriteLine("Read operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Read operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } //Delete operation: Delete the blob. try { blob.Delete(); Console.WriteLine("Delete operation succeeded for SAS " + sasUri); Console.WriteLine(); } catch (RequestFailedException e) { Console.WriteLine("Delete operation failed for SAS " + sasUri); Console.WriteLine("Additional error information: " + e.Message); Console.WriteLine(); } }