Example #1
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 #2
0
        private static async Task <string> GetCsvFileFromAzure(CloudBlobContainer container, string blobPath)
        {
            CloudAppendBlob blob    = container.GetAppendBlobReference(blobPath);
            string          csvText = await blob.DownloadTextAsync();

            return(csvText);
        }
Example #3
0
        public async Task <string> DownloadTextByAppendUri(Uri uri)
        {
            var blob    = new CloudAppendBlob(uri, _cloudBlobClient);
            var content = await blob.DownloadTextAsync();

            return(content);
        }
Example #4
0
        private static async Task <List <CSVResponse> > GetSurveyResponses(object data)
        {
            AzureBlobDocuments document = jsonHelper.FromJson <AzureBlobDocuments>(data.ToString());

            CSVResponse        question    = new CSVResponse();
            List <CSVResponse> csvResponse = new List <CSVResponse>();
            bool   headerIgnore            = true;
            string csvData = null;

            CloudAppendBlob appendBlob = CONTAINER.GetAppendBlobReference(document.Filename);

            try
            {
                csvData = await appendBlob.DownloadTextAsync();

                if (csvData != null)
                {
                    csvData = csvData.Replace("\r\n", ",-,");     //replace "\r\n" with end line character
                    csvData = csvData.Remove(csvData.Length - 1); //remove trailing "," before splitting to stop erronous final record.
                    string[] values = csvData.Split(',');

                    for (int i = 0; i < values.Count(); i++)
                    {
                        Int32.TryParse(values[i++], out int surveyID);
                        question.SurveyID = surveyID;
                        question.Date     = values[i++];
                        question.Time     = values[i++];

                        int j = 1;
                        do
                        {
                            if (values[i].Equals("-"))
                            {
                                break;
                            }
                            question.Responses.Add(new QuestionResponse()
                            {
                                QuestionNumber = j++, Answer = values[i++]
                            });
                        } while (i <= values.Count());

                        if (!headerIgnore)
                        {
                            csvResponse.Add(question);
                        }

                        question     = new CSVResponse();
                        headerIgnore = false;
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception Caught - " + ex.Message);
            }

            return(csvResponse);
        }
        //获取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 #6
0
        private static async Task downloadAsText() // Suggested FOR API DOWNLOADING AS TEXT.
        {
            Console.Write("Please enter text to download from blob: ");
            string text = Console.ReadLine();

            // Get a reference to a blob named.
            CloudAppendBlob appendBlob = container.GetAppendBlobReference(text);

            //Blob donwload as String
            string csvData = await appendBlob.DownloadTextAsync();

            //print out the data
            Console.WriteLine(csvData);
        }
        //获取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 #8
0
        public async static Task ProcessMessagesAsync(
            string blobUris)
        {
            bool stopRedundantTransfer  = false;
            bool blobCursorupdateStatus = false;
            long cursorPosition         = 0;

            if (Uri.TryCreate(blobUris, UriKind.Absolute, out Uri validUri))
            {
                //TODO send CancellationToken
                var blobItem = await blobClient.GetBlobReferenceFromServerAsync(validUri);

                if (blobItem.GetType() == typeof(CloudAppendBlob))
                {
                    CloudAppendBlob appBlob = (CloudAppendBlob)blobItem;
                    //TODO send CancellationToken
                    string blobData = await appBlob.DownloadTextAsync();

                    Console.WriteLine(blobData);
                }
                else if (blobItem.GetType() == typeof(CloudBlockBlob))
                {
                    CloudBlockBlob appBlob = (CloudBlockBlob)blobItem;

                    if (!long.TryParse(appBlob.Properties.Length.ToString(), out long newLength))
                    {
                        newLength = 0;
                    }

                    while (!blobCursorupdateStatus)
                    {
                        CloudBlockBlob blob     = cacheContainer.GetBlockBlobReference(appBlob.Name);
                        var            position = await GetBlobReadPosition(blob);

                        //2669 < 3050
                        if (position < newLength)
                        {
                            blobCursorupdateStatus = await UpdateBlobReadCursorPosition(blob, newLength.ToString());

                            cursorPosition = position;
                        }
                        else if (position >= newLength)
                        {
                            blobCursorupdateStatus = true;
                            stopRedundantTransfer  = true;
                        }
                    }

                    if (!stopRedundantTransfer)
                    {
                        string value = null;
                        using (var stream = new MemoryStream())
                        {
                            await appBlob.DownloadToStreamAsync(stream);

                            //Console.WriteLine($"Length: {stream.Length}");

                            using (var reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                stream.Position = cursorPosition;
                                while (!reader.EndOfStream)
                                {
                                    value = await reader.ReadLineAsync();

                                    Console.WriteLine(value);
                                }
                            }
                        }
                    }
                }
            }
            Console.ReadLine();
        }
Example #9
0
        public async Task RunPermissionsTestBlobs(SharedAccessAccountPolicy policy)
        {
            CloudBlobClient blobClient    = GenerateCloudBlobClient();
            string          containerName = "c" + Guid.NewGuid().ToString("N");

            try
            {
                CloudStorageAccount account           = new CloudStorageAccount(blobClient.Credentials, false);
                string              accountSASToken   = account.GetSharedAccessSignature(policy);
                StorageCredentials  accountSAS        = new StorageCredentials(accountSASToken);
                CloudStorageAccount accountWithSAS    = new CloudStorageAccount(accountSAS, blobClient.StorageUri, null, null, null);
                CloudBlobClient     blobClientWithSAS = accountWithSAS.CreateCloudBlobClient();
                CloudBlobContainer  containerWithSAS  = blobClientWithSAS.GetContainerReference(containerName);
                CloudBlobContainer  container         = blobClient.GetContainerReference(containerName);

                // General pattern - If current perms support doing a thing with SAS, do the thing with SAS and validate with shared
                // Otherwise, make sure SAS fails and then do the thing with shared key.

                // Things to do:
                // Create the container (Create / Write perms, Container RT)
                // List containers with prefix (List perms, Service RT)
                // Create an append blob (Create / Write perms, Object RT)
                // Append a block to append blob (Add / Write perms, Object RT)
                // Read the data from the append blob (Read perms, Object RT)
                // Delete the blob (Delete perms, Object RT)
                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Container) == SharedAccessAccountResourceTypes.Container))
                {
                    await containerWithSAS.CreateAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await containerWithSAS.CreateAsync(), "Create a container should fail with SAS without Create or Write and Container-level permissions.");

                    await container.CreateAsync();
                }

                Assert.IsTrue(await container.ExistsAsync());

                if (((policy.Permissions & SharedAccessAccountPermissions.List) == SharedAccessAccountPermissions.List) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Service) == SharedAccessAccountResourceTypes.Service))
                {
                    ContainerResultSegment           segment = null;
                    BlobContinuationToken            ct      = null;
                    IEnumerable <CloudBlobContainer> results = null;
                    do
                    {
                        segment = await blobClientWithSAS.ListContainersSegmentedAsync(container.Name, ct);

                        ct      = segment.ContinuationToken;
                        results = segment.Results;
                    }while(ct != null && !results.Any());

                    Assert.AreEqual(container.Name, segment.Results.First().Name);
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => { ContainerResultSegment segment = await blobClientWithSAS.ListContainersSegmentedAsync(container.Name, null); segment.Results.First(); }, "List containers should fail with SAS without List and Service-level permissions.");
                }

                string          blobName          = "blob";
                CloudAppendBlob appendBlob        = container.GetAppendBlobReference(blobName);
                CloudAppendBlob appendBlobWithSAS = containerWithSAS.GetAppendBlobReference(blobName);

                //Try creating credentials using SAS Uri directly
                CloudAppendBlob appendBlobWithSASUri = new CloudAppendBlob(new Uri(container.Uri + accountSASToken));

                if ((((policy.Permissions & SharedAccessAccountPermissions.Create) == SharedAccessAccountPermissions.Create) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await appendBlobWithSAS.CreateOrReplaceAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await appendBlobWithSAS.CreateOrReplaceAsync(), "Creating an append blob should fail with SAS without Create or Write and Object-level perms.");

                    await appendBlob.CreateOrReplaceAsync();
                }

                Assert.IsTrue(await appendBlob.ExistsAsync());

                string blobText = "blobText";
                if ((((policy.Permissions & SharedAccessAccountPermissions.Add) == SharedAccessAccountPermissions.Add) || ((policy.Permissions & SharedAccessAccountPermissions.Write) == SharedAccessAccountPermissions.Write)) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(blobText)))
                    {
                        await appendBlobWithSAS.AppendBlockAsync(stream);
                    }
                }
                else
                {
                    using (MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(blobText)))
                    {
                        await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await appendBlobWithSAS.AppendBlockAsync(memStream), "Append a block to an append blob should fail with SAS without Add or Write and Object-level perms.");

                        memStream.Seek(0, SeekOrigin.Begin);
                        await appendBlob.AppendBlockAsync(memStream);
                    }
                }

                Assert.AreEqual(blobText, await appendBlob.DownloadTextAsync());

                if (((policy.Permissions & SharedAccessAccountPermissions.Read) == SharedAccessAccountPermissions.Read) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    Assert.AreEqual(blobText, await appendBlobWithSAS.DownloadTextAsync());
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await appendBlobWithSAS.DownloadTextAsync(), "Reading a blob's contents with SAS without Read and Object-level permissions should fail.");
                }

                if (((policy.Permissions & SharedAccessAccountPermissions.Delete) == SharedAccessAccountPermissions.Delete) &&
                    ((policy.ResourceTypes & SharedAccessAccountResourceTypes.Object) == SharedAccessAccountResourceTypes.Object))
                {
                    await appendBlobWithSAS.DeleteAsync();
                }
                else
                {
                    await TestHelper.ExpectedExceptionAsync <StorageException>(async() => await appendBlobWithSAS.DeleteAsync(), "Deleting a blob with SAS without Delete and Object-level perms should fail.");

                    await appendBlob.DeleteAsync();
                }

                Assert.IsFalse(await appendBlob.ExistsAsync());
            }
            finally
            {
                blobClient.GetContainerReference(containerName).DeleteIfExistsAsync().Wait();
            }
        }