public static async Task UploadTextAsync(this AppendBlobClient appendBlobClient, string text, CancellationToken cancellationToken = default)
        {
            using Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(text));
            await appendBlobClient.CreateIfNotExistsAsync(cancellationToken : cancellationToken);

            await appendBlobClient.AppendBlockAsync(stream, cancellationToken : cancellationToken).ConfigureAwait(false);
        }
Пример #2
0
        private async Task <AppendBlobClient> CreateBlob(BlobContainerClient containerClient)
        {
            AppendBlobClient appendBlobClient = containerClient.GetAppendBlobClient(_fileName);
            await appendBlobClient.CreateIfNotExistsAsync();

            return(appendBlobClient);
        }
Пример #3
0
        public async Task CreateIfNotExistsAsync_Error()
        {
            await using DisposingContainer test = await GetTestContainerAsync();

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

            // Act
            await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>(
                blob.CreateIfNotExistsAsync(),
                actualException => Assert.AreEqual("InvalidUri", actualException.ErrorCode)
                );
        }
Пример #4
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);
        }
        public async Task <AppendBlobClient> GetBlobReferenceAsync(BlobServiceClient blobServiceClient, string blobContainerName, string blobName, bool bypassBlobCreationValidation)
        {
            var blobContainer = blobServiceClient.GetBlobContainerClient(blobContainerName);

            await CreateBlobContainerIfNotExistsAsync(blobContainer, bypassBlobCreationValidation).ConfigureAwait(false);

            AppendBlobClient newAppendBlobClient = null;

            try
            {
                newAppendBlobClient = blobContainer.GetAppendBlobClient(blobName);

                //  TODO-VPL:  CreateOrReplaceAsync does not exist in the new SDK
                //  TODO-VPL:  AccessCondition is nowhere to be seen...  here is the original line:
                //newAppendBlobClient.CreateOrReplaceAsync(AccessCondition.GenerateIfNotExistsCondition(), null, null).GetAwaiter().GetResult();
                await newAppendBlobClient.CreateIfNotExistsAsync();
            }
            catch (RequestFailedException ex) when(ex.Status == (int)HttpStatusCode.Conflict && ex.ErrorCode == "BlobAlreadyExists")
            {
                //StorageException (http 409 conflict, error code BlobAlreadyExists) is thrown due to the AccessCondition. The append blob already exists.
                //No problem this is expected
            }
            catch (Exception ex)
            {
                Debugging.SelfLog.WriteLine($"Failed to create blob: {ex}");
                throw;
            }

            //  TODO-VPL:  This is done differently in the new SDK ; we need to do a get properties and they return the properties, i.e. done elsewhere
            //if (newAppendBlobClient != null)
            //{
            //    //this is the first time the code gets its hands on this blob reference, get the blob properties from azure.
            //    //used later on to know when to roll over the file if the 50.000 max blocks is getting close.
            //    await newAppendBlobClient.FetchAttributesAsync().ConfigureAwait(false);
            //}

            return(newAppendBlobClient);
        }