Пример #1
0
        public async Task AppendBlockFromUriAsync_Range()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            await test.Container.SetAccessPolicyAsync(PublicAccessType.BlobContainer);

            var data = GetRandomBuffer(4 * Constants.KB);

            using (var stream = new MemoryStream(data))
            {
                AppendBlobClient sourceBlob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
                await sourceBlob.CreateAsync();

                await sourceBlob.AppendBlockAsync(stream);

                AppendBlobClient destBlob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
                await destBlob.CreateAsync();

                // Act
                await destBlob.AppendBlockFromUriAsync(sourceBlob.Uri, new HttpRange(2 * Constants.KB, 2 * Constants.KB));

                // Assert
                Response <BlobDownloadInfo> result = await destBlob.DownloadAsync(new HttpRange(0, 2 * Constants.KB));

                var dataResult = new MemoryStream();
                await result.Value.Content.CopyToAsync(dataResult);

                Assert.AreEqual(2 * Constants.KB, dataResult.Length);
                TestHelper.AssertSequenceEqual(data.Skip(2 * Constants.KB).Take(2 * Constants.KB), dataResult.ToArray());
            }
        }
Пример #2
0
        public async Task AppendBlockAsync()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            var blobName          = GetNewBlobName();
            AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(blobName));
            await blob.CreateAsync();

            const int blobSize = Constants.KB;
            var       data     = GetRandomBuffer(blobSize);

            // Act
            using (var stream = new MemoryStream(data))
            {
                await blob.AppendBlockAsync(stream);
            }

            // Assert
            Response <BlobDownloadInfo> result = await blob.DownloadAsync(new HttpRange(0, data.Length));

            var dataResult = new MemoryStream();
            await result.Value.Content.CopyToAsync(dataResult);

            Assert.AreEqual(data.Length, dataResult.Length);
            TestHelper.AssertSequenceEqual(data, dataResult.ToArray());
        }
Пример #3
0
        public async Task AppendBlockAsync_WithUnreliableConnection()
        {
            const int blobSize = 1 * Constants.MB;

            await using DisposingContainer test = await GetTestContainerAsync();

            BlobContainerClient containerFaulty = InstrumentClient(
                new BlobContainerClient(
                    test.Container.Uri,
                    new StorageSharedKeyCredential(
                        TestConfigDefault.AccountName,
                        TestConfigDefault.AccountKey),
                    GetFaultyBlobConnectionOptions()));

            // Arrange
            var blobName = GetNewBlobName();
            AppendBlobClient blobFaulty = InstrumentClient(containerFaulty.GetAppendBlobClient(blobName));
            AppendBlobClient blob       = InstrumentClient(test.Container.GetAppendBlobClient(blobName));

            await blob.CreateAsync();

            var data            = GetRandomBuffer(blobSize);
            var progressList    = new List <long>();
            var progressHandler = new Progress <long>(progress => { progressList.Add(progress); /*logger.LogTrace("Progress: {progress}", progress.BytesTransferred);*/ });
            var timesFaulted    = 0;

            // Act
            using (var stream = new FaultyStream(
                       new MemoryStream(data),
                       256 * Constants.KB,
                       1,
                       new IOException("Simulated stream fault"),
                       () => timesFaulted++))
            {
                await blobFaulty.AppendBlockAsync(stream, progressHandler : progressHandler);
                await WaitForProgressAsync(progressList, data.LongLength);

                Assert.IsTrue(progressList.Count > 1, "Too few progress received");
                // Changing from Assert.AreEqual because these don't always update fast enough
                Assert.GreaterOrEqual(data.LongLength, progressList.Last(), "Final progress has unexpected value");
            }

            // Assert
            Response <BlobDownloadInfo> downloadResponse = await blob.DownloadAsync();

            var actual = new MemoryStream();
            await downloadResponse.Value.Content.CopyToAsync(actual);

            Assert.AreEqual(data.Length, actual.Length);
            TestHelper.AssertSequenceEqual(data, actual.ToArray());
            Assert.AreNotEqual(0, timesFaulted);
        }
Пример #4
0
        public async Task AppendBlobSample()
        {
            // Instantiate a new BlobServiceClient using a connection string.
            BlobServiceClient blobServiceClient = new BlobServiceClient(TestConfigurations.DefaultTargetTenant.ConnectionString);

            // Instantiate a new BlobContainerClient
            BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient("mycontainer4");

            try
            {
                // Create new Container in the Service
                await blobContainerClient.CreateAsync();

                // Instantiate a new PageBlobClient
                AppendBlobClient appendBlobClient = blobContainerClient.GetAppendBlobClient("appendblob");

                // Create PageBlob in the Service
                await appendBlobClient.CreateAsync();

                // Append content to AppendBlob
                using (FileStream fileStream = File.OpenRead("Samples/SampleSource.txt"))
                {
                    await appendBlobClient.AppendBlockAsync(fileStream);
                }

                // Download PageBlob
                using (FileStream fileStream = File.Create("AppendDestination.txt"))
                {
                    Response <BlobDownloadInfo> downloadResponse = await appendBlobClient.DownloadAsync();

                    await downloadResponse.Value.Content.CopyToAsync(fileStream);
                }

                // Delete PageBlob in the Service
                await appendBlobClient.DeleteAsync();
            }
            finally
            {
                // Delete Container in the Service
                await blobContainerClient.DeleteAsync();
            }
        }