public async Task OpenWriteAsyncRejectsETagMismatchFoundBeforeUploadStarts()
        {
            // 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 & Assert
            var ex = await Assert.ThrowsAsync <StorageException>(
                async() =>
            {
                using (var stream = await file.OpenWriteAsync(AccessCondition.GenerateIfNotExistsCondition()))
                {
                    await stream.WriteAsync(new byte[0], 0, 0);
                }
            });

            Assert.Equal(HttpStatusCode.Conflict, (HttpStatusCode)ex.RequestInformation.HttpStatusCode);
        }
        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);
                }
        }
        private static async Task CopyFileWorksAsync(
            CopyAsync copyAsync,
            string srcPrefix,
            CloudBlobCoreFileStorageService srcService,
            string destPrefix,
            CloudBlobCoreFileStorageService destService)
        {
            // Arrange
            var srcFolderName = CoreConstants.Folders.ValidationFolderName;
            var srcFileName   = $"{srcPrefix}/src";
            var srcContent    = "Hello, world.";

            var destFolderName = CoreConstants.Folders.PackagesFolderName;
            var destFileName   = $"{destPrefix}/dest";

            await srcService.SaveFileAsync(
                srcFolderName,
                srcFileName,
                new MemoryStream(Encoding.ASCII.GetBytes(srcContent)),
                overwrite : false);

            // Act
            await copyAsync(srcService, srcFolderName, srcFileName, destService, destFolderName, destFileName);

            // Assert
            using (var destStream = await destService.GetFileAsync(destFolderName, destFileName))
                using (var destReader = new StreamReader(destStream))
                {
                    var destContent = destReader.ReadToEnd();
                    Assert.Equal(srcContent, destContent);
                }
        }
        public async Task DoesNotCopyWhenSourceAndDestinationHaveSameHash()
        {
            // Arrange
            var srcFolderName = CoreConstants.Folders.ValidationFolderName;
            var srcFileName   = $"{_prefixA}/src";
            var srcContent    = "Hello, world.";

            var destFolderName = CoreConstants.Folders.PackagesFolderName;
            var destFileName   = $"{_prefixB}/dest";

            await _targetA.SaveFileAsync(
                srcFolderName,
                srcFileName,
                new MemoryStream(Encoding.ASCII.GetBytes(srcContent)),
                overwrite : false);

            await _targetB.SaveFileAsync(
                destFolderName,
                destFileName,
                new MemoryStream(Encoding.ASCII.GetBytes(srcContent)),
                overwrite : false);

            var originalDestFileReference = await _targetB.GetFileReferenceAsync(destFolderName, destFileName);

            var originalDestETag = originalDestFileReference.ContentId;

            var srcUri = await _targetA.GetFileReadUriAsync(
                srcFolderName,
                srcFileName,
                DateTimeOffset.UtcNow.AddHours(1));

            var destAccessCondition = AccessConditionWrapper.GenerateIfNotExistsCondition();

            // Act
            await _targetB.CopyFileAsync(
                srcUri,
                destFolderName,
                destFileName,
                destAccessCondition);

            // Assert
            var finalDestFileReference = await _targetB.GetFileReferenceAsync(destFolderName, destFileName);

            var finalDestETag = finalDestFileReference.ContentId;

            Assert.Equal(originalDestETag, finalDestETag);
        }
        public async Task ReturnsCurrentETagForIfMatch()
        {
            // Arrange
            var folderName = CoreConstants.Folders.ValidationFolderName;
            var fileName   = _prefixA;
            await _targetA.SaveFileAsync(folderName, fileName, new MemoryStream(new byte[0]));

            var initialReference = await _targetA.GetFileReferenceAsync(folderName, fileName);

            initialReference.OpenRead().Dispose();

            // Act
            var reference = await _targetA.GetFileReferenceAsync(folderName, fileName, initialReference.ContentId);

            // Assert
            Assert.NotNull(reference);
            Assert.Null(reference.OpenRead());
            Assert.Equal(initialReference.ContentId, reference.ContentId);
        }
コード例 #6
0
        public async Task CanReadAndDeleteBlobUsingPrivilegedFileUri()
        {
            // Arrange
            var folderName      = CoreConstants.ValidationFolderName;
            var fileName        = _prefixA;
            var expectedContent = "Hello, world.";

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

            var deleteUri = await _targetA.GetPriviledgedFileUriAsync(
                folderName,
                fileName,
                FileUriPermissions.Read | FileUriPermissions.Delete,
                DateTimeOffset.UtcNow.AddHours(1));

            // Act
            var sasToken         = new StorageCredentials(deleteUri.Query);
            var deleteUriBuilder = new UriBuilder(deleteUri)
            {
                Query = null
            };
            var blob = new CloudBlockBlob(deleteUriBuilder.Uri, sasToken);

            var actualContent = await blob.DownloadTextAsync();

            await blob.DeleteAsync();

            // Assert
            Assert.Equal(expectedContent, actualContent);
            var exists = await _targetA.FileExistsAsync(folderName, fileName);

            Assert.False(exists, "The file should no longer exist.");
        }
コード例 #7
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);
        }
コード例 #8
0
        public async Task DoesNotCopyWhenSourceAndDestinationHaveSameHash()
        {
            // Arrange
            var srcFolderName = CoreConstants.Folders.ValidationFolderName;
            var srcFileName   = $"{_prefixA}/src";
            var srcContent    = "Hello, world.";
            var srcSha512     = "AD0C37C31D69B315F3A81F13C8CDE701094AD91725BA1B0DC3199CA9713661B8280" +
                                "D6EF7E68F133E6211E2E5A9A3150445D76F1708E04521B0EE034F0B0BAF26";

            var destFolderName = CoreConstants.Folders.PackagesFolderName;
            var destFileName   = $"{_prefixB}/dest";

            await _targetA.SaveFileAsync(
                srcFolderName,
                srcFileName,
                new MemoryStream(Encoding.ASCII.GetBytes(srcContent)),
                overwrite : false);

            await _targetA.SetMetadataAsync(srcFolderName, srcFileName, (lazyStream, metadata) =>
            {
                metadata[CoreConstants.Sha512HashAlgorithmId] = srcSha512;
                return(Task.FromResult(true));
            });

            await _targetB.SaveFileAsync(
                destFolderName,
                destFileName,
                new MemoryStream(Encoding.ASCII.GetBytes(srcContent)),
                overwrite : false);

            await _targetB.SetMetadataAsync(destFolderName, destFileName, (lazyStream, metadata) =>
            {
                metadata[CoreConstants.Sha512HashAlgorithmId] = srcSha512;
                return(Task.FromResult(true));
            });

            var originalDestFileReference = await _targetB.GetFileReferenceAsync(destFolderName, destFileName);

            var originalDestETag = originalDestFileReference.ContentId;

            var srcUri = await _targetA.GetFileReadUriAsync(
                srcFolderName,
                srcFileName,
                DateTimeOffset.UtcNow.AddHours(1));

            var destAccessCondition = AccessConditionWrapper.GenerateIfNotExistsCondition();

            // Act
            await _targetB.CopyFileAsync(
                srcUri,
                destFolderName,
                destFileName,
                destAccessCondition);

            // Assert
            var finalDestFileReference = await _targetB.GetFileReferenceAsync(destFolderName, destFileName);

            var finalDestETag = finalDestFileReference.ContentId;

            Assert.Equal(originalDestETag, finalDestETag);
        }
        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);
        }