Esempio n. 1
0
        private static async Task <Response <BlobContentInfo> > WriteStreamToTestDataFolder(string filepath, Stream fileStream)
        {
            string dataPath = GetDataOutputBlobPath() + filepath;

            if (!Directory.Exists(Path.GetDirectoryName(dataPath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(dataPath));
            }

            int filesize;

            using (Stream streamToWriteTo = File.Open(dataPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                await fileStream.CopyToAsync(streamToWriteTo);

                streamToWriteTo.Flush();
                filesize = (int)streamToWriteTo.Length;
            }

            BlobContentInfo mockedBlobInfo = BlobsModelFactory.BlobContentInfo(new ETag("ETagSuccess"), DateTime.Now, new byte[1], DateTime.Now.ToUniversalTime().ToString(), "encryptionKeySha256", "encryptionScope", 1);
            Mock <Response <BlobContentInfo> > mockResponse = new Mock <Response <BlobContentInfo> >();

            mockResponse.SetupGet(r => r.Value).Returns(mockedBlobInfo);

            Mock <Response> responseMock = new Mock <Response>();

            responseMock.SetupGet(r => r.Status).Returns((int)HttpStatusCode.Created);
            mockResponse.Setup(r => r.GetRawResponse()).Returns(responseMock.Object);

            return(mockResponse.Object);
        }
Esempio n. 2
0
        public async Task <IActionResult> Create(AppartmentViewModel appartment)
        {
            if (appartment.Photo != null)
            {
                BlobContainerClient containerClient = _blobService.GetBlobContainerClient("home");
                Stream          stream      = appartment.Photo.OpenReadStream();
                string          fileName    = $"{Guid.NewGuid().ToString()}{appartment.Photo.FileName}";
                BlobContentInfo blobContent = await containerClient.UploadBlobAsync(fileName, stream);

                BlobClient blobClient = containerClient.GetBlobClient(fileName);
                string     url        = blobClient.Uri.AbsoluteUri;
                Appartment newAppart  = new Appartment();
                newAppart.Title       = appartment.Title;
                newAppart.Description = appartment.Description;
                newAppart.Owner       = appartment.Owner;
                newAppart.Price       = appartment.Price;
                newAppart.Photo       = fileName;
                newAppart.PhotoPath   = url;
                _context.Add(newAppart);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(appartment));
        }
Esempio n. 3
0
        // Set the etag on the specified blob
        private string TouchBlob(string containerName, string blobName)
        {
            string          content         = Guid.NewGuid().ToString();
            var             blobClient      = _blobContainer.GetBlobClient(blobName);
            BlobContentInfo blobContentInfo = blobClient.Upload(new MemoryStream(Encoding.UTF8.GetBytes(content)), overwrite: true);

            return(blobContentInfo.ETag.ToString());
        }
Esempio n. 4
0
 internal static MappingContent BuildMapping(Uri blobUri, BlobContentInfo blobContentInfo, Dictionary <string, string> metadata, Stream contents)
 {
     return(BuildMapping(blobUri,
                         metadata,
                         blobContentInfo.LastModified.LocalDateTime,
                         contents.Length,
                         contents));
 }
        public async Task <Response <string> > Add(string containerName, byte[] file, string extension)
        {
            string nameBlob = Guid.NewGuid().ToString() + "." + extension;

            BlobContainerClient containerClient = await ContainerClient(containerName);

            BlobClient blobClient = containerClient.GetBlobClient(nameBlob);

            using (MemoryStream memoryStream = new MemoryStream(file, false))
            {
                BlobContentInfo blobContentInfo = await blobClient.UploadAsync(memoryStream);
            }

            return(new Response <string>(data: nameBlob));
        }
Esempio n. 6
0
        /// <summary>
        /// Method to upload a file to Blob storage
        /// </summary>
        /// <param name="connectionString">Connection String details for Azure Blob Storage</param>
        /// <param name="containerName">Container Name to upload files</param>
        /// <param name="blobName">File name</param>
        /// <param name="filePath">File path of the file to upload</param>
        /// <returns>BlobStorageHelperInfo</returns>
        public static async Task <BlobStorageHelperInfo> UploadFileAsync(string connectionString, string containerName, string blobName, string filePath)
        {
            BlobStorageHelperInfo blobStorageHelperInfo = new BlobStorageHelperInfo();

            try
            {
                //checking if container is available
                if (!await IsContainerAvailableAsync(connectionString, containerName))
                {
                    blobStorageHelperInfo.Message = $"Container {containerName} is not available";
                    blobStorageHelperInfo.Status  = false;
                    return(blobStorageHelperInfo);
                }

                //checking if blob is already available
                if (await IsBlobAvailableAsync(connectionString, containerName, blobName))
                {
                    blobStorageHelperInfo.Message = $"Blob \"{blobName}\" already exists";
                    blobStorageHelperInfo.Status  = false;
                    return(blobStorageHelperInfo);
                }

                //Upload blob
                BlobServiceClient   blobServiceClient   = new BlobServiceClient(connectionString);
                BlobContainerClient blobContainerClient = blobServiceClient.GetBlobContainerClient(containerName);
                BlobClient          blobClient          = blobContainerClient.GetBlobClient(blobName);

                FileStream      uploadFileStream = File.OpenRead(filePath);
                BlobContentInfo status           = await blobClient.UploadAsync(uploadFileStream, true);

                uploadFileStream.Close();
                blobStorageHelperInfo.Message = $"File uploaded successfully. Uri : {blobClient.Uri}";
                blobStorageHelperInfo.Status  = true;
                return(blobStorageHelperInfo);
            }
            catch (Exception ex)
            {
                throw new Exception($"The file upload was not successful. Exception: {ex.Message}");
            }
        }
Esempio n. 7
0
        public async Task UploadToContainer()
        {
            Console.WriteLine("Uploading");

            var uploadPath           = $"{_downloadPath}\\toUpload";
            var uploadContainerName  = "testcontainer";
            var sampleUploadFileName = "Sample upload.txt";

            BlobContainerClient uploadContainer = _client.GetBlobContainerClient(uploadContainerName);

            using FileStream fsu = File.OpenRead($"{uploadPath}\\{sampleUploadFileName}");

            // Upload file
            BlobContentInfo blobContentInfo = await uploadContainer.UploadBlobAsync(sampleUploadFileName, fsu);

            // Update metadata
            BlobClient metaBlobClient = uploadContainer.GetBlobClient("Sample upload.txt");
            IDictionary <string, string> newMetadata = new Dictionary <string, string>();

            newMetadata.Add("Owner", "David");

            await metaBlobClient.SetMetadataAsync(newMetadata);
        }
            // To claim ownership of a partition, we have to write a new blob to Blob Storage
            // (if this partition has never been claimed before) or update the metadata of an existing blob.

            protected override async Task <IEnumerable <EventProcessorPartitionOwnership> > ClaimOwnershipAsync(
                IEnumerable <EventProcessorPartitionOwnership> desiredOwnership,
                CancellationToken cancellationToken = default)
            {
                List <EventProcessorPartitionOwnership> claimedOwnerships = new List <EventProcessorPartitionOwnership>();

                foreach (EventProcessorPartitionOwnership ownership in desiredOwnership)
                {
                    Dictionary <string, string> ownershipMetadata = new Dictionary <string, string>()
                    {
                        { OwnerIdentifierMetadataKey, ownership.OwnerIdentifier },
                    };

                    // Construct the path to the blob and get a blob client for it so we can interact with it.

                    string ownershipBlob = string.Format(
                        OwnershipPrefixFormat + ownership.PartitionId,
                        ownership.FullyQualifiedNamespace.ToLowerInvariant(),
                        ownership.EventHubName.ToLowerInvariant(),
                        ownership.ConsumerGroup.ToLowerInvariant());

                    BlobClient ownershipBlobClient = StorageContainer.GetBlobClient(ownershipBlob);

                    try
                    {
                        if (ownership.Version == null)
                        {
                            // In this case, we are trying to claim ownership of a partition which was previously unowned, and
                            // hence did not have an ownership file.  To ensure only a single host grabs the partition, we use a
                            // conditional request so that we only create our blob in the case where it does not yet exist.

                            using MemoryStream emptyStream = new MemoryStream(Array.Empty <byte>());

                            BlobRequestConditions requestConditions = new BlobRequestConditions
                            {
                                IfNoneMatch = ETag.All
                            };

                            BlobContentInfo info = await ownershipBlobClient.UploadAsync(
                                emptyStream,
                                metadata : ownershipMetadata,
                                conditions : requestConditions,
                                cancellationToken : cancellationToken)
                                                   .ConfigureAwait(false);

                            claimedOwnerships.Add(new EventProcessorPartitionOwnership
                            {
                                ConsumerGroup           = ownership.ConsumerGroup,
                                EventHubName            = ownership.EventHubName,
                                FullyQualifiedNamespace = ownership.FullyQualifiedNamespace,
                                LastModifiedTime        = info.LastModified,
                                OwnerIdentifier         = ownership.OwnerIdentifier,
                                PartitionId             = ownership.PartitionId,
                                Version = info.ETag.ToString()
                            });
                        }
                        else
                        {
                            // In this case, the partition is owned by some other host.  The ownership file already exists,
                            // so we just need to change metadata on it.  But we should only do this if the metadata has not
                            // changed between when we listed ownership and when we are trying to claim ownership, i.e.  the
                            // ETag for the file has not changed.

                            BlobRequestConditions requestConditions = new BlobRequestConditions
                            {
                                IfMatch = new ETag(ownership.Version)
                            };

                            BlobInfo info = await ownershipBlobClient.SetMetadataAsync(
                                ownershipMetadata,
                                requestConditions,
                                cancellationToken)
                                            .ConfigureAwait(false);

                            claimedOwnerships.Add(new EventProcessorPartitionOwnership
                            {
                                ConsumerGroup           = ownership.ConsumerGroup,
                                EventHubName            = ownership.EventHubName,
                                FullyQualifiedNamespace = ownership.FullyQualifiedNamespace,
                                LastModifiedTime        = info.LastModified,
                                OwnerIdentifier         = ownership.OwnerIdentifier,
                                PartitionId             = ownership.PartitionId,
                                Version = info.ETag.ToString()
                            });
                        }
                    }
                    catch (RequestFailedException ex) when(
                        ex.ErrorCode == BlobErrorCode.BlobAlreadyExists ||
                        ex.ErrorCode == BlobErrorCode.ConditionNotMet)
                    {
                        // In this case, another host has claimed the partition before we did.  That's safe to ignore.
                        // We'll still try to claim other partitions.
                    }
                }

                return(claimedOwnerships);
            }
Esempio n. 9
0
 public static string GetContentHash(this BlobContentInfo self)
 {
     return(Convert.ToBase64String(self.ContentHash));
 }
Esempio n. 10
0
 public static string GetETagString(this BlobContentInfo self)
 {
     return(self.ETag.ToString().Trim('"'));
 }
        //-------------------------------------------------
        // Demonstrate optimistic concurrency for write operations to a blob
        //-------------------------------------------------

        // <Snippet_DemonstrateOptimisticConcurrencyBlob>
        private static async Task DemonstrateOptimisticConcurrencyBlob(BlobClient blobClient)
        {
            Console.WriteLine("Demonstrate optimistic concurrency");

            BlobContainerClient containerClient = blobClient.GetParentBlobContainerClient();

            try
            {
                // Create the container if it does not exist.
                await containerClient.CreateIfNotExistsAsync();

                // Upload text to a new block blob.
                string blobContents1 = "First update. Overwrite blob if it exists.";
                byte[] byteArray     = Encoding.ASCII.GetBytes(blobContents1);

                ETag originalETag;

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, overwrite : true);

                    originalETag = blobContentInfo.ETag;
                    Console.WriteLine("Blob added. Original ETag = {0}", originalETag);
                }

                // This code simulates an update by another client.
                // No ETag was provided, so original blob is overwritten and ETag updated.
                string blobContents2 = "Second update overwrites first update.";
                byteArray = Encoding.ASCII.GetBytes(blobContents2);

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, overwrite : true);

                    Console.WriteLine("Blob updated. Updated ETag = {0}", blobContentInfo.ETag);
                }

                // Now try to update the blob using the original ETag value.
                string blobContents3 = "Third update. If-Match condition set to original ETag.";
                byteArray = Encoding.ASCII.GetBytes(blobContents3);

                // Set the If-Match condition to the original ETag.
                BlobUploadOptions blobUploadOptions = new BlobUploadOptions()
                {
                    Conditions = new BlobRequestConditions()
                    {
                        IfMatch = originalETag
                    }
                };

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    // This call should fail with error code 412 (Precondition Failed).
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, blobUploadOptions);
                }
            }
            catch (RequestFailedException e)
            {
                if (e.Status == (int)HttpStatusCode.PreconditionFailed)
                {
                    Console.WriteLine(
                        @"Precondition failure as expected. Blob's ETag does not match ETag provided.");
                }
                else
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }
        }
        // </Snippet_DemonstrateOptimisticConcurrencyBlob>

        #endregion

        #region DemonstratePessimisticConcurrencyBlob

        //-------------------------------------------------
        // Demonstrate pessimistic concurrency for write operations to a blob
        //-------------------------------------------------

        // <Snippet_DemonstratePessimisticConcurrencyBlob>
        public static async Task DemonstratePessimisticConcurrencyBlob(BlobClient blobClient)
        {
            Console.WriteLine("Demonstrate pessimistic concurrency");

            BlobContainerClient containerClient = blobClient.GetParentBlobContainerClient();
            BlobLeaseClient     blobLeaseClient = blobClient.GetBlobLeaseClient();

            try
            {
                // Create the container if it does not exist.
                await containerClient.CreateIfNotExistsAsync();

                // Upload text to a blob.
                string blobContents1 = "First update. Overwrite blob if it exists.";
                byte[] byteArray     = Encoding.ASCII.GetBytes(blobContents1);
                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, overwrite : true);
                }

                // Acquire a lease on the blob.
                BlobLease blobLease = await blobLeaseClient.AcquireAsync(TimeSpan.FromSeconds(15));

                Console.WriteLine("Blob lease acquired. LeaseId = {0}", blobLease.LeaseId);

                // Set the request condition to include the lease ID.
                BlobUploadOptions blobUploadOptions = new BlobUploadOptions()
                {
                    Conditions = new BlobRequestConditions()
                    {
                        LeaseId = blobLease.LeaseId
                    }
                };

                // Write to the blob again, providing the lease ID on the request.
                // The lease ID was provided, so this call should succeed.
                string blobContents2 = "Second update. Lease ID provided on request.";
                byteArray = Encoding.ASCII.GetBytes(blobContents2);

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream, blobUploadOptions);
                }

                // This code simulates an update by another client.
                // The lease ID is not provided, so this call fails.
                string blobContents3 = "Third update. No lease ID provided.";
                byteArray = Encoding.ASCII.GetBytes(blobContents3);

                using (MemoryStream stream = new MemoryStream(byteArray))
                {
                    // This call should fail with error code 412 (Precondition Failed).
                    BlobContentInfo blobContentInfo = await blobClient.UploadAsync(stream);
                }
            }
            catch (RequestFailedException e)
            {
                if (e.Status == (int)HttpStatusCode.PreconditionFailed)
                {
                    Console.WriteLine(
                        @"Precondition failure as expected. The lease ID was not provided.");
                }
                else
                {
                    Console.WriteLine(e.Message);
                    throw;
                }
            }
            finally
            {
                await blobLeaseClient.ReleaseAsync();
            }
        }
Esempio n. 13
0
 public void CanUploadBlobsRecursivelyToContainer() => RunOnService(async service =>
 {
     BlobContainerClient blobContainerClient = GetBlobContainerClient("dummycontainer");
     BlobContentInfo response = await service.UploadContentToContainer(blobContainerClient, "C:\\dummy.txt", "Recursive");
 });