示例#1
0
        public async Task DownloadBlobText()
        {
            string data = "hello world";

            //setup blob
            string containerName   = Randomize("sample-container");
            string blobName        = Randomize("sample-file");
            var    containerClient = new BlobContainerClient(ConnectionString, containerName);

            try
            {
                containerClient.Create();
                containerClient.GetBlobClient(blobName).Upload(BinaryData.FromString(data));

                #region Snippet:SampleSnippetsBlobMigration_DownloadBlobText
                BlobClient         blobClient     = containerClient.GetBlobClient(blobName);
                BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();

                string downloadedData = downloadResult.Content.ToString();
                #endregion

                Assert.AreEqual(data, downloadedData);
            }
            finally
            {
                await containerClient.DeleteIfExistsAsync();
            }
        }
示例#2
0
        public async Task <string> GetBlob()
        {
            var storageUrl      = new Uri($"https://{Environment.GetEnvironmentVariable("StorageAccount")}.blob.core.windows.net");
            var storageClient   = new BlobServiceClient(storageUrl, new DefaultAzureCredential());
            var containerClient = storageClient.GetBlobContainerClient($"{Environment.GetEnvironmentVariable("BlobContainer")}");

            string     blobName   = "test.txt";
            BlobClient blobClient = containerClient.GetBlobClient(blobName); // Get a reference to a blob

            try
            {
                // read contents from assembly and upload to new file
                var assembly     = Assembly.GetExecutingAssembly();
                var resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(blobName));
                using Stream stream = assembly.GetManifestResourceStream(resourceName);

                await blobClient.UploadAsync(stream, true);
            }
            catch (Exception ex)
            {
                _logger.LogError("The upload of the file to blob failed.", ex);
            }

            var blob = await blobClient.DownloadContentAsync();

            return(blob.Value.Content.ToString());
        }
        public async Task <Response <BlobDownloadResult> > GetBlob(string fileName, BlobContainerClient blobContainerClient)
        {
            BlobClient blobClient = blobContainerClient.GetBlobClient(fileName);
            var        file       = await blobClient.DownloadContentAsync();

            return(file);
        }
    public override async Task <string> DownloadArtifactAsync(Guid deploymentId, string artifactName)
    {
        var blobClient     = new BlobClient(azureStorageArtifactsOptions.ConnectionString, DEPLOYMENT_CONTAINER_NAME, $"{deploymentId}/{artifactName}");
        var artifactExists = await blobClient.ExistsAsync().ConfigureAwait(false);

        if (artifactExists)
        {
            var blob = await blobClient.DownloadContentAsync().ConfigureAwait(false);

            return(blob.Value.Content.ToString());
        }

        return(null);
    }
示例#5
0
        private async Task <ClaimPermissions> DownloadPermissionsAsync(string id)
        {
            BlobClient blob = this.Container.GetBlobClient(id);
            Response <BlobDownloadResult> response = await blob.DownloadContentAsync().ConfigureAwait(false);

            // Note: although BlobDownloadResult supports direct deserialization from JSON, using System.Text.Json
            // (meaning it can work directly with UTF-8 content, avoiding the conversion to UTF-16 we're doing
            // here) we currently depend on the JSON.NET serialization settings mechanism, so we have to use
            // this more inefficient route for now.
            string           permissionsJson = response.Value.Content.ToString();
            ClaimPermissions permissions     = JsonConvert.DeserializeObject <ClaimPermissions>(permissionsJson, this.serializerSettings);

            permissions.ETag = response.Value.Details.ETag.ToString("G");
            return(permissions);
        }
        public async Task CreateBlobClient_UploadFile_ContentMatch()
        {
            //Arrange
            BlobServiceClient   blobServiceClient = _azureStorageResource.CreateBlobServiceClient();
            BlobContainerClient containerClient   = blobServiceClient.GetBlobContainerClient("foo");
            await containerClient.CreateIfNotExistsAsync();

            string inputText = "Hello_AzureStorage";
            var    data      = Encoding.UTF8.GetBytes(inputText);

            //Act
            BlobClient textFile = containerClient.GetBlobClient("test.txt");
            await textFile.UploadAsync(new BinaryData(data));

            //Assert
            Response <BlobDownloadResult> downloaded = await textFile.DownloadContentAsync();

            var downloadedFileContent = Encoding.UTF8.GetString(downloaded.Value.Content.ToArray());

            downloadedFileContent.Should().Be(inputText);
        }
示例#7
0
        public async Task UploadBlobText()
        {
            string data = "hello world";

            BlobContainerClient containerClient = new BlobContainerClient(ConnectionString, Randomize("sample-container"));

            try
            {
                await containerClient.CreateAsync();

                string blobName = Randomize("sample-blob");

                string     localFilePath = this.CreateTempPath();
                FileStream fs            = File.OpenWrite(localFilePath);
                var        bytes         = Encoding.UTF8.GetBytes(data);
                await fs.WriteAsync(bytes, 0, bytes.Length);

                await fs.FlushAsync();

                fs.Close();

                #region Snippet:SampleSnippetsBlobMigration_UploadBlobText
                BlobClient blobClient = containerClient.GetBlobClient(blobName);
                await blobClient.UploadAsync(BinaryData.FromString("hello world"), overwrite : true);

                #endregion

                BinaryData downloadedData = (await blobClient.DownloadContentAsync()).Value.Content;

                Assert.AreEqual(data, downloadedData.ToString());
            }
            finally
            {
                await containerClient.DeleteIfExistsAsync();
            }
        }
示例#8
0
    public async static Task <string> DownloadTextAsync(this BlobClient client)
    {
        var res = await client.DownloadContentAsync();

        return(res.Value.Content.ToString());
    }
        public async Task ClaimCheck()
        {
            await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: false))
            {
                #region Snippet:CreateBlobContainer
#if SNIPPET
                var containerClient = new BlobContainerClient("<storage connection string>", "claim-checks");
#else
                var containerClient = new BlobContainerClient(TestEnvironment.StorageClaimCheckConnectionString, "claim-checks");
#endif
                await containerClient.CreateIfNotExistsAsync();

                #endregion

                try
                {
                    #region Snippet:UploadMessage

                    byte[] body     = ServiceBusTestUtilities.GetRandomBuffer(1000000);
                    string blobName = Guid.NewGuid().ToString();
                    await containerClient.UploadBlobAsync(blobName, new BinaryData(body));

                    var message = new ServiceBusMessage
                    {
                        ApplicationProperties =
                        {
                            ["blob-name"] = blobName
                        }
                    };

                    #endregion

                    #region Snippet:ClaimCheckSendMessage

#if SNIPPET
                    var client = new ServiceBusClient("<service bus connection string>");
#else
                    var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
#endif
                    ServiceBusSender sender = client.CreateSender(scope.QueueName);
                    await sender.SendMessageAsync(message);

                    #endregion

                    #region Snippet:ReceiveClaimCheck

                    ServiceBusReceiver        receiver        = client.CreateReceiver(scope.QueueName);
                    ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveMessageAsync();

                    if (receivedMessage.ApplicationProperties.TryGetValue("blob-name", out object blobNameReceived))
                    {
#if SNIPPET
                        var blobClient = new BlobClient("<storage connection string>", "claim-checks", (string)blobNameReceived);
#else
                        var blobClient = new BlobClient(
                            TestEnvironment.StorageClaimCheckConnectionString,
                            "claim-checks",
                            (string)blobNameReceived);
#endif
                        BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();

                        BinaryData messageBody = downloadResult.Content;

                        // Once we determine that we are done with the message, we complete it and delete the corresponding blob.
                        await receiver.CompleteMessageAsync(receivedMessage);

                        await blobClient.DeleteAsync();

#if !SNIPPET
                        Assert.AreEqual(body, messageBody.ToArray());
#endif
                    }
                    #endregion
                }
                finally
                {
                    await containerClient.DeleteAsync();
                }
            }
        }