public Task UploadTextAsync(string content, Encoding encoding = null, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null, CancellationToken cancellationToken = default(CancellationToken)) { using (CloudBlobStream stream = _store.OpenWriteAppend(_containerName, _blobName, _metadata)) { byte[] buffer = Encoding.UTF8.GetBytes(content); stream.Write(buffer, 0, buffer.Length); stream.Commit(); } return(Task.FromResult(0)); }
public static void UploadEmptyPage(this IStoragePageBlob blob) { if (blob == null) { throw new ArgumentNullException("blob"); } using (CloudBlobStream stream = blob.OpenWriteAsync(512, CancellationToken.None).GetAwaiter().GetResult()) { stream.Commit(); } }
// This shows how to copy devices from one IoT Hub to another. // First, export the list from the Source hut to devices.txt (ExportDevices). // Next, read in that file. Each row is a serialized object; // read them into the generic list serializedDevices. // Delete the devices.txt in blob storage, because we're going to recreate it. // For each serializedDevice, deserialize it, set ImportMode to CREATE, // reserialize it, and write it to a StringBuilder. The ImportMode field is what // tells the job framework to add each device. // Write the new StringBuilder to the block blob. // This essentially replaces the list with a list of devices that have ImportJob = Delete. // Call ImportDevicesAsync, which will read in the list in devices.txt, then add each one // because it doesn't already exist. If it already exists, it will write an entry to // the import error log and not add the new one. private async Task CopyAllDevicesToNewHub(string sourceHubConnectionString, string destHubConnectionString, string containerUri, string deviceListFile) { Console.WriteLine("Exporting devices on current hub"); // Read the devices from the hub and write them to devices.txt in blob storage. await ExportDevices(containerUri, sourceHubConnectionString).ConfigureAwait(false); // Read devices.txt which contains serialized objects. // Write each line to the serializedDevices list. (List<string>). CloudBlockBlob blockBlob = _cloudBlobContainer.GetBlockBlobReference(deviceListFile); // Get the URI for the blob. string blobUri = blockBlob.Uri.ToString(); // Instantiate the generic list. var serializedDevices = new List <string>(); Console.WriteLine("Read in list of devices from blob storage."); // Read the blob file of devices, import each row into serializedDevices. using Stream blobStream = await blockBlob.OpenReadAsync(AccessCondition.GenerateIfExistsCondition(), null, null).ConfigureAwait(false); using var streamReader = new StreamReader(blobStream, Encoding.UTF8); while (streamReader.Peek() != -1) { string line = await streamReader.ReadLineAsync().ConfigureAwait(false); serializedDevices.Add(line); } // Delete the blob containing the list of devices, because we're going to recreate it. CloudBlockBlob blobToDelete = _cloudBlobContainer.GetBlockBlobReference("devices.txt"); Console.WriteLine("Update ImportMode to be Create."); // Step 1: Update each device's ImportMode to Create var sb = new StringBuilder(); serializedDevices.ForEach(serializedDevice => { // Deserialize back to an ExportImportDevice and update the import mode property. ExportImportDevice device = JsonConvert.DeserializeObject <ExportImportDevice>(serializedDevice); device.ImportMode = ImportMode.Create; // Reserialize the object now that we've updated the property. sb.AppendLine(JsonConvert.SerializeObject(device)); }); // Step 2: Delete the blob if it already exists, then write the in-memory list to the blob. await blobToDelete.DeleteIfExistsAsync().ConfigureAwait(false); byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString()); using CloudBlobStream stream = await blobToDelete.OpenWriteAsync().ConfigureAwait(false); for (var i = 0; i < bytes.Length; i += 500) { int length = Math.Min(bytes.Length - i, 500); await stream.WriteAsync(bytes, i, length).ConfigureAwait(false); } stream.Commit(); Console.WriteLine("Creating and running registry manager job to import the entries from the text file to the new hub"); // Step 3: Call import using the same blob to create all devices. // Loads devices.txt and adds the devices to the destination hub. using RegistryManager registryManager = RegistryManager.CreateFromConnectionString(destHubConnectionString); JobProperties importJob = await registryManager.ImportDevicesAsync(containerUri, containerUri).ConfigureAwait(false); // Wait until job is finished while (true) { importJob = await registryManager.GetJobAsync(importJob.JobId).ConfigureAwait(false); Console.WriteLine($"Import job status is {importJob.Status}"); if (importJob.Status == JobStatus.Completed || importJob.Status == JobStatus.Failed || importJob.Status == JobStatus.Cancelled) { // Job has finished executing break; } await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false); } }
/// <summary> /// Generate NumToAdd devices and add them to the hub. /// To do this, generate each identity. /// Include authentication keys. /// Write the device info to a block blob. /// Import the devices into the identity registry by calling the import job. /// </summary> private async Task GenerateAndAddDevices(string hubConnectionString, string containerURI, int NumToAdd, string devicesToAdd) { int interimProgressCount = 0; int displayProgressCount = 1000; int totalProgressCount = 0; // generate reference for list of new devices we're going to add, will write list to this blob CloudBlockBlob generatedListBlob = _cloudBlobContainer.GetBlockBlobReference(devicesToAdd); // define serializedDevices as a generic list<string> List <string> serializedDevices = new List <string>(NumToAdd); for (var i = 1; i <= NumToAdd; i++) { // Create device name with this format: Hub_00000000 + a new guid. // This should be large enough to display the largest number (1 million). string deviceName = $"Hub_{i:D8}_{Guid.NewGuid()}"; Debug.Print($"device = '{deviceName}'\n"); // Create a new ExportImportDevice. // CryptoKeyGenerator is in the Microsoft.Azure.Devices.Common namespace. var deviceToAdd = new ExportImportDevice { Id = deviceName, Status = DeviceStatus.Enabled, Authentication = new AuthenticationMechanism { SymmetricKey = new SymmetricKey { PrimaryKey = CryptoKeyGenerator.GenerateKey(32), SecondaryKey = CryptoKeyGenerator.GenerateKey(32), } }, // This indicates that the entry should be added as a new device. ImportMode = ImportMode.Create, }; // Add device to the list as a serialized object. serializedDevices.Add(JsonConvert.SerializeObject(deviceToAdd)); // Not real progress as you write the new devices, but will at least show *some* progress. interimProgressCount++; totalProgressCount++; if (interimProgressCount >= displayProgressCount) { Console.WriteLine("Added {0} messages.", totalProgressCount); interimProgressCount = 0; } } // Now have a list of devices to be added, each one has been serialized. // Write the list to the blob. var sb = new StringBuilder(); serializedDevices.ForEach(serializedDevice => sb.AppendLine(serializedDevice)); // Before writing the new file, make sure there's not already one there. await generatedListBlob.DeleteIfExistsAsync().ConfigureAwait(false); // Write list of serialized objects to the blob. using CloudBlobStream stream = await generatedListBlob.OpenWriteAsync().ConfigureAwait(false); byte[] bytes = Encoding.UTF8.GetBytes(sb.ToString()); for (var i = 0; i < bytes.Length; i += 500) { int length = Math.Min(bytes.Length - i, 500); await stream.WriteAsync(bytes, i, length).ConfigureAwait(false); } stream.Commit(); Console.WriteLine("Creating and running registry manager job to write the new devices."); // Should now have a file with all the new devices in it as serialized objects in blob storage. // generatedListBlob has the list of devices to be added as serialized objects. // Call import using the blob to add the new devices. // Log information related to the job is written to the same container. // This normally takes 1 minute per 100 devices (according to the docs). // First, initiate an import job. // This reads in the rows from the text file and writes them to IoT Devices. // If you want to add devices from a file, you can create a file and use this to import it. // They have to be in the exact right format. RegistryManager registryManager = RegistryManager.CreateFromConnectionString(hubConnectionString); try { // The first URL is the container to import from; the file must be called devices.txt // The second URL points to the container to write errors to as a block blob. // This lets you import the devices from any file name. Since we wrote the new // devices to [devicesToAdd], need to read the list from there as well. JobProperties importJob = await registryManager .ImportDevicesAsync(containerURI, containerURI, devicesToAdd) .ConfigureAwait(false); // This will catch any errors if something bad happens to interrupt the job. while (true) { importJob = await registryManager.GetJobAsync(importJob.JobId).ConfigureAwait(false); if (importJob.Status == JobStatus.Completed || importJob.Status == JobStatus.Failed || importJob.Status == JobStatus.Cancelled) { // Job has finished executing break; } await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false); } } catch (Exception ex) { Debug.Print("exception message {0}", ex.Message); } }
public override void Commit() { _inner.Commit(); }