/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpWebResponse response) { CommonUtility.AssertNotNull("response", response); // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = HttpResponseParsers.GetETag(response); #if WINDOWS_PHONE containerProperties.LastModified = HttpResponseParsers.GetLastModified(response); #else containerProperties.LastModified = response.LastModified.ToUniversalTime(); #endif // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); // Reading public access containerProperties.PublicAccess = GetAcl(response); // WORM policies string hasImmutability = response.Headers[Constants.HeaderConstants.HasImmutabilityPolicyHeader]; containerProperties.HasImmutabilityPolicy = string.IsNullOrEmpty(hasImmutability) ? (bool?)null : bool.Parse(hasImmutability); string hasLegalHold = response.Headers[Constants.HeaderConstants.HasLegalHoldHeader]; containerProperties.HasLegalHold = string.IsNullOrEmpty(hasLegalHold) ? (bool?)null : bool.Parse(hasLegalHold); return(containerProperties); }
/// <summary> /// Parse the return values of container operation /// </summary> internal void ParseContainerCollection(Collection <PSObject> Values) { _Output.Clear(); foreach (PSObject result in Values) { Dictionary <string, object> dic = new Dictionary <string, object>(); foreach (PSMemberInfo member in result.Members) { if (member.Value != null) { // skip the PSMethod members if (member.Value.GetType() != typeof(PSMethod)) { dic.Add(member.Name, member.Value); } } else { dic.Add(member.Name, member.Value); } if (member.Name.Equals("Properties")) { BlobContainerProperties properties = (BlobContainerProperties)member.Value; dic.Add("LastModified", properties.LastModified); } } _Output.Add(dic); } //clean pipeline when finished CleanPipeline(); }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpResponseMessage response) { CommonUtility.AssertNotNull("response", response); // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = (response.Headers.ETag == null) ? null : response.Headers.ETag.ToString(); containerProperties.LastModified = response?.Content?.Headers?.LastModified; // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); // Reading public access containerProperties.PublicAccess = GetAcl(response); // WORM policies string hasImmutability = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasImmutabilityPolicyHeader); containerProperties.HasImmutabilityPolicy = string.IsNullOrEmpty(hasImmutability) ? (bool?)null : bool.Parse(hasImmutability); string hasLegalHold = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasLegalHoldHeader); containerProperties.HasLegalHold = string.IsNullOrEmpty(hasLegalHold) ? (bool?)null : bool.Parse(hasLegalHold); return(containerProperties); }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpResponseMessage response) { // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = (response.Headers.ETag == null) ? null : response.Headers.ETag.ToString(); if (response.Content != null) { containerProperties.LastModified = response.Content.Headers.LastModified; } else { containerProperties.LastModified = null; } // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); // Reading public access containerProperties.PublicAccess = GetAcl(response); return(containerProperties); }
private static async Task ReadInfos(string connString) { BlobServiceClient storageAccount; try { storageAccount = new BlobServiceClient(connString); AccountInfo accountInfo = await storageAccount.GetAccountInfoAsync(); Console.WriteLine("AccountKind: " + accountInfo.AccountKind); Console.WriteLine("AccountSku: " + accountInfo.SkuName); if (accountInfo.SkuName == SkuName.StandardRagrs) { BlobServiceStatistics stats = await storageAccount.GetStatisticsAsync(); Console.WriteLine("Statistics (GeoReplication): " + stats.GeoReplication); } var containers = storageAccount.GetBlobContainers(); foreach (var container in containers) { Console.WriteLine("checking Container: " + container.Name); if (container.IsDeleted ?? true) { BlobContainerClient containerClient = new BlobContainerClient(connString, container.Name); BlobContainerProperties containerProperties = await containerClient.GetPropertiesAsync(); //TODO: Generate Demo SASPolicy GenerateSASPolicy(containerClient); Console.WriteLine("Container has LegalHold? " + containerProperties.HasLegalHold); Console.WriteLine("Container is Immutable? " + containerProperties.HasImmutabilityPolicy); await foreach (BlobItem blob in containerClient.GetBlobsAsync()) { Console.WriteLine("Blob: " + blob.Name); Console.WriteLine(" Type Of:" + blob.Properties.BlobType); Console.WriteLine(blob.Properties.ContentLength > 1024 ? " Size (KB):" + (blob.Properties.ContentLength / 1024) : " Size (byte):" + blob.Properties.ContentLength); } } else { Console.WriteLine(" >>>> Is Deleted!" + container.Properties.RemainingRetentionDays); } } } catch (Exception e) { Console.WriteLine("OH SNAP!: " + e.Message); } }
public async Task <BlobContainerPropertiesAndBlobList> GetContainerPropertiesAndFullDetails(string containerName, CancellationToken cancellationToken = default) { BlobContainerClient containerClient = BlobStorageManager.GetBlobContainerClient(_azureStorageOptions, containerName); BlobContainerProperties blobContainerProperties = await containerClient.GetPropertiesAsync(); IEnumerable <BlobItem> blobContainerContent = containerClient.GetBlobs(); return(new BlobContainerPropertiesAndBlobList { BlobContainerProperties = blobContainerProperties, BlobItems = blobContainerContent.ToArray(), }); }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpWebResponse response) { // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = HttpResponseParsers.GetETag(response); containerProperties.LastModified = response.LastModified.ToUniversalTime(); // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); return containerProperties; }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpWebResponse response) { // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = HttpResponseParsers.GetETag(response); containerProperties.LastModified = response.LastModified.ToUniversalTime(); // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); return(containerProperties); }
private BlobContainerProperties GetBlobContainerProperties(DateTime?lastModified) { var properties = new BlobContainerProperties(); if (lastModified != null) { var lastModifiedProperty = typeof(BlobContainerProperties).GetProperty( "LastModified", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); lastModifiedProperty.SetValue(properties, new DateTimeOffset(lastModified.Value)); } return(properties); }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpResponseMessage response) { // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = (response.Headers.ETag == null) ? null : response.Headers.ETag.ToString(); if (response.Content != null) { containerProperties.LastModified = response.Content.Headers.LastModified; } else { containerProperties.LastModified = null; } // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); // Reading public access containerProperties.PublicAccess = GetAcl(response); // WORM policy string hasImmutability = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasImmutabilityPolicyHeader); containerProperties.HasImmutabilityPolicy = string.IsNullOrEmpty(hasImmutability) ? (bool?)null : bool.Parse(hasImmutability); string hasLegalHold = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.HasLegalHoldHeader); containerProperties.HasLegalHold = string.IsNullOrEmpty(hasLegalHold) ? (bool?)null : bool.Parse(hasLegalHold); string defaultEncryptionScope = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.DefaultEncryptionScopeHeader); if (!string.IsNullOrEmpty(defaultEncryptionScope)) { containerProperties.EncryptionScopeOptions = new BlobContainerEncryptionScopeOptions(); containerProperties.EncryptionScopeOptions.DefaultEncryptionScope = defaultEncryptionScope; string preventEncryptionScopeOverride = response.Headers.GetHeaderSingleValueOrDefault(Constants.HeaderConstants.PreventEncryptionScopeOverrideHeader); containerProperties.EncryptionScopeOptions.PreventEncryptionScopeOverride = string.Equals(preventEncryptionScopeOverride, Constants.HeaderConstants.TrueHeader, StringComparison.OrdinalIgnoreCase); } return(containerProperties); }
public async Task InitializeAsync( CancellationToken ct) { try { var blobServiceClient = new BlobServiceClient(connectionString); blobContainer = blobServiceClient.GetBlobContainerClient(containerName); blobContainerProperties = await blobContainer.GetPropertiesAsync(cancellationToken : ct); await blobContainer.CreateIfNotExistsAsync(cancellationToken : ct); } catch (Exception ex) { throw new AssetStoreException($"Cannot connect to blob container '{containerName}'.", ex); } }
public async Task <bool> AppendContainerMetaData(string containerName, IDictionary <string, string> metaData, CancellationToken cancellationToken = default) { BlobContainerClient containerClient = BlobStorageManager.GetBlobContainerClient(_azureStorageOptions, containerName); BlobContainerProperties blobContainerProperties = await containerClient.GetPropertiesAsync(); IDictionary <string, string> existingMetaData = blobContainerProperties.Metadata; foreach (KeyValuePair <string, string> metaItem in metaData) { existingMetaData.Add(metaItem); } Response <BlobContainerInfo> result = await containerClient.SetMetadataAsync(metadata : metaData, cancellationToken : cancellationToken); return(result.GetRawResponse().Status == (int)HttpStatusCode.OK); }
public async Task <Response <BlobDownloadInfo> > GetBlob(string blobName) { try { BlobContainerProperties properties = container.GetProperties(); foreach (var metadate in properties.Metadata) { Console.WriteLine(string.Format($"{metadate.Key}: {metadate.Value}")); } var blob = await new BlobClient(new Uri("https://sep6.blob.core.windows.net/airplane-project/" + blobName)).DownloadAsync(); return(blob); } catch (Exception e) { return(null); } }
public static Blob ToBlob(string name, BlobContainerProperties properties) { var blob = new Blob(name, BlobItemKind.Folder); blob.LastModificationTime = properties.LastModified; blob.TryAddProperties( "IsContainer", true, "ETag", properties.ETag, "HasImmutabilityPolicy", properties.HasImmutabilityPolicy, "HasLegalHold", properties.HasLegalHold, "LastModified", properties.LastModified, "LeaseDuration", properties.LeaseDuration, "LeaseState", properties.LeaseState, "LeaseStatus", properties.LeaseStatus, "PublicAccess", properties.PublicAccess); blob.Metadata.MergeRange(properties.Metadata); return(blob); }
// Get container properties. // Return true on success, false if not found, throw exception on error. public bool GetContainerProperties(string containerName, out BlobContainerProperties properties) { properties = null; try { CloudBlobContainer container = BlobClient.GetContainerReference(containerName); container.FetchAttributes(); properties = container.Properties; return(true); } catch (StorageClientException ex) { if ((int)ex.StatusCode == 404) { return(false); } throw; } }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpWebResponse response) { CommonUtility.AssertNotNull("response", response); // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = HttpResponseParsers.GetETag(response); #if WINDOWS_PHONE containerProperties.LastModified = HttpResponseParsers.GetLastModified(response); #else containerProperties.LastModified = response.LastModified.ToUniversalTime(); #endif // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); return containerProperties; }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpWebResponse response) { CommonUtility.AssertNotNull("response", response); // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = HttpResponseParsers.GetETag(response); #if WINDOWS_PHONE containerProperties.LastModified = HttpResponseParsers.GetLastModified(response); #else containerProperties.LastModified = response.LastModified.ToUniversalTime(); #endif // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); return(containerProperties); }
private static async Task DisplayBlobMetadata(BlobContainerClient blobContainerClient) { Console.WriteLine("Container Properties"); Console.WriteLine("------------------------------------------------------------"); Response <BlobContainerProperties> response = await blobContainerClient.GetPropertiesAsync(); BlobContainerProperties containerProperties = response.Value; Console.WriteLine($"Container name : {blobContainerClient.Name}"); Console.WriteLine($" Last modified : {containerProperties.LastModified}"); Console.WriteLine(" Container Metadata"); foreach (var key in containerProperties.Metadata.Keys) { var value = containerProperties.Metadata[key]; Console.WriteLine($" Key: {key} Value: {value}"); } Console.WriteLine("Blob Properties"); Console.WriteLine("------------------------------------------------------------"); AsyncPageable <BlobItem> blobs = blobContainerClient.GetBlobsAsync(BlobTraits.Metadata); await foreach (var blobItem in blobs) { // Print out some useful blob properties Console.WriteLine($"Blob name: {blobItem.Name}"); Console.WriteLine($" Created on : {blobItem.Properties.CreatedOn}"); Console.WriteLine($" Last modified: {blobItem.Properties.LastModified}"); // Enumerate the metadata Console.WriteLine(" Blob Metadata"); foreach (var key in blobItem.Metadata.Keys) { var value = blobItem.Metadata[key]; Console.WriteLine($" Key: {key} Value: {value}"); } } }
/// <summary> /// Gets the container's properties from the response. /// </summary> /// <param name="response">The web response.</param> /// <returns>The container's attributes.</returns> public static BlobContainerProperties GetProperties(HttpResponseMessage response) { // Set the container properties BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.ETag = (response.Headers.ETag == null) ? null : response.Headers.ETag.ToString(); if (response.Content != null) { containerProperties.LastModified = response.Content.Headers.LastModified; } else { containerProperties.LastModified = null; } // Get lease properties containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(response); containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(response); containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(response); return containerProperties; }
public static Blob ToBlob(CloudBlobContainer container) { var blob = new Blob(container.Name, BlobItemKind.Folder); blob.Properties["IsContainer"] = "True"; if (container.Properties != null) { BlobContainerProperties props = container.Properties; if (props.ETag != null) { blob.Properties["ETag"] = props.ETag; } if (props.HasImmutabilityPolicy != null) { blob.Properties["HasImmutabilityPolicy"] = props.HasImmutabilityPolicy.ToString(); } if (props.HasLegalHold != null) { blob.Properties["HasLegalHold"] = props.HasLegalHold.ToString(); } blob.Properties["LeaseDuration"] = props.LeaseDuration.ToString(); blob.Properties["LeaseState"] = props.LeaseState.ToString(); blob.Properties["LeaseStatus"] = props.LeaseStatus.ToString(); if (props.PublicAccess != null) { blob.Properties["PublicAccess"] = props.PublicAccess.ToString(); } } if (container.Metadata?.Count > 0) { blob.Metadata.MergeRange(container.Metadata); } return(blob); }
/// <summary> /// Reads a container entry completely including its properties and metadata. /// </summary> /// <returns>Container listing entry</returns> private BlobContainerEntry ParseContainerEntry() { Uri uri = null; string name = null; IDictionary <string, string> metadata = null; BlobContainerProperties containerProperties = new BlobContainerProperties(); this.reader.ReadStartElement(); while (this.reader.IsStartElement()) { if (this.reader.IsEmptyElement) { this.reader.Skip(); } else { switch (this.reader.Name) { case Constants.UrlElement: string url = this.reader.ReadElementContentAsString(); Uri.TryCreate(url, UriKind.Absolute, out uri); break; case Constants.NameElement: name = this.reader.ReadElementContentAsString(); break; case Constants.PropertiesElement: this.reader.ReadStartElement(); while (this.reader.IsStartElement()) { if (this.reader.IsEmptyElement) { this.reader.Skip(); } else { switch (this.reader.Name) { case Constants.LastModifiedElement: containerProperties.LastModified = reader.ReadElementContentAsString().ToUTCTime(); break; case Constants.EtagElement: containerProperties.ETag = reader.ReadElementContentAsString(); break; case Constants.LeaseStatusElement: containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString()); break; case Constants.LeaseStateElement: containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString()); break; case Constants.LeaseDurationElement: containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString()); break; } } } this.reader.ReadEndElement(); break; case Constants.MetadataElement: metadata = Response.ParseMetadata(this.reader); break; default: reader.Skip(); break; } } } this.reader.ReadEndElement(); if (metadata == null) { metadata = new Dictionary <string, string>(); } return(new BlobContainerEntry { Properties = containerProperties, Name = name, Uri = uri, Metadata = metadata, }); }
internal static FileSystemProperties ToFileSystemProperties(this BlobContainerProperties containerProperties) =>
// Perform blob storage operations. static void TestBlobStorage() { string storageConnectionString = ConfigurationManager.ConnectionStrings["Storage"].ConnectionString; BlobHelper BlobHelper = new BlobHelper(storageConnectionString); try { Separator(); List <CloudBlobContainer> containers; Console.Write("List containers "); if (BlobHelper.ListContainers(out containers)) { Console.WriteLine("true"); if (containers != null) { foreach (CloudBlobContainer container in containers) { Console.Write(container.Name + " "); } Console.WriteLine(); } } else { Console.WriteLine("false"); } Separator(); Console.Write("Create container "); if (BlobHelper.CreateContainer("samplecontainer1")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Delete container "); if (BlobHelper.DeleteContainer("samplecontainer0")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Set container ACL "); if (BlobHelper.SetContainerACL("samplecontainer1", "container")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string accessLevel; Console.Write("Get container ACL "); if (BlobHelper.GetContainerACL("samplecontainer1", out accessLevel)) { Console.WriteLine("true " + accessLevel); } else { Console.WriteLine("false"); } Separator(); SortedList <string, SharedAccessPolicy> policies = new SortedList <string, SharedAccessPolicy>(); SharedAccessPolicy policy1 = new SharedAccessPolicy() { Permissions = SharedAccessPermissions.List | SharedAccessPermissions.Read | SharedAccessPermissions.Write | SharedAccessPermissions.Delete, SharedAccessStartTime = DateTime.UtcNow, SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1) }; policies.Add("Policy1", policy1); policies.Add("Policy2", new SharedAccessPolicy() { Permissions = SharedAccessPermissions.Read, SharedAccessStartTime = DateTime.Parse("2010-01-01T09:38:05Z"), SharedAccessExpiryTime = DateTime.Parse("2012-12-31T09:38:05Z") }); Console.Write("Set container access policy "); if (BlobHelper.SetContainerAccessPolicy("samplecontainer1", policies)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get container access policy "); if (BlobHelper.GetContainerAccessPolicy("samplecontainer1", out policies)) { Console.WriteLine("true"); if (policies != null) { foreach (KeyValuePair <string, SharedAccessPolicy> policy in policies) { Console.WriteLine("Policy " + policy.Key); } } } else { Console.WriteLine("false"); } Separator(); string signature = String.Empty; Console.Write("Create shared access signature "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", policy1, out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); signature = String.Empty; Console.Write("Create shared access signature from access policy "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", "Policy1", out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); signature = String.Empty; Console.Write("Create shared access signature from access policy 2 "); if (BlobHelper.GenerateSharedAccessSignature("samplecontainer1", "Policy2", out signature)) { Console.WriteLine("true " + signature); } else { Console.WriteLine("false"); } Separator(); BlobContainerProperties containerProperties = new BlobContainerProperties(); Console.Write("Get container properties "); if (BlobHelper.GetContainerProperties("samplecontainer1", out containerProperties)) { Console.WriteLine("true"); Console.WriteLine("BlobContainerProperties.Etag: " + containerProperties.ETag); Console.WriteLine("BlobContainerProperties.LastModifiedUtc: " + containerProperties.LastModifiedUtc.ToShortDateString()); } else { Console.WriteLine("false"); } Separator(); NameValueCollection metadata = new NameValueCollection(); metadata.Add("property1", "Value1"); metadata.Add("property2", "Value2"); metadata.Add("property3", "Value3"); Console.Write("Set container metadata "); if (BlobHelper.SetContainerMetadata("samplecontainer1", metadata)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); metadata = new NameValueCollection(); Console.Write("Get container metadata "); if (BlobHelper.GetContainerMetadata("samplecontainer1", out metadata)) { Console.WriteLine("true"); if (metadata != null) { for (int i = 0; i < metadata.Count; i++) { Console.WriteLine(metadata.GetKey(i) + ": " + metadata.Get(i)); } } } else { Console.WriteLine("false"); } Separator(); //Console.Write("Delete container container1 "); //if (BlobHelper.DeleteContainer("samplecontainer1")) //{ // Console.WriteLine("true"); //} //else //{ // Console.WriteLine("false"); //} Separator(); List <CloudBlob> blobs; Console.Write("List blobs "); if (BlobHelper.ListBlobs("samplecontainer1", out blobs)) { Console.WriteLine("true"); if (blobs != null) { foreach (CloudBlob blob in blobs) { Console.WriteLine(blob.Uri.LocalPath); } } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob "); if (BlobHelper.PutBlob("samplecontainer1", "blob1.txt", "This is a text blob!")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put page blob "); if (BlobHelper.PutBlob("samplecontainer1", "pageblob1.txt", 2048)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string page0 = new string('A', 512); Console.Write("Put page 0 "); if (BlobHelper.PutPage("samplecontainer1", "pageblob1.txt", page0, 0, 512)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string page1 = new string('B', 512); Console.Write("Put page 1 "); if (BlobHelper.PutPage("samplecontainer1", "pageblob1.txt", page1, 512, 512)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); page0 = String.Empty; Console.Write("Get page 0 "); if (BlobHelper.GetPage("samplecontainer1", "pageblob1.txt", 0, 512, out page0)) { Console.WriteLine("true"); Console.WriteLine(page0); } else { Console.WriteLine("false"); } Separator(); page1 = String.Empty; Console.Write("Get page 1 "); if (BlobHelper.GetPage("samplecontainer1", "pageblob1.txt", 512, 512, out page1)) { Console.WriteLine("true"); Console.WriteLine(page1); } else { Console.WriteLine("false"); } Separator(); PageRange[] ranges; Console.Write("Get page regions "); if (BlobHelper.GetPageRegions("samplecontainer1", "pageblob1.txt", out ranges)) { Console.WriteLine("true"); if (ranges != null) { foreach (PageRange range in ranges) { Console.WriteLine(range.StartOffset.ToString() + "-" + range.EndOffset.ToString()); } } } else { Console.WriteLine("false"); } Separator(); string content; Console.Write("Get blob "); if (BlobHelper.GetBlob("samplecontainer1", "blob1.txt", out content)) { Console.WriteLine("true"); Console.WriteLine(content); } else { Console.WriteLine("false"); } Separator(); Console.Write("Copy blob "); if (BlobHelper.CopyBlob("samplecontainer1", "blob1.txt", "samplecontainer1", "blob2.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Snapshot blob "); if (BlobHelper.SnapshotBlob("samplecontainer1", "blob1.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Delete blob "); if (BlobHelper.DeleteBlob("samplecontainer1", "blob2.txt")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); SortedList <string, string> properties = new SortedList <string, string>(); properties.Add("ContentType", "text/html"); Console.Write("Set blob properties "); if (BlobHelper.SetBlobProperties("samplecontainer1", "blob1.txt", properties)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get blob properties "); if (BlobHelper.GetBlobProperties("samplecontainer1", "blob1.txt", out properties)) { Console.WriteLine("true"); foreach (KeyValuePair <string, string> item in properties) { Console.WriteLine(item.Key + ": " + item.Value); } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob If Unchanged 1 "); if (BlobHelper.PutBlobIfUnchanged("samplecontainer1", "blob1.txt", "This is a text blob!", properties["ETag"])) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob If Unchanged 2 "); if (BlobHelper.PutBlobIfUnchanged("samplecontainer1", "blob1.txt", "This is a text blob!", "BadETag")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put blob with MD5 "); if (BlobHelper.PutBlobWithMD5("samplecontainer1", "blob1.txt", "This is a text blob!")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); metadata.Clear(); metadata.Add("property1", "Value1"); metadata.Add("property2", "Value2"); metadata.Add("property3", "Value3"); metadata.Add("property4", "Value4"); Console.Write("Set blob metadata "); if (BlobHelper.SetBlobMetadata("samplecontainer1", "blob1.txt", metadata)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Get blob metadata "); if (BlobHelper.GetBlobMetadata("samplecontainer1", "blob1.txt", out metadata)) { Console.WriteLine("true"); for (int i = 0; i < metadata.Count; i++) { Console.WriteLine(metadata.GetKey(i) + ": " + metadata.Get(i)); } } else { Console.WriteLine("false"); } Separator(); string leaseAction = "acquire"; string leaseId = null; Console.Write("Lease blob - acquire "); if (BlobHelper.LeaseBlob("samplecontainer1", "blob1.txt", leaseAction, ref leaseId)) { Console.WriteLine("true " + leaseId); } else { Console.WriteLine("false"); } Separator(); leaseAction = "release"; Console.Write("Lease blob - release "); if (BlobHelper.LeaseBlob("samplecontainer1", "blob1.txt", leaseAction, ref leaseId)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string[] blockIds = new string[3]; Console.Write("Put block 0 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 0, blockIds, "AAAAAAAAAA")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block 1 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 1, blockIds, "BBBBBBBBBB")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block 2 "); if (BlobHelper.PutBlock("samplecontainer1", "largeblob1.txt", 2, blockIds, "CCCCCCCCCC")) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } Separator(); string[] getblockids = null; Console.Write("Get block list "); if (BlobHelper.GetBlockList("samplecontainer1", "largeblob1.txt", out getblockids)) { Console.WriteLine("true"); if (getblockids != null) { for (int i = 0; i < getblockids.Length; i++) { Console.WriteLine("Block " + i + " id = " + getblockids[i]); } } } else { Console.WriteLine("false"); } Separator(); Console.Write("Put block list "); if (BlobHelper.PutBlockList("samplecontainer1", "largeblob1.txt", blockIds)) { Console.WriteLine("true"); } else { Console.WriteLine("false"); } } catch (Exception ex) { Console.WriteLine("EXCEPTION " + ex.ToString()); } }
/// <summary> /// Reads a container entry completely including its properties and metadata. /// </summary> /// <returns>Container listing entry</returns> private static async Task <BlobContainerEntry> ParseContainerEntryAsync(XmlReader reader, Uri baseUri, CancellationToken token) { token.ThrowIfCancellationRequested(); string name = null; IDictionary <string, string> metadata = null; BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.PublicAccess = BlobContainerPublicAccessType.Off; await reader.ReadStartElementAsync().ConfigureAwait(false); while (await reader.IsStartElementAsync().ConfigureAwait(false)) { token.ThrowIfCancellationRequested(); if (reader.IsEmptyElement) { await reader.SkipAsync().ConfigureAwait(false); } else { switch (reader.Name) { case Constants.NameElement: name = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false); break; case Constants.PropertiesElement: await reader.ReadStartElementAsync().ConfigureAwait(false); while (await reader.IsStartElementAsync().ConfigureAwait(false)) { token.ThrowIfCancellationRequested(); if (reader.IsEmptyElement) { await reader.SkipAsync().ConfigureAwait(false); } else { switch (reader.Name) { case Constants.LastModifiedElement: containerProperties.LastModified = (await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)).ToUTCTime(); break; case Constants.EtagElement: containerProperties.ETag = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false); break; case Constants.HasImmutabilityPolicyElement: containerProperties.HasImmutabilityPolicy = await reader.ReadElementContentAsBooleanAsync().ConfigureAwait(false); break; case Constants.HasLegalHoldElement: containerProperties.HasLegalHold = await reader.ReadElementContentAsBooleanAsync().ConfigureAwait(false); break; case Constants.LeaseStatusElement: containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)); break; case Constants.LeaseStateElement: containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)); break; case Constants.LeaseDurationElement: containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)); break; case Constants.PublicAccessElement: containerProperties.PublicAccess = ContainerHttpResponseParsers.GetContainerAcl(await reader.ReadElementContentAsStringAsync().ConfigureAwait(false)); break; default: await reader.SkipAsync().ConfigureAwait(false); break; } } } await reader.ReadEndElementAsync().ConfigureAwait(false); break; case Constants.MetadataElement: metadata = await Response.ParseMetadataAsync(reader).ConfigureAwait(false); break; default: await reader.SkipAsync().ConfigureAwait(false); break; } } } await reader.ReadEndElementAsync().ConfigureAwait(false); if (metadata == null) { metadata = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); } return(new BlobContainerEntry { Properties = containerProperties, Name = name, Uri = NavigationHelper.AppendPathToSingleUri(baseUri, name), Metadata = metadata, }); }
/// <summary> /// Reads a container entry completely including its properties and metadata. /// </summary> /// <returns>Container listing entry</returns> private BlobContainerEntry ParseContainerEntry(Uri baseUri) { string name = null; IDictionary <string, string> metadata = null; BlobContainerProperties containerProperties = new BlobContainerProperties(); containerProperties.PublicAccess = BlobContainerPublicAccessType.Off; this.reader.ReadStartElement(); while (this.reader.IsStartElement()) { if (this.reader.IsEmptyElement) { this.reader.Skip(); } else { switch (this.reader.Name) { case Constants.NameElement: name = this.reader.ReadElementContentAsString(); break; case Constants.PropertiesElement: this.reader.ReadStartElement(); while (this.reader.IsStartElement()) { if (this.reader.IsEmptyElement) { this.reader.Skip(); } else { switch (this.reader.Name) { case Constants.LastModifiedElement: containerProperties.LastModified = reader.ReadElementContentAsString().ToUTCTime(); break; case Constants.EtagElement: containerProperties.ETag = reader.ReadElementContentAsString(); break; case Constants.HasImmutabilityPolicyElement: containerProperties.HasImmutabilityPolicy = reader.ReadElementContentAsBoolean(); break; case Constants.HasLegalHoldElement: containerProperties.HasLegalHold = reader.ReadElementContentAsBoolean(); break; case Constants.LeaseStatusElement: containerProperties.LeaseStatus = BlobHttpResponseParsers.GetLeaseStatus(reader.ReadElementContentAsString()); break; case Constants.LeaseStateElement: containerProperties.LeaseState = BlobHttpResponseParsers.GetLeaseState(reader.ReadElementContentAsString()); break; case Constants.LeaseDurationElement: containerProperties.LeaseDuration = BlobHttpResponseParsers.GetLeaseDuration(reader.ReadElementContentAsString()); break; case Constants.PublicAccessElement: containerProperties.PublicAccess = ContainerHttpResponseParsers.GetContainerAcl(reader.ReadElementContentAsString()); break; default: reader.Skip(); break; } } } this.reader.ReadEndElement(); break; case Constants.MetadataElement: metadata = Response.ParseMetadata(this.reader); break; default: reader.Skip(); break; } } } this.reader.ReadEndElement(); if (metadata == null) { metadata = new Dictionary <string, string>(); } return(new BlobContainerEntry { Properties = containerProperties, Name = name, Uri = NavigationHelper.AppendPathToSingleUri(baseUri, name), Metadata = metadata, }); }