Example #1
0
        async Task <SourcedState> LoadSourcedStateFromScratch()
        {
            SourcedState state = new SourcedState();

            if (!await file.ExistsAsync())
            {
                return(state);
            }

            var records = await GetAllAsync();

            var dict = new Dictionary <long, RecordContainer>();

            foreach (var e in records)
            {
                dict[e.VersionId] = e;
            }

            if (dict.Count > 0)
            {
                state.AddNewRecords(dict);
            }

            return(state);
        }
        public static async void UploadErrorLogs()
        {
            var TaskA = new System.Threading.Tasks.Task(async() =>
            {
                pathcre();
                try
                {
                    sc             = new StorageCredentials("icsintegration", "+7UyQSwTkIfrL1BvEbw5+GF2Pcqh3Fsmkyj/cEqvMbZlFJ5rBuUgPiRR2yTR75s2Xkw5Hh9scRbIrb68GRCIXA==");
                    storageaccount = new CloudStorageAccount(sc, true);
                    blobClient     = storageaccount.CreateCloudBlobClient();
                    container      = blobClient.GetContainerReference("userlogs");
                    await container.CreateIfNotExistsAsync();                   //{
                    append = container.GetAppendBlobReference(userid + ".csv"); //}
                    if (!await append.ExistsAsync())
                    {
                        await append.CreateOrReplaceAsync();
                    }
                }
                catch (Exception exe)
                {
                    Log.Error("Error", exe.Message);
                }
            });

            TaskA.Start();
        }
Example #3
0
        public CloudAppendBlob GetCloudBlob(CloudStorageAccount storageAccount, string folderName, string fileName, bool bypassBlobCreationValidation)
        {
            if (cloudAppendBlob != null)
            {
                return(cloudAppendBlob);
            }

            var cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(folderName);

            try
            {
                cloudBlobContainer.CreateIfNotExistsAsync().SyncContextSafeWait(waitTimeoutMilliseconds);
                cloudAppendBlob = cloudBlobContainer.GetAppendBlobReference(fileName);
                if (!cloudAppendBlob.ExistsAsync().Result)
                {
                    cloudAppendBlob.CreateOrReplaceAsync().Wait();
                }
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Failed to create blob container: {ex}");
                if (!bypassBlobCreationValidation)
                {
                    throw;
                }
            }
            return(cloudAppendBlob);
        }
Example #4
0
        /// <summary>
        /// Log the event to persistent store
        /// </summary>
        /// <param name="username">username of the caller</param>
        /// <param name="message">message from the caller</param>
        private static async void Persist(string username, string message)
        {
            string accountKey  = Environment.GetEnvironmentVariable("AccountKey");
            string accountName = Environment.GetEnvironmentVariable("AccountName");

            // Implement the accout, set true for https for SSL.
            StorageCredentials  creds      = new StorageCredentials(accountName, accountKey);
            CloudStorageAccount strAcc     = new CloudStorageAccount(creds, true);
            CloudBlobClient     blobClient = strAcc.CreateCloudBlobClient();

            // Setup our container we are going to use and create it.
            CloudBlobContainer container = blobClient.GetContainerReference("logs");
            await container.CreateIfNotExistsAsync();

            // Build my typical log file name.
            DateTime date         = DateTime.Today;
            DateTime dateLogEntry = DateTime.Now;

            // This creates a reference to the append blob we are going to use.
            CloudAppendBlob appBlob = container.GetAppendBlobReference(
                string.Format("{0}{1}", date.ToString("yyyyMMdd"), ".log"));

            // Now we are going to check if todays file exists and if it doesn't we create it.
            if (!await appBlob.ExistsAsync())
            {
                await appBlob.CreateOrReplaceAsync();
            }

            // Add the entry to our log.
            await appBlob.AppendTextAsync($"{dateLogEntry.ToString("o")}-{username}-{message}\r\n");
        }
        public async Task UploadFile(string sourcePath, string sourceFilename, string containerName, string targetFilename, string contentType, bool append = false)
        {
            byte[] fileContent                = File.ReadAllBytes(Path.Join(fixPath(sourcePath), sourceFilename));
            CloudStorageAccount account       = CloudStorageAccount.Parse(storageConnectionString);
            CloudBlobClient     serviceClient = account.CreateCloudBlobClient();

            var container = serviceClient.GetContainerReference(getContainerName(containerName));

            container.CreateIfNotExistsAsync().Wait();
            CloudAppendBlob blob = container.GetAppendBlobReference(getFilepathForContainer(containerName, targetFilename));

            if (!append)
            {
                await blob.CreateOrReplaceAsync();
            }
            else
            {
                if (!blob.ExistsAsync().Result)
                {
                    throw new Exception($"Cannot append to nonexistent blob file {sourceFilename}");
                }
            }
            blob.Properties.ContentType = contentType;
            await blob.AppendTextAsync(fileContent.ToString());
        }
        private async Task CheckBloksCountAsync()
        {
            if (!_blob.Properties.AppendBlobCommittedBlockCount.HasValue)
            {
                await _blob.FetchAttributesAsync();
            }
            if (_blob.Properties.AppendBlobCommittedBlockCount.Value < _maxBlocksCount)
            {
                return;
            }

            int i = 1;

            while (true)
            {
                var fileName = $"{_blob.Name}--{i:00}";
                _blob = _blobContainer.GetAppendBlobReference(fileName);
                bool exists = await _blob.ExistsAsync();

                if (!exists)
                {
                    break;
                }
                ++i;
            }
            _log.WriteInfo("BlobSaver.CheckBloksCountAsync", _container, $"Created additional blob - {_blob.Name}");
            await InitBlobPropertiesAsync();
        }
Example #7
0
        /// <summary>
        /// Gets the content from blob.
        /// </summary>
        /// <param name="blobName">Name of blob</param>
        /// <returns>csv data as string</returns>
        public async Task <string> GetContentAsync(string blobName)
        {
            string appendBlobContent = null;

            try
            {
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString("AzureStorage"));
                CloudBlobClient     cloudBlobClient     = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer  cloudBlobContainer  = cloudBlobClient.GetContainerReference(configuration.GetConnectionString("ContainerName"));
                CloudAppendBlob     cloudAppendBlob     = cloudBlobContainer.GetAppendBlobReference(blobName);
                if (await cloudAppendBlob.ExistsAsync())
                {
                    appendBlobContent = await cloudAppendBlob.DownloadTextAsync();
                }
                else
                {
                    appendBlobContent = await GetZipAsync(blobName);
                }
                return(appendBlobContent);
            }
            catch (Exception)
            {
                return(appendBlobContent);
            }
        }
Example #8
0
 public static void CreateIfNotExists(this CloudAppendBlob blob)
 {
     if (blob.ExistsAsync().Result)
     {
         blob.CreateOrReplaceAsync().Wait();
     }
 }
Example #9
0
 public static async void UploadAsyncLogs(string log)
 {
     try
     {
         //pathcre();
         //Console.WriteLine(log);
         if (userid == "0" || userid == "DefaultLogs")
         {
             if (CurrentUser.GetGuestId() == "0" || CurrentUser.GetGuestId() == null)
             {
                 userid = "DefaultLogs";
             }
             else
             {
                 userid = "g_" + CurrentUser.GetGuestId();
             }
         }
         CloudAppendBlob append = container.GetAppendBlobReference(userid + ".csv");
         Console.WriteLine("User id " + userid + " ");
         Console.WriteLine(log);
         if (!await append.ExistsAsync())
         {
             await append.CreateOrReplaceAsync();
         }
         await append.AppendTextAsync(log);
     }
     catch (Exception ex)
     {
         //Console.WriteLine(ex.Message);
     }
 }
Example #10
0
        public async Task Test_31_Blob_withInfiniteLease()
        {
            CloudBlobContainer cbc = _Client.GetContainerReference("test-append-blob-container");
            await cbc.CreateIfNotExistsAsync();

            CloudAppendBlob blob       = cbc.GetAppendBlobReference("test-append-blob-withlease2.data");
            var             blobExists = await blob.ExistsAsync();

            if (!blobExists)
            {
                await blob.CreateOrReplaceAsync();
            }

            string data = string.Empty.PadLeft(64 * 1024, '*');

            string leaseIdGuid = Guid.NewGuid().ToString();

            var oc      = new OperationContext();
            var ac      = new AccessCondition();
            var leaseID = await blob.AcquireLeaseAsync(null, leaseIdGuid, ac, null, null);

            ac.LeaseId = leaseID;

            try
            {
                for (int ix = 0; ix < 10; ix++)
                {
                    await blob.AppendTextAsync(data, null, ac, null, oc);
                }
            }
            finally
            {
                await blob.ReleaseLeaseAsync(ac, null, oc);
            }
        }
Example #11
0
        private static async Task <List <FileRecord> > ReadStreamToRecords(CloudAppendBlob blob)
        {
            List <FileRecord> records = new List <FileRecord>();

            using (var s = new MemoryStream())
            {
                if (!await blob.ExistsAsync())
                {
                    return(records);
                }

                await blob.DownloadRangeToStreamAsync(s, 0, blob.Properties.Length);

                s.Position = 0;
                var reader = new BinaryReader(s);

                var record = new FileRecord();
                while (record.ReadContentFromStream(reader))
                {
                    records.Add(record);
                    record = new FileRecord();
                }
            }

            return(records);
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            string name = req.Query["name"];

            try
            {
                log.LogInformation("C# HTTP trigger function processed a request.");


                var StorageURL = Environment.GetEnvironmentVariable("StorageURL");


                string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic data        = JsonConvert.DeserializeObject(requestBody);
                name = name ?? data?.name;


                var    azureServiceTokenProvider = new AzureServiceTokenProvider();
                string accessToken = await azureServiceTokenProvider.GetAccessTokenAsync("https://storage.azure.com");


                log.LogInformation("accessToken : retrieved");


                // create the credential, using the
                var tokenCredential    = new Microsoft.WindowsAzure.Storage.Auth.TokenCredential(accessToken);
                var storageCredentials = new StorageCredentials(tokenCredential);

                log.LogInformation("credentials : created");
                var fileName = "append";
                var Uri      = new Uri(StorageURL + fileName);
                var blob     = new CloudAppendBlob(Uri, storageCredentials);

                log.LogInformation($"blobfile : setup {0}", Uri);

                if (!(await blob.ExistsAsync()))
                {
                    await blob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null);
                }

                await blob.AppendTextAsync(name);

                var fileName2 = "regular.txt";
                var Uri2      = new Uri(StorageURL + fileName2);
                var blob2     = new CloudBlockBlob(Uri2, storageCredentials);

                await blob2.UploadTextAsync(name);
            }
            catch (Exception ex)
            {
                log.LogInformation($"EXEC {ex.ToString()} ");
            }
            return(name != null
            ? (ActionResult) new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body"));
        }
Example #13
0
        public async Task WriteLog(ILog log, CloudBlobContainer container = null)
        {
            try
            {
                if (container == null)
                {
                    container = await GetCloudBlobLogsContainer(LOGS_KEY);
                }
                var    path = this.GetCloudContainerLogPath(log.LogType);
                var    dir  = container.GetDirectoryReference(path);
                var    file = DateTime.Now.Hour.ToString("00") + "00.log";
                string json = JsonConvert.SerializeObject(log, JSON.SerializationSettings);

                // Note: AppendBlockBlob is not available in the Storage Emulator/Explorer
                if (GetCloudConnectionString(LOGS_KEY) == "UseDevelopmentStorage=true")
                {
                    CloudBlockBlob blob = dir.GetBlockBlobReference(file);
                    try
                    {
                        await blob.UploadTextAsync(json);
                    }
                    catch (StorageException ex)
                    {
                    }
                    catch (Exception ex)
                    {
                    }
                }
                else
                {
                    CloudAppendBlob blob = dir.GetAppendBlobReference(DateTime.Now.Hour.ToString("00") + "00.log");
                    if (!(await blob.ExistsAsync()))
                    {
                        try
                        {
                            await blob.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null);
                        }
                        catch (StorageException ex)
                        {
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    using (var stream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
                    {
                        await blob.AppendBlockAsync(stream);
                    }
                }
            }
            catch (StorageException ex)
            {
            }
            catch (Exception ex)
            {
            }
        }
        private async static void AcquireLeaseDemo()
        {
            try
            {
                string             name      = "LeaseDemo/text-0221e06e5e254fb3bb0bf0e37d9d0e32.txt";
                CloudBlobContainer container = _cloudBlobClient.GetContainerReference("text-files");
                await container.CreateIfNotExistsAsync();

                CloudAppendBlob cloudAppendBlob = container.GetAppendBlobReference(name);
                bool            shouldContinue  = true;
                if (await cloudAppendBlob.ExistsAsync())
                {
                    if (cloudAppendBlob.Properties.LeaseStatus == LeaseStatus.Locked)
                    {
                        shouldContinue = false;
                        Console.WriteLine("Blob is currently in lease");
                    }
                    else
                    {
                        await cloudAppendBlob.CreateOrReplaceAsync();
                    }
                }
                if (shouldContinue)
                {
                    string           proposedLeaseId  = Guid.NewGuid().ToString("n");
                    AccessCondition  accessCondition  = new AccessCondition();
                    OperationContext operationContext = new OperationContext();
                    Console.WriteLine("Acquiring lock...");

                    string leaseId = await cloudAppendBlob.AcquireLeaseAsync(null, proposedLeaseId, accessCondition, null, operationContext);

                    accessCondition.LeaseId = leaseId;
                    try
                    {
                        Console.WriteLine("Uploading text...");
                        for (int i = 0; i <= 50; i++)
                        {
                            await cloudAppendBlob.AppendTextAsync($"{Guid.NewGuid().ToString("n")}{Environment.NewLine}", UTF8Encoding.UTF8, accessCondition, null, operationContext);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    finally
                    {
                        await cloudAppendBlob.ReleaseLeaseAsync(accessCondition, null, operationContext);

                        Console.WriteLine("Process finished. Lock has been released");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
Example #15
0
        /// <summary>
        /// Delivers blob information like length, lastmodified, etag
        /// </summary>
        /// <returns></returns>
        public async Task <BlobProperties> GetBlobProperties()
        {
            if (await blob.ExistsAsync())
            {
                await blob.FetchAttributesAsync();

                return(blob.Properties);
            }

            return(null);
        }
Example #16
0
        private async Task <int> UploadToAzureStorage(String messageString)
        {
            try
            {
                //  create Azure Storage
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=norkartstorageaccount;AccountKey=ywvq9KDDK/F1mqiz1NJ/vEj9lT7wuVrEqWtO1f3hi+i4vw9gOCvJG1rOjVKpjx/Ki1q6rVjG7uUalT+hWdXxCA==");


                //  create a blob client.
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                //  create a container
                CloudBlobContainer container = blobClient.GetContainerReference("norkartstorageaccount");


                CloudAppendBlob appBlob = container.GetAppendBlobReference("Temperature&Humidity/" + DateTime.Now.ToString("yyyy-dd-M") + ".json");

                //CloudAppendBlob appBlob = container.GetAppendBlobReference("TestFile.txt");

                System.Diagnostics.Debug.WriteLine("APPBLOB EXISTS: " + appBlob.ExistsAsync().Result.ToString());
                if (appBlob.ExistsAsync().Result.ToString() != "True")
                {
                    await appBlob.CreateOrReplaceAsync();
                }
                //  create a local file
                //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss"), CreationCollisionOption.GenerateUniqueName);

                //StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("TestFile.txt", CreationCollisionOption.GenerateUniqueName);


                await appBlob.AppendTextAsync(messageString + Environment.NewLine);

                return(1);
            }
            catch
            {
                //  return error
                System.Diagnostics.Debug.WriteLine("STORAGE ERROR");
                return(0);
            }
        }
Example #17
0
        //获取blob数据
        public static async Task <string> GetBlobConent(string blobName, CloudBlobContainer blobContainer)
        {
            CloudAppendBlob blob = blobContainer.GetAppendBlobReference($"{(object)blobName}.json");
            bool            flag = await blob.ExistsAsync();

            if (flag)
            {
                return(await blob.DownloadTextAsync());
            }
            else
            {
                return("");
            }
        }
Example #18
0
        private async Task SendBufferAsync(LoggingEvent[] events)
        {
            CloudAppendBlob appendBlob = _cloudBlobContainer.GetAppendBlobReference(Filename(_directoryName));

            if (!await appendBlob.ExistsAsync().ConfigureAwait(false))
            {
                await appendBlob.CreateOrReplaceAsync().ConfigureAwait(false);
            }
            else
            {
                _lineFeed = Environment.NewLine;
            }

            await Task.WhenAll(events.Select(ProcessEvent));
        }
Example #19
0
        private async Task <CloudAppendBlob> GetBlobAsync(DateTime eventTime)
        {
            string tagName = eventTime.ToString("yyyy-MM-dd");

            if (tagName != _currentTagName)
            {
                _currentTagName = tagName;
                _appendBlob     = _blobContainer.GetAppendBlobReference($"{_blobNamePrefix}{_currentTagName}.txt");
                if (!(await _appendBlob.ExistsAsync()))
                {
                    await _appendBlob.CreateOrReplaceAsync();
                }
            }

            return(_appendBlob);
        }
Example #20
0
        public static async void UploadErrorLogs()
        {
            pathcre();
            try
            {
                sc             = new StorageCredentials("icsintegration", "+7UyQSwTkIfrL1BvEbw5+GF2Pcqh3Fsmkyj/cEqvMbZlFJ5rBuUgPiRR2yTR75s2Xkw5Hh9scRbIrb68GRCIXA==");
                storageaccount = new CloudStorageAccount(sc, true);
                blobClient     = storageaccount.CreateCloudBlobClient();
                container      = blobClient.GetContainerReference("userlogs");

                //var csv = new StringBuilder();
                //var newLine = string.Format("Exception", DateTime.Now, "Test", "Test", "Test");
                //csv.AppendLine(newLine);
                //File.AppendAllText(logspath, csv.ToString());

                await container.CreateIfNotExistsAsync();

                //CloudBlockBlob blob = container.GetBlockBlobReference(CurrentUser.getUserId() + ".csv"); //(path);
                //if (CurrentUser.getUserId() == null)
                //{
                append = container.GetAppendBlobReference(userid + ".csv");
                //}
                //else
                //{
                //	append = container.GetAppendBlobReference(CurrentUser.getUserId() + ".csv");
                //}
                if (!await append.ExistsAsync())
                {
                    await append.CreateOrReplaceAsync();
                }


                //await append.AppendBlockAsync("aksfhgaUKGdfkAUSFDAUSGFD");
                //await append.UploadTextAsync(string.Format("Exception,Test,Test,Test"));
                //await append.AppendTextAsync(  string.Format("Exception,Test1,Test1,Test1 "+"\n"));
                //await append.AppendTextAsync(string.Format("{0},{1},{2},{3},{4}", "Exception", DateTime.Now, lineno, screenid + "\n"));

                //using (var fs = System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
                //            {
                //                await blob.UploadFromStreamAsync(fs);
                //            }
            }
            catch (Exception exe)
            {
                Log.Error("Error", exe.Message);
            }
        }
Example #21
0
        public static async Task WriteStateToBlob(CloudBlobContainer blobContainer, string blobName, string content)
        {
            CloudAppendBlob appendBlob = blobContainer.GetAppendBlobReference($"{(object)blobName}.json");

            bool flag = await appendBlob.ExistsAsync();

            if (!flag)
            {
                await appendBlob.CreateOrReplaceAsync();
            }
            appendBlob.Properties.ContentType = "application/json";
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
            {
                stream.Position = 0;
                await appendBlob.UploadFromStreamAsync(stream);
            }
        }
        public async Task Test_30_Blob_InfiniteLease()
        {
            // Containers are like folders
            CloudBlobContainer container = _client.GetContainerReference("test-append-blob-container");

            await container.CreateIfNotExistsAsync();

            // Blobs are like files
            CloudAppendBlob blob = container.GetAppendBlobReference("test-append-blob-with-lease");

            var blobExists = await blob.ExistsAsync();

            if (!blobExists)
            {
                await blob.CreateOrReplaceAsync();
            }

            string data = string.Empty.PadLeft(64 * 1024, '*');

            string leaseId = Guid.NewGuid().ToString();

            var operationContext = new OperationContext();

            var accessCondition = new AccessCondition();

            // For pessimistic access control
            // Acquire lease
            var leaseID = await blob.AcquireLeaseAsync(null, leaseId, accessCondition, null, operationContext);

            accessCondition.LeaseId = leaseID;

            try
            {
                // Make modifications based on that lease
                for (int i = 0; i < 1000; i++)
                {
                    await blob.AppendTextAsync(data, null, accessCondition, null, operationContext);
                }
            }
            finally
            {
                // And finally release that lease
                await blob.ReleaseLeaseAsync(accessCondition, null, operationContext);
            }
        }
        public async Task DownloadFile(string targetPath, string targetFilename, string sasUri, bool append = false)
        {
            string          targetFullPath = Path.Join(fixPath(targetPath), targetFilename);
            CloudAppendBlob blob           = new CloudAppendBlob(new Uri(sasUri));

            if (!append)
            {
                await blob.DownloadToFileAsync(targetFullPath, FileMode.Create);
            }
            else
            {
                if (!blob.ExistsAsync().Result)
                {
                    throw new Exception($"Cannot download nonexistent blob file {targetFilename}");
                }
                await blob.DownloadToFileAsync(targetFullPath, FileMode.Append);
            }
        }
Example #24
0
        public static void UploadNewText()
        {
            Console.Write("Please enter text to add to the blob: ");
            string text = Console.ReadLine();

            //file name
            string          fileName = "test.csv";
            CloudAppendBlob blob     = container.GetAppendBlobReference(fileName);

            //Create new blob file, if it does not exist.
            if (blob.ExistsAsync().Result == false)
            {
                blob.CreateOrReplaceAsync();
            }

            //Append text to the end of data.
            blob.AppendTextAsync(text);
        }
Example #25
0
        } // End of the DeleteByLastModifiedDate method

        #endregion

        #region Helper methods

        /// <summary>
        /// Append text to an append blob
        /// </summary>
        private async Task WriteToAppendBlob(string blob_name, string log)
        {
            // Get a blob reference
            CloudAppendBlob blob = this.container.GetAppendBlobReference(blob_name);

            // Create a blob if it doesn't exist
            if (await blob.ExistsAsync() == false)
            {
                await blob.CreateOrReplaceAsync();
            }

            // Append the log to a blob
            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(log)))
            {
                // Append text to the blob
                await blob.AppendBlockAsync(stream);
            }

        } // End of the WriteToAppendBlob method
        public async Task UploadFile(string sourcePath, string sourceFilename, string sasUri, string contentType, bool append = false)
        {
            byte[]          fileContent = File.ReadAllBytes(Path.Join(fixPath(sourcePath), sourceFilename));
            CloudAppendBlob blob        = new CloudAppendBlob(new Uri(sasUri));

            if (!append)
            {
                await blob.CreateOrReplaceAsync();
            }
            else
            {
                if (!blob.ExistsAsync().Result)
                {
                    throw new Exception($"Cannot append to nonexistent blob file {sourceFilename}");
                }
            }
            blob.Properties.ContentType = contentType;
            await blob.AppendTextAsync(fileContent.ToString());
        }
        private void Roll()
        {
            _day = DateTime.Now;
            string stamp    = _day.ToString("yyyy-MM-dd");
            string blobName = $"{_baseBlobName}_{stamp}.log";

            _blob = _container.GetAppendBlobReference(blobName);

            if (!_blob.ExistsAsync().Result)
            {
                _blob.CreateOrReplaceAsync().Wait();

                _blob.FetchAttributesAsync().Wait();

                _blob.Metadata["Day"]  = stamp;
                _blob.Metadata["Name"] = _baseBlobName;

                _blob.SetMetadataAsync().Wait();
            }
        }
Example #28
0
        //获取blob数据
        public static async Task <string> GetBlobConent(string blobName, CloudBlobContainer blobContainer)
        {
            CloudAppendBlob blob = blobContainer.GetAppendBlobReference($"{(object)blobName}.json");
            bool            flag = await blob.ExistsAsync();

            bool   exist = flag;
            string str1;

            if (exist)
            {
                string str2 = await blob.DownloadTextAsync();

                object obj = (object)str2;
                str1 = $"[{obj}]";
            }
            else
            {
                str1 = "";
            }
            return(str1);
        }
Example #29
0
        //public async Task WriteBlockBlobAsync(string containerName, string filename,
        //                    byte[] source, string contentType, CancellationToken token = default(CancellationToken))
        //{
        //    if (source == null)
        //    {
        //        throw new ArgumentNullException("source");
        //    }

        //    CloudBlobContainer container = await GetContainerReferenceAsync(containerName);
        //    CloudBlockBlob blob = container.GetBlockBlobReference(filename);
        //    blob.Properties.ContentType = contentType;

        //    await UploadAsync(blob, source, token);
        //}

        public async Task WriteAppendBlobAsync(string containerName, string filename, byte[] source, string contentType = "application/octet-stream", CancellationToken token = default(CancellationToken))
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            CloudBlobContainer container = await GetContainerReferenceAsync(containerName);

            CloudAppendBlob blob = container.GetAppendBlobReference(filename);

            if (!await blob.ExistsAsync())
            {
                blob.Properties.ContentType = contentType;
                await UploadAsync(blob, source, token);
            }
            else
            {
                await blob.AppendFromByteArrayAsync(source, 0, source.Length);
            }
        }
        protected override void Write(LogEventInfo logEvent)
        {
            _client = _client ?? CloudStorageAccount.Parse(ConnectionString.Render(logEvent)).CreateCloudBlobClient();

            if (_client == null)
            {
                return;
            }

            var containerName = Container.Render(logEvent);
            var blobName      = BlobName.Render(logEvent);

            if (_container == null || _container.Name != containerName)
            {
                _container = _client.GetContainerReference(containerName);
                _container.CreateIfNotExistsAsync().Wait();
                _blob = null;
            }

            if (_blob == null || _blob.Name != blobName || _forceCheck)
            {
                _blob = _container.GetAppendBlobReference(blobName);

                if (!_blob.ExistsAsync().Result)
                {
                    try
                    {
                        _blob.Properties.ContentType = "text/plain";
                        _blob.CreateOrReplaceAsync().Wait();
                        _blob.SetPropertiesAsync().Wait();
                    }
                    catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.Conflict)
                    {
                        // to be expected
                    }
                }
            }

            _blob.AppendTextAsync(Layout.Render(logEvent) + "\r\n").Wait();
        }