public async Task OpenReadAsyncReturnsReadableStreamWhenBlobExistsAndPopulatesProperties()
        {
            // Arrange
            var folderName      = CoreConstants.Folders.ValidationFolderName;
            var fileName        = _prefixA;
            var expectedContent = "Hello, world.";

            await _targetA.SaveFileAsync(
                folderName,
                fileName,
                new MemoryStream(Encoding.ASCII.GetBytes(expectedContent)),
                overwrite : false);

            var container = _clientA.GetContainerReference(folderName);
            var file      = container.GetBlobReference(fileName);

            // Act
            using (var stream = await file.OpenReadAsync(accessCondition: null))
                using (var streamReader = new StreamReader(stream))
                {
                    var actualContent = await streamReader.ReadToEndAsync();

                    // Assert
                    Assert.Equal(expectedContent, actualContent);
                    Assert.Equal(expectedContent.Length, file.Properties.Length);
                    Assert.NotNull(file.ETag);
                }
        }
Exemplo n.º 2
0
        public async Task AllowsDefaultRequestOptionsToBeSet()
        {
            // Arrange
            var folderName = CoreConstants.Folders.ValidationFolderName;
            var fileName   = _prefixA;
            await _targetA.SaveFileAsync(
                folderName,
                fileName,
                new MemoryStream(new byte[1024 * 1024]),
                overwrite : false);

            var client = new CloudBlobClientWrapper(
                _fixture.ConnectionStringA,
                new BlobRequestOptions
            {
                MaximumExecutionTime = TimeSpan.FromMilliseconds(1),
            });
            var container   = client.GetContainerReference(folderName);
            var file        = container.GetBlobReference(fileName);
            var destination = new MemoryStream();

            // Act & Assert
            // This should throw due to timeout.
            var ex = await Assert.ThrowsAsync <StorageException>(() => file.DownloadToStreamAsync(destination));

            Assert.Contains("timeout", ex.Message);
        }
        public async Task OpenWriteAsyncReturnsWritableStream()
        {
            // Arrange
            var    folderName      = CoreConstants.Folders.ValidationFolderName;
            var    fileName        = _prefixA;
            var    expectedContent = "Hello, world.";
            var    bytes           = Encoding.UTF8.GetBytes(expectedContent);
            string expectedContentMD5;

            using (var md5 = MD5.Create())
            {
                expectedContentMD5 = Convert.ToBase64String(md5.ComputeHash(bytes));
            }

            var container = _clientA.GetContainerReference(folderName);
            var file      = container.GetBlobReference(fileName);

            // Act
            using (var stream = await file.OpenWriteAsync(accessCondition: null))
            {
                await stream.WriteAsync(bytes, 0, bytes.Length);
            }

            // Assert
            // Reinitialize the blob to verify the metadata is fresh.
            file = container.GetBlobReference(fileName);
            using (var memoryStream = new MemoryStream())
            {
                await file.DownloadToStreamAsync(memoryStream);

                var actualContent = Encoding.ASCII.GetString(memoryStream.ToArray());
                Assert.Equal(expectedContent, actualContent);

                Assert.NotNull(file.ETag);
                Assert.NotEmpty(file.ETag);
                Assert.Equal(expectedContentMD5, file.Properties.ContentMD5);
            }
        }
Exemplo n.º 4
0
        public async Task EnumeratesBlobs()
        {
            // Arrange
            var folderName = CoreConstants.Folders.ValidationFolderName;
            var blobAName  = $"{_prefixA}/a.txt";
            var blobBName  = $"{_prefixA}/b.txt";
            var blobCName  = $"{_prefixA}/c.txt";
            await _targetA.SaveFileAsync(folderName, blobAName, new MemoryStream(Encoding.UTF8.GetBytes("A")));

            await _targetA.SaveFileAsync(folderName, blobCName, new MemoryStream(Encoding.UTF8.GetBytes("C")));

            await _targetA.SaveFileAsync(folderName, blobBName, new MemoryStream(Encoding.UTF8.GetBytes("B")));

            var container = _clientA.GetContainerReference(folderName);

            // Act
            var segmentA = await container.ListBlobsSegmentedAsync(
                _prefixA,
                useFlatBlobListing : true,
                blobListingDetails : BlobListingDetails.None,
                maxResults : 2,
                blobContinuationToken : null,
                options : null,
                operationContext : null,
                cancellationToken : CancellationToken.None);

            var segmentB = await container.ListBlobsSegmentedAsync(
                _prefixA,
                useFlatBlobListing : true,
                blobListingDetails : BlobListingDetails.None,
                maxResults : 2,
                blobContinuationToken : segmentA.ContinuationToken,
                options : null,
                operationContext : null,
                cancellationToken : CancellationToken.None);

            // Assert
            Assert.Equal(2, segmentA.Results.Count);
            Assert.Equal(blobAName, segmentA.Results[0].Name);
            Assert.Equal(blobBName, segmentA.Results[1].Name);
            Assert.Equal(blobCName, Assert.Single(segmentB.Results).Name);
        }