public async Task UploadAsync_OverwritesDeliberately_Stream()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Upload one blob
            var        name = GetNewBlobName();
            BlobClient blob = InstrumentClient(test.Container.GetBlobClient(name));

            using var stream = new MemoryStream(GetRandomBuffer(Constants.KB));
            await blob.UploadAsync(stream);

            // Overwriting works if allowed
            using var stream2 = new MemoryStream(GetRandomBuffer(Constants.KB));
            await blob.UploadAsync(stream2, overwrite : true);
        }
        public async Task Ctor_AzureSasCredential_VerifyNoSasInUri()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            string sas = GetAccountSasCredentials().SasToken;
            Uri    uri = test.Container.GetParentBlobServiceClient().Uri;

            uri = new Uri(uri.ToString() + "?" + sas);

            // Act
            TestHelper.AssertExpectedException <ArgumentException>(
                () => new BlobServiceClient(uri, new AzureSasCredential(sas)),
                e => e.Message.Contains($"You cannot use {nameof(AzureSasCredential)} when the resource URI also contains a Shared Access Signature"));
        }
        public async Task Ctor_AzureSasCredential()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            string sas = GetAccountSasCredentials().SasToken;
            Uri    uri = test.Container.GetParentBlobServiceClient().Uri;

            // Act
            var sasClient = InstrumentClient(new BlobServiceClient(uri, new AzureSasCredential(sas), GetOptions()));
            BlobServiceProperties properties = await sasClient.GetPropertiesAsync();

            // Assert
            Assert.IsNotNull(properties);
        }
        public async Task UploadAsync_Stream_UploadsBlock()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            BlobClient blob = InstrumentClient(test.Container.GetBlobClient(GetNewBlobName()));
            var        data = GetRandomBuffer(Constants.KB);

            using (var stream = new MemoryStream(data))
            {
                await blob.UploadAsync(stream);
            }

            Response <BlobProperties> properties = await blob.GetPropertiesAsync();

            Assert.AreEqual(BlobType.Block, properties.Value.BlobType);
        }
Exemple #5
0
        public async Task UploadAsync_DoesNotOverwrite_Stream()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Upload one blob
            var        name = GetNewBlobName();
            BlobClient blob = InstrumentClient(test.Container.GetBlobClient(name));

            using var stream = new MemoryStream(GetRandomBuffer(Constants.KB));
            await blob.UploadAsync(stream);

            // Overwriting fails
            using var stream2 = new MemoryStream(GetRandomBuffer(Constants.KB));
            Assert.ThrowsAsync <RequestFailedException>(
                async() => await blob.UploadAsync(stream2, overwrite: false));
        }
Exemple #6
0
        public async Task Batch_SasUri_NotOwner()
        {
            // Create a container using SAS for Auth
            string containerName = GetNewContainerName();

            await using DisposingContainer test = await GetTestContainerAsync();

            await using TestScenario scenario = Scenario(GetServiceClient_BlobServiceSas_Container(containerName));
            Uri[] blobs = await scenario.CreateBlobUrisAsync(test.Container, 2);

            BlobBatchClient        client = scenario.GetBlobBatchClient();
            RequestFailedException ex     = Assert.ThrowsAsync <RequestFailedException>(
                async() => await client.DeleteBlobsAsync(blobs));

            Assert.AreEqual(403, ex.Status);
        }
Exemple #7
0
        public async Task AppendBlockAsync_Error()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
            var data = GetRandomBuffer(Constants.KB);

            // Act
            using (var stream = new MemoryStream(data))
            {
                await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                    blob.AppendBlockAsync(stream),
                    e => Assert.AreEqual("BlobNotFound", e.ErrorCode.Split('\n')[0]));
            }
        }
Exemple #8
0
        public async Task AppendBlockAsync_NullStream_Error()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));

            // Act
            using (var stream = (MemoryStream)null)
            {
                // Check if the correct param name that is causing the error is being returned
                await TestHelper.AssertExpectedExceptionAsync <ArgumentNullException>(
                    blob.AppendBlockAsync(content: stream),
                    e => Assert.AreEqual("body", e.ParamName));
            }
        }
Exemple #9
0
        private async Task <TestExceptionPolicy> PerformSecondaryStorageTest(int numberOfReadFailuresToSimulate, bool retryOn404 = false)
        {
            BlobServiceClient service = GetServiceClient_SecondaryAccount_ReadEnabledOnRetry(
                numberOfReadFailuresToSimulate,
                out TestExceptionPolicy testExceptionPolicy,
                retryOn404);

            await using DisposingContainer test = await GetTestContainerAsync(service : service);

            IList <BlobContainerItem> containers = await EnsurePropagatedAsync(
                async() => await service.GetBlobContainersAsync().ToListAsync(),
                containers => containers.Count > 0);

            Assert.IsTrue(containers.Count >= 1);
            return(testExceptionPolicy);
        }
Exemple #10
0
        public async Task FindBlobsByTagAsync()
        {
            // Arrange
            BlobServiceClient service = GetServiceClient_SharedKey();

            await using DisposingContainer test = await GetTestContainerAsync();

            string                      blobName   = GetNewBlobName();
            AppendBlobClient            appendBlob = InstrumentClient(test.Container.GetAppendBlobClient(blobName));
            string                      tagKey     = "myTagKey";
            string                      tagValue   = "myTagValue";
            Dictionary <string, string> tags       = new Dictionary <string, string>
            {
                { tagKey, tagValue }
            };
            AppendBlobCreateOptions options = new AppendBlobCreateOptions
            {
                Tags = tags
            };
            await appendBlob.CreateAsync(options);

            string expression = $"\"{tagKey}\"='{tagValue}'";

            // It takes a few seconds for Filter Blobs to pick up new changes
            await Delay(2000);

            // Act
            List <TaggedBlobItem> blobs = new List <TaggedBlobItem>();

            await foreach (Page <TaggedBlobItem> page in service.FindBlobsByTagsAsync(expression).AsPages())
            {
                blobs.AddRange(page.Values);
            }

            // Assert
            TaggedBlobItem filterBlob = blobs.Where(r => r.BlobName == blobName).FirstOrDefault();

            if (_serviceVersion >= BlobClientOptions.ServiceVersion.V2020_04_08)
            {
                Assert.AreEqual(1, filterBlob.Tags.Count);
                Assert.AreEqual("myTagValue", filterBlob.Tags["myTagKey"]);
            }
            else
            {
                Assert.IsNotNull(filterBlob);
            }
        }
Exemple #11
0
        public async Task CreateAsync_AccessConditions()
        {
            var garbageLeaseId = GetGarbageLeaseId();

            AccessConditionParameters[] data = new[]
            {
                new AccessConditionParameters(),
                new AccessConditionParameters {
                    IfModifiedSince = OldDate
                },
                new AccessConditionParameters {
                    IfUnmodifiedSince = NewDate
                },
                new AccessConditionParameters {
                    Match = ReceivedETag
                },
                new AccessConditionParameters {
                    NoneMatch = GarbageETag
                },
                new AccessConditionParameters {
                    LeaseId = ReceivedLeaseId
                }
            };
            foreach (AccessConditionParameters parameters in data)
            {
                await using DisposingContainer test = await GetTestContainerAsync();

                // Arrange
                AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
                // AppendBlob needs to exists for us to test CreateAsync() with access conditions
                await blob.CreateAsync();

                parameters.Match = await SetupBlobMatchCondition(blob, parameters.Match);

                parameters.LeaseId = await SetupBlobLeaseCondition(blob, parameters.LeaseId, garbageLeaseId);

                AppendBlobRequestConditions accessConditions = BuildDestinationAccessConditions(
                    parameters: parameters,
                    lease: true);

                // Act
                Response <BlobContentInfo> response = await blob.CreateAsync(conditions : accessConditions);

                // Assert
                Assert.IsNotNull(response.GetRawResponse().Headers.RequestId);
            }
        }
Exemple #12
0
        public async Task CreateAsync_Metadata()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            AppendBlobClient             blob     = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
            IDictionary <string, string> metadata = BuildMetadata();

            // Act
            await blob.CreateAsync(
                metadata : metadata);

            // Assert
            Response <BlobProperties> response = await blob.GetPropertiesAsync();

            AssertMetadataEquality(metadata, response.Value.Metadata);
        }
        public IDisposable GetNewContainer(
            out BlobContainerClient container,
            BlobServiceClient service             = default,
            string containerName                  = default,
            IDictionary <string, string> metadata = default,
            PublicAccessType?publicAccessType     = default)
        {
            containerName = containerName ?? this.GetNewContainerName();
            service       = service ?? this.GetServiceClient_SharedKey();
            var result = new DisposingContainer(
                this.InstrumentClient(service.GetBlobContainerClient(containerName)),
                metadata ?? new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase),
                publicAccessType ?? PublicAccessType.Container);

            container = this.InstrumentClient(result.ContainerClient);
            return(result);
        }
Exemple #14
0
        public async Task ListContainersSegmentAsync()
        {
            // Arrange
            BlobServiceClient service = GetServiceClient_SharedKey();

            // Ensure at least one container
            await using DisposingContainer test = await GetTestContainerAsync(service : service);

            // Act
            IList <BlobContainerItem> containers = await service.GetBlobContainersAsync().ToListAsync();

            // Assert
            Assert.IsTrue(containers.Count() >= 1);
            var accountName = new BlobUriBuilder(service.Uri).AccountName;

            TestHelper.AssertCacheableProperty(accountName, () => service.AccountName);
        }
Exemple #15
0
        public static IDisposable GetNewContainer(
            out BlobContainerClient container,
            BlobServiceClient service             = default,
            string containerName                  = default,
            IDictionary <string, string> metadata = default,
            PublicAccessType?publicAccessType     = default)
        {
            containerName = containerName ?? TestHelper.GetNewContainerName();

            service = service ?? GetServiceClient_SharedKey();

            var result = new DisposingContainer(service.GetBlobContainerClient(containerName), metadata ?? new Dictionary <string, string>(), publicAccessType ?? PublicAccessType.Container);

            container = result.ContainerClient;

            return(result);
        }
        private async Task UploadStreamAndVerify(
            long size,
            StorageTransferOptions transferOptions)
        {
            using Stream stream = await CreateLimitedMemoryStream(size);

            await using DisposingContainer test = await GetTestContainerAsync();

            var        name       = GetNewBlobName();
            BlobClient blob       = InstrumentClient(test.Container.GetBlobClient(name));
            var        credential = new StorageSharedKeyCredential(TestConfigDefault.AccountName, TestConfigDefault.AccountKey);

            blob = InstrumentClient(new BlobClient(blob.Uri, credential, GetOptions(true)));

            await blob.StagedUploadAsync(
                content : stream,
                blobHttpHeaders : default,
Exemple #17
0
        public async Task CreateAsync_CPK()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            var blobName             = GetNewBlobName();
            AppendBlobClient    blob = InstrumentClient(test.Container.GetAppendBlobClient(blobName));
            CustomerProvidedKey customerProvidedKey = GetCustomerProvidedKey();

            blob = InstrumentClient(blob.WithCustomerProvidedKey(customerProvidedKey));

            // Act
            Response <BlobContentInfo> response = await blob.CreateAsync();

            // Assert
            Assert.AreEqual(customerProvidedKey.EncryptionKeyHash, response.Value.EncryptionKeySha256);
        }
Exemple #18
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);*/ });

            // Act
            using (var stream = new FaultyStream(new MemoryStream(data), 256 * Constants.KB, 1, new IOException("Simulated stream fault")))
            {
                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());
        }
Exemple #19
0
        public async Task ListContainersSegmentAsync_MaxResults()
        {
            BlobServiceClient service = GetServiceClient_SharedKey();

            // Ensure at least one container
            await using DisposingContainer test = await GetTestContainerAsync(service : service);

            await using DisposingContainer container = await GetTestContainerAsync(service : service);

            // Act
            Page <BlobContainerItem> page = await
                                            service.GetBlobContainersAsync()
                                            .AsPages(pageSizeHint: 1)
                                            .FirstAsync();

            // Assert
            Assert.AreEqual(1, page.Values.Count());
        }
Exemple #20
0
        public async Task Batch_AzureSasCredential()
        {
            // Create a container using SAS for Auth
            await using DisposingContainer test = await GetTestContainerAsync();

            var serviceClient    = GetServiceClient_SharedKey();
            var sas              = GetAccountSasCredentials().SasToken;
            var sasServiceClient = InstrumentClient(new BlobServiceClient(serviceClient.Uri, new AzureSasCredential(sas), GetOptions()));

            await using TestScenario scenario = Scenario(sasServiceClient);
            Uri[] blobs = await scenario.CreateBlobUrisAsync(test.Container, 2);

            BlobBatchClient client = scenario.GetBlobBatchClient();

            Response[] responses = await client.DeleteBlobsAsync(blobs);

            scenario.AssertStatus(202, responses);
        }
Exemple #21
0
        public async Task Ctor_AzureSasCredential()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            string sas    = GetContainerSas(test.Container.Name, BlobContainerSasPermissions.All).ToString();
            var    client = test.Container.GetBlobClient(GetNewBlobName());
            await client.UploadAsync(new MemoryStream());

            Uri blobUri = client.Uri;

            // Act
            var            sasClient      = InstrumentClient(new BlobClient(blobUri, new AzureSasCredential(sas), GetOptions()));
            BlobProperties blobProperties = await sasClient.GetPropertiesAsync();

            // Assert
            Assert.IsNotNull(blobProperties);
        }
        public async Task AccountPermissionsRawPermissions_InvalidPermission()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            AccountSasBuilder accountSasBuilder = new AccountSasBuilder
            {
                StartsOn      = Recording.UtcNow.AddHours(-1),
                ExpiresOn     = Recording.UtcNow.AddHours(1),
                Services      = AccountSasServices.Blobs,
                ResourceTypes = AccountSasResourceTypes.All
            };

            // Act
            TestHelper.AssertExpectedException(
                () => accountSasBuilder.SetPermissions("werteyfg"),
                new ArgumentException("e is not a valid SAS permission"));
        }
        public async Task UploadAsync_Stream_NullStreamFail()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            var        name = GetNewBlobName();
            BlobClient blob = InstrumentClient(test.Container.GetBlobClient(name));
            var        data = GetRandomBuffer(Constants.KB);

            // Act
            using (var stream = (Stream)null)
            {
                // Check if the correct param name that is causing the error is being returned
                await TestHelper.AssertExpectedExceptionAsync <ArgumentNullException>(
                    blob.UploadAsync(stream),
                    e => Assert.AreEqual("body", e.ParamName));
            }
        }
Exemple #24
0
        public async Task ListContainersSegmentAsync_Prefix()
        {
            BlobServiceClient service = GetServiceClient_SharedKey();
            var prefix        = "aaa";
            var containerName = prefix + GetNewContainerName();

            // Ensure at least one container
            await using DisposingContainer test = await GetTestContainerAsync(service : service, containerName : containerName);

            AsyncPageable <BlobContainerItem> containers = service.GetBlobContainersAsync(prefix: prefix);
            IList <BlobContainerItem>         items      = await containers.ToListAsync();

            // Assert
            Assert.AreNotEqual(0, items.Count());
            Assert.IsTrue(items.All(c => c.Name.StartsWith(prefix)));
            Assert.IsNotNull(items.Single(c => c.Name == containerName));
            Assert.IsTrue(items.All(c => c.Properties.Metadata == null));
        }
        public async Task QueryAsync_ParquetOutputError()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            BlockBlobClient blockBlobClient = InstrumentClient(test.Container.GetBlockBlobClient(GetNewBlobName()));

            // Act
            string           query   = @"select * from blobstorage where id < 1;";
            BlobQueryOptions options = new BlobQueryOptions
            {
                OutputTextConfiguration = new BlobQueryParquetTextOptions()
            };

            await TestHelper.AssertExpectedExceptionAsync <ArgumentException>(
                blockBlobClient.QueryAsync(
                    query, options),
                e => Assert.AreEqual($"{nameof(BlobQueryParquetTextOptions)} can only be used for input serialization.", e.Message));
        }
        public async Task QueryAsync_Min()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            BlockBlobClient blockBlobClient = InstrumentClient(test.Container.GetBlockBlobClient(GetNewBlobName()));
            Stream          stream          = CreateDataStream(Constants.KB);
            await blockBlobClient.UploadAsync(stream);

            // Act
            string query = @"SELECT _2 from BlobStorage WHERE _1 > 250;";
            Response <BlobDownloadInfo> response = await blockBlobClient.QueryAsync(query);

            using StreamReader streamReader = new StreamReader(response.Value.Content);
            string s = await streamReader.ReadToEndAsync();

            // Assert
            Assert.AreEqual("400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n400\n", s);
        }
        public async Task ContainerPermissionsRawPermissions_Invalid()
        {
            // Arrange
            await using DisposingContainer test = await GetTestContainerAsync();

            BlobSasBuilder blobSasBuilder = new BlobSasBuilder
            {
                StartsOn          = Recording.UtcNow.AddHours(-1),
                ExpiresOn         = Recording.UtcNow.AddHours(1),
                BlobContainerName = test.Container.Name
            };

            // Act
            TestHelper.AssertExpectedException(
                () => blobSasBuilder.SetPermissions(
                    rawPermissions: "ptsdfsd",
                    normalize: true),
                new ArgumentException("s is not a valid SAS permission"));
        }
Exemple #28
0
        public async Task ListContainersSegmentAsync_Metadata()
        {
            BlobServiceClient service = GetServiceClient_SharedKey();

            // Ensure at least one container
            await using DisposingContainer test = await GetTestContainerAsync();

            // Arrange
            IDictionary <string, string> metadata = BuildMetadata();
            await test.Container.SetMetadataAsync(metadata);

            // Act
            IList <BlobContainerItem> containers = await service.GetBlobContainersAsync(BlobContainerTraits.Metadata).ToListAsync();

            // Assert
            AssertDictionaryEquality(
                metadata,
                containers.Where(c => c.Name == test.Container.Name).FirstOrDefault().Properties.Metadata);
        }
Exemple #29
0
        public async Task CreateAsync_AccessConditionsFail()
        {
            var garbageLeaseId = GetGarbageLeaseId();

            AccessConditionParameters[] data = new[]
            {
                new AccessConditionParameters {
                    IfModifiedSince = NewDate
                },
                new AccessConditionParameters {
                    IfUnmodifiedSince = OldDate
                },
                new AccessConditionParameters {
                    Match = GarbageETag
                },
                new AccessConditionParameters {
                    NoneMatch = ReceivedETag
                },
                new AccessConditionParameters {
                    LeaseId = garbageLeaseId
                }
            };
            foreach (AccessConditionParameters parameters in data)
            {
                await using DisposingContainer test = await GetTestContainerAsync();

                // Arrange
                AppendBlobClient blob = InstrumentClient(test.Container.GetAppendBlobClient(GetNewBlobName()));
                // AppendBlob needs to exists for us to test CreateAsync() with access conditions
                await blob.CreateAsync();

                parameters.NoneMatch = await SetupBlobMatchCondition(blob, parameters.NoneMatch);

                AppendBlobRequestConditions accessConditions = BuildDestinationAccessConditions(
                    parameters: parameters,
                    lease: true);

                // Act
                await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                    blob.CreateAsync(conditions: accessConditions),
                    e => { });
            }
        }
Exemple #30
0
        public async Task CreateIfNotExistsAsync()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

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

            // Act
            Response <BlobContentInfo> response = await blob.CreateIfNotExistsAsync();

            // Assert
            Assert.IsNotNull(response.GetRawResponse().Headers.RequestId);

            IList <BlobItem> blobs = await test.Container.GetBlobsAsync().ToListAsync();

            Assert.AreEqual(1, blobs.Count);
            Assert.AreEqual(blobName, blobs.First().Name);
        }