public void UploadToAppendBlob()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));

            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            CloudBlobContainer container = blobClient.GetContainerReference("my-append-blobs");

            container.CreateIfNotExists();

            CloudAppendBlob appendBlob = container.GetAppendBlobReference("append-blob.log");

            if (!appendBlob.Exists())
            {
                appendBlob.CreateOrReplace();
            }

            int numBlocks = 10;

            Random rnd = new Random();

            byte[] bytes = new byte[numBlocks];
            rnd.NextBytes(bytes);

            //Simulate a logging operation by writing text data and byte data to the end of the append blob.
            for (int i = 0; i < numBlocks; i++)
            {
                appendBlob.AppendText(string.Format("Timestamp: {0:u} \tLog Entry: {1}{2}", DateTime.UtcNow, bytes[i], Environment.NewLine));
            }

            Console.WriteLine(appendBlob.DownloadText());
        }
Example #2
0
        public static string GetLogs()
        {
            string connectionString = @"DefaultEndpointsProtocol=https;AccountName=hotelmanagerblob;AccountKey=0rIspWPXwm7U22uvlHZshW6mHeJKLw1W5Tu3OQbi8jUasSRVju61eU0teHvNHwtzuGW68vmtRH9uE/rlhoqAnQ==;EndpointSuffix=core.windows.net";
                                      
            string file = "appendBlop.txt";
            string logs = "No Log or file not found";

            CloudStorageAccount account = CloudStorageAccount.Parse(connectionString);

            if (account == null) return logs;

            CloudBlobClient client = account.CreateCloudBlobClient();

            if (client == null) return logs;

            CloudBlobContainer container = client.GetContainerReference("hotelmanagerlog");

            if (!container.Exists()) container.Create();

            CloudAppendBlob blob = container.GetAppendBlobReference(file);

            if (!blob.Exists()) return logs;

            logs = blob.DownloadText();

            return logs;
        }
Example #3
0
        public void AddStringToFile(string containerName, string fileName, string content)
        {
            var container = OpenOrInitializeContainer(containerName);

            CloudAppendBlob appendBlob = container.GetAppendBlobReference(fileName);

            //Create the append blob. Note that if the blob already exists, the CreateOrReplace() method will overwrite it.
            //You can check whether the blob exists to avoid overwriting it by using CloudAppendBlob.Exists().
            appendBlob.CreateOrReplace();

            appendBlob.AppendText($"Timestamp: {DateTime.UtcNow} \tLog Entry: {content}");

            appendBlob.DownloadText();
        }
Example #4
0
        private static async Task AppendBlobProcessAsync()
        {
            CloudStorageAccount storageAccount = null;

            CloudBlobContainer cloudBlobContainer = null;

            string sourceFile = null;

            string destinationFile = null;


            // Retrieve the connection string for use with the application. The storage connection string is stored

            // in an environment variable on the machine running the application called storageconnectionstring.

            // If the environment variable is created after the application is launched in a console or with Visual

            // Studio, the shell needs to be closed and reloaded to take the environment variable into account.

            string storageConnectionString = ConfigurationManager.AppSettings["StorageConnectionString"]; //Environment.GetEnvironmentVariable("StorageConnectionString");


            //storageAccount = CloudStorageAccount.DevelopmentStorageAccount;

            // Check whether the connection string can be parsed.

            if (CloudStorageAccount.TryParse(storageConnectionString, out storageAccount))
            {
                try

                {
                    // Create the CloudBlobClient that represents the Blob storage endpoint for the storage account.

                    ////CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

                    //127.0.0.1:10000/devstoreaccount1/testcont

                    // Create a container called 'quickstartblobs' and append a GUID value to it to make the name unique.

                    ////cloudBlobContainer = cloudBlobClient.GetContainerReference("testcontappendblob");// "quickstartblobs" + Guid.NewGuid().ToString());

                    ////await cloudBlobContainer.CreateIfNotExistsAsync();

                    ////Console.WriteLine("Created container '{0}'", cloudBlobContainer.Name);

                    ////Console.WriteLine();



                    ////// Set the permissions so the blobs are public.

                    ////BlobContainerPermissions permissions = new BlobContainerPermissions

                    ////{

                    ////    PublicAccess = BlobContainerPublicAccessType.Blob

                    ////};

                    ////await cloudBlobContainer.SetPermissionsAsync(permissions);

                    // Get a reference to the blob address, then upload the file to the blob.

                    // Use the value of localFileName for the blob name.


                    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
                    cloudBlobContainer = cloudBlobClient.GetContainerReference("testcont");
                    cloudBlobContainer.CreateIfNotExists();

                    CloudAppendBlob cloudAppendBlob = cloudBlobContainer.GetAppendBlobReference("1.txt");
                    cloudAppendBlob.CreateOrReplace();
                    cloudAppendBlob.AppendText("Content added");
                    cloudAppendBlob.AppendText("More content added");
                    cloudAppendBlob.AppendText("Even more content added");

                    string appendBlobContent = cloudAppendBlob.DownloadText();


                    //cloudAppendBlob.UploadFromFile(sourceFile);



                    // List the blobs in the container.

                    Console.WriteLine("Listing blobs in container.");

                    BlobContinuationToken blobContinuationToken = null;

                    do

                    {
                        var results = await cloudBlobContainer.ListBlobsSegmentedAsync(null, blobContinuationToken);

                        // Get the value of the continuation token returned by the listing call.

                        blobContinuationToken = results.ContinuationToken;

                        foreach (IListBlobItem item in results.Results)

                        {
                            Console.WriteLine(item.Uri);
                        }
                    } while (blobContinuationToken != null); // Loop while the continuation token is not null.

                    destinationFile = sourceFile.Replace(".txt", "_DOWNLOADED.txt");

                    Console.WriteLine("Downloading blob to {0}", destinationFile);

                    Console.WriteLine();

                    await cloudAppendBlob.DownloadToFileAsync(destinationFile, FileMode.Create);


                    File.WriteAllText(sourceFile, "Hello, World, I am good, how is your health!");

                    await cloudAppendBlob.UploadFromFileAsync(sourceFile);

                    await cloudAppendBlob.DeleteAsync();
                }

                catch (StorageException ex)

                {
                    Console.WriteLine("Error returned from the service: {0}", ex.Message);
                }

                finally

                {
                    // Console.WriteLine("Press any key to delete the sample files and example container.");

                    Console.ReadLine();

                    // Clean up resources. This includes the container and the two temp files.

                    //Console.WriteLine("Deleting the container and any blobs it contains");

                    if (cloudBlobContainer != null)
                    {
                        // await cloudBlobContainer.DeleteIfExistsAsync();
                    }
                    File.Delete(sourceFile);
                    File.Delete(destinationFile);
                }
            }

            else

            {
                Console.WriteLine(

                    "A connection string has not been defined in the system environment variables. " +

                    "Add a environment variable named 'storageconnectionstring' with your storage " +

                    "connection string as a value.");
            }
        }
Example #5
0
 /// <summary>
 /// This method is used mainly by unit test
 /// The source code uses it is for easier to be tested through unittest
 /// It is used to get the content of a cloud append blob
 /// </summary>
 /// <param name="blob"></param>
 /// <returns></returns>
 public string GetContent(CloudAppendBlob blob)
 {
     return(blob.DownloadText());
 }
Example #6
0
        static void AzureStorageBlobExamples()
        {
            // Getting configuration from the App.config file and get access to the CloudStorageAccount.
            // Azure access Key should is stored in the App.config
            String storageKeyValue = CloudConfigurationManager.GetSetting("AzureStorageAccount");

            // Just in case to check whether reading the correct value
            Console.WriteLine("Storage Account Key Used" + storageKeyValue);

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageKeyValue);

            // Getting the Azure Client
            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();

            // Getting reference for the container
            CloudBlobContainer cloudContainer = cloudBlobClient.GetContainerReference("images");

            // Create the container (folder) if does not exists
            cloudContainer.CreateIfNotExists();

            // Get the reference for the Blob in Azure
            CloudBlockBlob blobCloud = cloudContainer.GetBlockBlobReference("photo.png");


            // Learning 1: Copy a file from local path in to Azure Blob storage

            //Open a local file and upload to the Azure
            using (var fileStream = System.IO.File.OpenRead(@"C:\Users\Administrator\Pictures\photo.jpg"))
            {
                blobCloud.UploadFromStream(fileStream);
                Console.WriteLine("File is uploaded to the cloud");
            }

            // Learning 2: Download a blob from Azure to the local host

            // Just copy a blob from cloud to a local file. Assume the same file which has been uploaded in the previous step
            blobCloud.DownloadToFile(@"C:\Users\Administrator\Pictures\photocopy.jpg", FileMode.CreateNew);

            // Learning 3: List all blob objects URI from a container

            //List all blobs in a container
            var blobs = cloudContainer.ListBlobs();

            foreach (var blob in blobs)
            {
                Console.Write(blob.Uri);
            }

            // Learning 4: List properties of a continer (Folder) and listing metadata of a container

            cloudContainer.FetchAttributes();
            Console.WriteLine(cloudContainer.Properties.LastModified);
            Console.WriteLine(cloudContainer.Properties.ETag);
            var metaData = cloudContainer.Metadata;

            foreach (var metaDataItem in metaData)
            {
                Console.Write("Key " + metaDataItem.Key + " & ");
                Console.WriteLine("Value" + metaDataItem.Value);
            }

            // Learning 5: Setting metaData for a container

            // Method 1
            cloudContainer.Metadata.Add("SampleKey", "SampleValue");
            // Method 2
            cloudContainer.Metadata["SecondSample"] = "Second Value";

            // Dont ask me why FetchAttributes() and SetMetadata() ! why not SetAttributes() or GetMetaData? Microsoft Way!
            cloudContainer.SetMetadata();


            // Learning 6: Setting permission for a container
            BlobContainerPermissions permissions = cloudContainer.GetPermissions();

            Console.WriteLine("Container permission " + permissions.PublicAccess.ToString());
            foreach (var sharedAccessPolicy in permissions.SharedAccessPolicies)
            {
                Console.WriteLine(sharedAccessPolicy.Key.ToString() + " = " + sharedAccessPolicy.Value.ToString());
            }
            // In order to as per the parent container
            permissions.PublicAccess = BlobContainerPublicAccessType.Container;
            // In order to remove the public access
            permissions.PublicAccess = BlobContainerPublicAccessType.Off;
            //Finally set the permission
            cloudContainer.SetPermissions(permissions);



            // Learning 7: Azure copy from one blob to another

            // Create a new Block Blog Reference
            CloudBlockBlob copyBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg");

            // Copy the original - blobCloud to the copyBlob
            copyBlob.StartCopy(new Uri(blobCloud.Uri.AbsoluteUri));


            // Learning 8: Copy all blobs from one container to another
            CloudBlobContainer sourceContainer = cloudContainer;
            CloudBlobContainer targetContainer = cloudBlobClient.GetContainerReference("newimages");

            targetContainer.CreateIfNotExists();
            foreach (var blob in sourceContainer.ListBlobs())
            {
                var sourceBlob = blob as CloudBlob;
                Console.WriteLine("Source Blob " + sourceBlob.Name);
                CloudBlockBlob newBlob = targetContainer.GetBlockBlobReference(sourceBlob.Name);
                newBlob.StartCopy(new Uri(blob.Uri.AbsoluteUri));
            }


            // Learning 9: Rename the blob
            CloudBlockBlob sourceBlockBlob = cloudContainer.GetBlockBlobReference("photo-copy.jpg");
            CloudBlockBlob targetBlockBlob = cloudContainer.GetBlockBlobReference("copy-photo.jpg");

            targetBlockBlob.StartCopy(new Uri(sourceBlockBlob.Uri.AbsoluteUri));
            while (targetBlockBlob.CopyState.Status == CopyStatus.Pending)
            {
                // Sleep for 3 seconds
                System.Threading.Thread.Sleep(2000);
            }
            sourceBlockBlob.Delete();


            // Learning 10: Appending to a blob
            DateTime           date         = DateTime.Today;
            CloudBlobContainer logContainer = cloudBlobClient.GetContainerReference("logs");
            CloudAppendBlob    logBlog      = logContainer.GetAppendBlobReference(string.Format("{0}{1}", date.ToString("yyyyMMdd"), ".log"));

            logContainer.CreateIfNotExists();

            // If the append blog does not exists, create one
            if (!logBlog.Exists())
            {
                logBlog.CreateOrReplace();
            }

            // AppendText
            logBlog.AppendText(string.Format("{0} : Azure is rocking in the cloud space at ", date.ToString("HH:MM:ss")));
            // Similar to the AppendText, there are
            // logBlog.AppendBlock
            // logBlog.AppendFromByteArray
            // logBlog.AppendFromFile
            // logBlog.AppendFromStream

            // Finally display the content of the log file.
            Console.WriteLine(logBlog.DownloadText());

            // Learning 11: Multiple Chunk of file upload to Azure
            AzureStorageReference.Program.uploadLargeFiles(cloudBlobClient, @"C:\Users\Administrator\Pictures");


            //Learning 12: Upload using Async

            AsyncCallback  callBack  = new AsyncCallback(x => Console.WriteLine("Copy Async Completed"));
            CloudBlockBlob copyAsync = cloudContainer.GetBlockBlobReference("newphoto.png");

            copyAsync.BeginStartCopy(blobCloud, callBack, null);

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }