public static async Task <bool> IsFileIdenticalToBlobAsync(this BlobClient client, string file)
        {
            BlobProperties properties = await client.GetPropertiesAsync();

            if (properties.ContentHash != null)
            {
                var localMD5 = CalculateMD5(file);
                var blobMD5  = Convert.ToBase64String(properties.ContentHash);
                return(blobMD5.Equals(localMD5, StringComparison.OrdinalIgnoreCase));
            }

            var localFileSize = new FileInfo(file).Length;
            var blobSize      = (await client.GetPropertiesAsync()).Value.ContentLength;

            if (localFileSize != blobSize)
            {
                return(false);
            }

            int bytesPerMegabyte = 1 * 1024 * 1024;

            using Stream localFileStream = File.OpenRead(file);
            byte[] localBuffer    = new byte[bytesPerMegabyte];
            byte[] remoteBuffer   = new byte[bytesPerMegabyte];
            int    localBytesRead = 0;

            do
            {
                long start = localFileStream.Position;
                localBytesRead = await localFileStream.ReadAsync(localBuffer, 0, bytesPerMegabyte);

                if (localBytesRead == 0)
                {
                    // eof == we are done, we have already validated that they are the same size
                    return(true);
                }

                var range = new HttpRange(start, localBytesRead);
                BlobDownloadInfo download = await client.DownloadAsync(range).ConfigureAwait(false);

                if (download.ContentLength != localBytesRead)
                {
                    return(false);
                }

                using (var stream = new MemoryStream(remoteBuffer, true))
                {
                    await download.Content.CopyToAsync(stream).ConfigureAwait(false);
                }
                if (!remoteBuffer.SequenceEqual(localBuffer))
                {
                    return(false);
                }
            }while (localBytesRead > 0);

            return(true);
        }
        private static async Task CopyBlobAsync(BlobContainerClient container, BlobContainerClient destContainer, JPOFileInfo info, ILogger log)
        {
            try {
                // Get the name of the first blob in the container to use as the source.
                string blobName = info.fileName;

                // Create a BlobClient representing the source blob to copy.
                BlobClient sourceBlob = container.GetBlobClient(blobName);

                // Ensure that the source blob exists.
                if (await sourceBlob.ExistsAsync())
                {
                    // Lease the source blob for the copy operation to prevent another client from modifying it.
                    BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient();

                    // Specifying -1 for the lease interval creates an infinite lease.
                    //await lease.AcquireAsync(TimeSpan.FromSeconds(100));

                    // Get the source blob's properties and display the lease state.
                    BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync();

                    log.LoggerInfo($"Lease state: {sourceProperties.LeaseState}", info);

                    Uri blob_sas_uri = BlobUtilities.GetServiceSASUriForBlob(sourceBlob, container.Name, null);

                    // Get a BlobClient representing the destination blob
                    BlobClient destBlob = destContainer.GetBlobClient(blobName);//destContainer.GetBlobClient(blob_sas_uri.ToString());

                    // Start the copy operation.
                    await destBlob.StartCopyFromUriAsync(blob_sas_uri);

                    // Get the destination blob's properties and display the copy status.
                    BlobProperties destProperties = await destBlob.GetPropertiesAsync();

                    // Update the source blob's properties.
                    sourceProperties = await sourceBlob.GetPropertiesAsync();

                    if (sourceProperties.LeaseState == LeaseState.Leased)
                    {
                        // Break the lease on the source blob.
                        await lease.BreakAsync();

                        // Update the source blob's properties to check the lease state.
                        sourceProperties = await sourceBlob.GetPropertiesAsync();
                    }
                }
            }
            catch (RequestFailedException ex) {
                log.LoggerError($"RequestFailedException: {ex.Message}", ex?.StackTrace, info);
                Console.WriteLine(ex.Message);
                Console.ReadLine();
                throw;
            }
        }
        public async Task <IMessageAttachment> GetAttachmentAsync(string queueName, string id, CancellationToken cancellationToken = default(CancellationToken))
        {
            var blob       = new BlobClient(_connectionString, queueName, id);
            var properties = await blob.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

            return(new MessageAttachment(properties.Value.Metadata["Filename"], properties.Value.ContentType, await blob.OpenReadAsync(cancellationToken: cancellationToken).ConfigureAwait(false)));
        }
Exemple #4
0
        public async Task <IActionResult> GetAsync(string userName, string fileId)
        {
            try
            {
                BlobClient     blobClient = this.uploadsContainer.GetBlobClient($"{userName}/{fileId}");
                BlobProperties props      = await blobClient.GetPropertiesAsync().ConfigureAwait(false);

                MemoryStream blobStream = new MemoryStream();

                var file = await blobClient.DownloadToAsync(blobStream).ConfigureAwait(false);

                blobStream.Position = 0;

                return(File(blobStream, props.ContentType));
            }
            catch (Azure.RequestFailedException ex)
            {
                logger.LogError($"Received Exception {ex}");
                if (ex.Status == (int)HttpStatusCode.NotFound)
                {
                    return(NotFound(ex));
                }
                return(StatusCode(501));
            }
            catch (Exception ex)
            {
                logger.LogError($"File not present for the resource {ex}");
                return(StatusCode(500, ex));
            }
        }
        // Iterate through the list of source blobs, and transfer them to the destination container
        private static async Task MoveMatchingBlobsAsync(IEnumerable <BlobClient> sourceBlobRefs, BlobContainerClient sourceContainer, BlobContainerClient destContainer)
        {
            foreach (BlobClient sourceBlobRef in sourceBlobRefs)
            {
                // Copy the source blob
                BlobClient sourceBlob = sourceContainer.GetBlobClient(sourceBlobRef.Name);

                // Check the source file's metadata
                Response <BlobProperties> propertiesResponse = await sourceBlob.GetPropertiesAsync();

                BlobProperties properties = propertiesResponse.Value;

                BlobClient           destBlob = destContainer.GetBlobClient(sourceBlobRef.Name);
                CopyFromUriOperation ops      = await destBlob.StartCopyFromUriAsync(GetSharedAccessUri(sourceBlobRef.Name, sourceContainer));

                // Display the status of the blob as it is copied
                while (ops.HasCompleted == false)
                {
                    long copied = await ops.WaitForCompletionAsync();

                    Console.WriteLine($"Blob: {destBlob.Name}, Copied: {copied} of {properties.ContentLength}");
                    await Task.Delay(500);
                }

                Console.WriteLine($"Blob: {destBlob.Name} Complete");

                // Remove the source blob
                bool blobExisted = await sourceBlobRef.DeleteIfExistsAsync();
            }
        }
        // Find all blobs that have been modified since the specified date and time
        private static async Task <IEnumerable <BlobClient> > FindMatchingBlobsAsync(BlobContainerClient blobContainer, DateTimeOffset transferBlobsModifiedSince)
        {
            List <BlobClient> blobList = new List <BlobClient>();

            // Iterate through the blobs in the source container
            List <BlobItem> segment = await blobContainer.GetBlobsAsync(prefix : "").ToListAsync();

            foreach (BlobItem blobItem in segment)
            {
                BlobClient blob = blobContainer.GetBlobClient(blobItem.Name);

                // Check the source file's metadata
                Response <BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();

                BlobProperties properties = propertiesResponse.Value;

                // Check the last modified date and time
                // Add the blob to the list if has been modified since the specified date and time
                if (DateTimeOffset.Compare(properties.LastModified.ToUniversalTime(), transferBlobsModifiedSince.ToUniversalTime()) > 0)
                {
                    blobList.Add(blob);
                }
            }

            // Return the list of blobs to be transferred
            return(blobList);
        }
        /// <summary>
        /// Get blob info by URL
        /// </summary>
        /// <param name="blobUrl">Absolute or relative URL to get blob</param>
        public virtual async Task <BlobInfo> GetBlobInfoAsync(string blobUrl)
        {
            if (string.IsNullOrEmpty(blobUrl))
            {
                throw new ArgumentNullException(nameof(blobUrl));
            }

            var      uri    = blobUrl.IsAbsoluteUrl() ? new Uri(blobUrl) : new Uri(_blobServiceClient.Uri, blobUrl.TrimStart(Delimiter[0]));
            BlobInfo result = null;

            try
            {
                var blob  = new BlobClient(new Uri(_blobServiceClient.Uri, uri.AbsolutePath.TrimStart('/')));
                var props = await blob.GetPropertiesAsync();

                result = ConvertBlobToBlobInfo(blob, props.Value);
            }
            catch
            {
                // Since the storage account is based on transaction volume, it is better to handle the 404 (BlobNotFound) exception because that is just one api call, as opposed to checking the BlobClient.ExistsAsync() first and then making the BlobClient.DownloadAsync() call (2 api transactions).
                //https://elcamino.cloud/articles/2020-03-30-azure-storage-blobs-net-sdk-v12-upgrade-guide-and-tips.html
            }

            return(result);
        }
Exemple #8
0
        public async Task UploadFileAsync(UploadFileViewModel fileToUpload)
        {
            BlobContainerClient container = blobServiceClient.GetBlobContainerClient(fileToUpload.SelectedContainer);

            try
            {
                // Get a reference to a blob
                BlobClient blob = container.GetBlobClient(fileToUpload.InputFile.FileName);

                // Open the file and upload its data
                using (FileStream file = File.OpenRead(fileToUpload.FilePath))
                {
                    await blob.UploadAsync(file);
                }

                // Verify we uploaded some content
                BlobProperties properties = await blob.GetPropertiesAsync();
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
                throw;
            }
        }
        /// <summary>
        /// Add a like for passed in blog name
        /// Client can pass in a -ve and +ve number to increase or
        /// decrease like count
        /// </summary>
        public async Task <int> Likes(BlogInfo blogInfo,
                                      string storageConnectionString,
                                      string contentFileRoot)
        {
            BlobClient blob = await CreateBlogInfoIfNotExists(blogInfo, contentFileRoot, storageConnectionString);

            int currentLike = blogInfo.Likes;
            // Return pre-computed content if info already
            // exists in the blog
            BlobProperties props = await blob.GetPropertiesAsync();

            if (props.Metadata.ContainsKey("likes"))
            {
                var oldLikes = Int32.Parse(props.Metadata["likes"]);
                currentLike += oldLikes;
            }

            if (currentLike < 0)
            {
                currentLike = 0;
            }
            props.Metadata["likes"] = currentLike.ToString();
            blob.SetMetadata(props.Metadata);
            return(currentLike);
        }
Exemple #10
0
        /// <summary>
        /// Get and prepend passed in comments for passed in blog name
        ///
        /// </summary>
        public async Task <string> Comments(BlogInfo blogInfo,
                                            string storageConnectionString,
                                            string contentFileRoot)
        {
            BlobClient blob = await CreateBlogInfoIfNotExists(blogInfo, contentFileRoot, storageConnectionString);

            string comments = blogInfo.Comments;
            // Return pre-computed content if info already
            // exists in the blog
            BlobProperties props = await blob.GetPropertiesAsync();

            if (props.Metadata.ContainsKey("comments"))
            {
                string oldComments =
                    Encoding.UTF8.GetString(Convert.FromBase64String(props.Metadata["comments"]));
                if (comments != null)
                {
                    comments += oldComments;
                }
                else
                {
                    comments = oldComments;
                }
            }

            if (comments == null)
            {
                return(null);
            }
            props.Metadata["comments"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(comments));;
            blob.SetMetadata(props.Metadata);
            return(comments);
        }
Exemple #11
0
        /// <summary>
        /// Get minstoread for passed in blog name + file content
        /// If metadata for blog doesn't exist in Azure blobs,
        /// compute and store info
        /// </summary>
        public async Task <string> GetMinsToRead(BlogInfo blogInfo,
                                                 string storageConnectionString,
                                                 string contentFileRoot)
        {
            BlobClient blob = await CreateBlogInfoIfNotExists(blogInfo, contentFileRoot, storageConnectionString);

            // Return pre-computed content if info already
            // exists in the blog
            BlobProperties props = await blob.GetPropertiesAsync();

            if (props.Metadata.ContainsKey("minsToRead"))
            {
                return(props.Metadata["minsToRead"]);
            }

            // Open and read the file from its http location
            var response = await httpClient.GetAsync(blogInfo.Path);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApplicationException("Cannot read blog file");
            }

            // Store the metadata in the blob
            var responseContent = await response.Content.ReadAsStringAsync();

            var mins = ComputeMinsToRead(blob, blogInfo, responseContent);

            props.Metadata.Add("minsToRead", mins.ToString());
            blob.SetMetadata(props.Metadata);
            return(mins.ToString());
        }
Exemple #12
0
        /// <summary>
        /// Get existing metadata for a blog
        /// </summary>
        public async Task <BlogInfo> GetBlogMetadata(BlogInfo info,
                                                     string storageConnectionString,
                                                     string contentFileRoot)
        {
            BlobClient blob = await CreateBlogInfoIfNotExists(info, contentFileRoot, storageConnectionString);

            // Return pre-computed content if info already
            // exists in the blog
            BlobProperties props = await blob.GetPropertiesAsync();

            if (props.Metadata.ContainsKey("comments"))
            {
                info.Comments =
                    Encoding.UTF8.GetString(Convert.FromBase64String(props.Metadata["comments"]));
            }
            if (props.Metadata.ContainsKey("likes"))
            {
                info.Likes = Int32.Parse(props.Metadata["likes"]);
            }

            if (props.Metadata.ContainsKey("minsToRead"))
            {
                info.MinsToRead = props.Metadata["minsToRead"];
            }
            return(info);
        }
Exemple #13
0
        private async Task UploadToContainerAsync(BlobClient blobClient, object obj)
        {
            using var ms = new MemoryStream();
            var json = JsonConvert.SerializeObject(obj);

            using StreamWriter writer = new StreamWriter(ms);
            writer.Write(json);
            writer.Flush();
            ms.Position = 0;

            await blobClient.UploadAsync(ms);

            var properties = await blobClient.GetPropertiesAsync();

            BlobHttpHeaders headers = new BlobHttpHeaders
            {
                // Set the MIME ContentType every time the properties
                // are updated or the field will be cleared
                ContentType = "application/json",

                // Populate remaining headers with
                // the pre-existing properties
                CacheControl       = properties.Value.CacheControl,
                ContentDisposition = properties.Value.ContentDisposition,
                ContentEncoding    = properties.Value.ContentEncoding,
                ContentHash        = properties.Value.ContentHash
            };

            // Set the blob's properties.
            await blobClient.SetHttpHeadersAsync(headers);
        }
Exemple #14
0
        public async Task SetBlobPropertiesAsync(BlobClient blob, string newCacheControl = null, string newContentType = null)
        {
            var properties = await blob.GetPropertiesAsync();

            BlobHttpHeaders headers = new BlobHttpHeaders
            {
                // Populate remaining headers with
                // the pre-existing properties
                ContentType        = properties.Value.ContentType,
                ContentLanguage    = properties.Value.ContentLanguage,
                CacheControl       = properties.Value.CacheControl,
                ContentDisposition = properties.Value.ContentDisposition,
                ContentEncoding    = properties.Value.ContentEncoding,
                ContentHash        = properties.Value.ContentHash
            };

            if (!String.IsNullOrWhiteSpace(newCacheControl))
            {
                headers.CacheControl = newCacheControl;
            }

            if (!String.IsNullOrWhiteSpace(newContentType))
            {
                headers.ContentType = newContentType;
            }

            // Set the blob's properties.
            await blob.SetHttpHeadersAsync(headers);
        }
        public async Task <string> Add(byte[] file, string container, string filePath, string contentType = null)
        {
            BlobContainerClient containerClient = _blobServiceClient.GetBlobContainerClient(container);

            BlobClient blobClient = containerClient.GetBlobClient(filePath);

            if (await blobClient.ExistsAsync())
            {
                var properties = await blobClient.GetPropertiesAsync();

                if (properties.GetHashCode() == filePath.GetHashCode())
                {
                    return(blobClient.Name);
                }
            }

            _logger.LogInformation($"Uploading to Blob Storage:\n\t {blobClient.Uri}\n");

            contentType ??= filePath.GetContentType();

            var memoryStream = new MemoryStream(file);
            await blobClient.UploadAsync(memoryStream, new BlobHttpHeaders { ContentType = contentType });

            _logger.LogInformation(blobClient.Name);

            return(filePath);
        }
        public async Task UploadAsync()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string path = CreateTempFile(SampleFileContent);

            string connectionString = ConnectionString;


            BlobContainerClient container = new BlobContainerClient(connectionString, Randomize("sample-container"));
            await container.CreateAsync();

            try
            {
                BlobClient blob = container.GetBlobClient(Randomize("sample-file"));


                using (FileStream file = File.OpenRead(path))
                {
                    await blob.UploadAsync(file);
                }


                BlobProperties properties = await blob.GetPropertiesAsync();

                Assert.AreEqual(SampleFileContent.Length, properties.ContentLength);
            }
            finally
            {
                // Clean up after the test when we're finished
                await container.DeleteAsync();
            }
        }
Exemple #17
0
        private static async Task <string> ApplyProperties(BlobClient blob, Dictionary <string, string> contentTypeMappings)
        {
            var    suffix      = Path.GetExtension(blob.Name);
            string contentType = null;

            if (contentTypeMappings.ContainsKey(suffix))
            {
                contentType = contentTypeMappings[suffix];
            }

            // Get the existing properties
            BlobProperties properties = await blob.GetPropertiesAsync();

            BlobHttpHeaders headers = new BlobHttpHeaders
            {
                ContentType        = contentType ?? properties.ContentType,
                ContentLanguage    = properties.ContentLanguage,
                CacheControl       = properties.CacheControl,
                ContentDisposition = properties.ContentDisposition,
                ContentEncoding    = properties.ContentEncoding,
                ContentHash        = properties.ContentHash
            };

            // Set the blob's properties.
            await blob.SetHttpHeadersAsync(headers);

            return(contentType ?? "");
        }
        /// <summary>
        /// Checks the blob to see if it can be archived.
        /// </summary>
        /// <param name="blob"></param>
        /// <param name="filterCriteria"></param>
        /// <returns></returns>
        private static async Task <bool> DoesBlobMatchFilterCriteria(BlobClient blob, Models.FilterCriteria filterCriteria)
        {
            BlobProperties blobProperties = await blob.GetPropertiesAsync();

            if (blobProperties.AccessTier == AccessTier.Archive)
            {
                return(false);
            }
            var  dateTimeFrom          = filterCriteria.LastModifiedDateFrom ?? DateTime.MinValue;
            var  dateTimeTo            = filterCriteria.LastModifiedDateTo ?? DateTime.MaxValue;
            var  minBlobSize           = filterCriteria.MinBlobSize;
            bool isDateTimeCheckPassed = false;

            if (blobProperties.LastModified != null)
            {
                var lastModified = blobProperties.LastModified.DateTime;
                if (dateTimeFrom <= lastModified && dateTimeTo >= lastModified)
                {
                    isDateTimeCheckPassed = true;
                }
            }
            bool isBlobSizeCheckPassed = blobProperties.ContentLength >= minBlobSize;

            return(isDateTimeCheckPassed || isBlobSizeCheckPassed);
        }
Exemple #19
0
        public async Task UploadAsync()
        {
            // Create a temporary Lorem Ipsum file on disk that we can upload
            string path = _mockupService.CreateTempFile();

            // Get a reference to a container named "sample-container" and then create it
            BlobContainerClient container = new BlobContainerClient(connectionString, _mockupService.Randomize(AppConstant.SAMPLE_CONTAINER_NAME));
            await container.CreateAsync();

            try
            {
                // Get a reference to a blob
                BlobClient blob = container.GetBlobClient(_mockupService.Randomize(AppConstant.SAMPLE_FILE_NAME));

                // Open the file and upload its data
                using (FileStream file = File.OpenRead(path))
                {
                    await blob.UploadAsync(file);
                }

                // Verify we uploaded some content
                BlobProperties properties = await blob.GetPropertiesAsync();
            }
            finally
            {
                // delete file and container after compleating operation
                await container.DeleteAsync();
            }
        }
Exemple #20
0
        protected virtual async Task <Blob> GetBlobAsync(string fullPath, CancellationToken cancellationToken)
        {
            (BlobContainerClient container, string path) = await GetPartsAsync(fullPath, false).ConfigureAwait(false);

            if (container == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(path))
            {
                //it's a container

                Response <BlobContainerProperties> attributes = await container.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(container.Name, attributes));
            }

            BlobClient client = container.GetBlobClient(path);

            try
            {
                Response <BlobProperties> properties = await client.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(AzConvert.ToBlob(_containerName, path, properties));
            }
            catch (RequestFailedException ex) when(ex.ErrorCode == "BlobNotFound")
            {
                return(null);
            }
        }
        private async Task <string> TryCreateSnapshot(BlobClient blobClient, string versionKey)
        {
            var version = 1;

            try
            {
                var propResponse = await blobClient.GetPropertiesAsync();

                if (propResponse != null)
                {
                    var metadata = propResponse.Value.Metadata;
                    if (metadata.TryGetValue(versionKey, out string prevVersion))
                    {
                        version = int.Parse(prevVersion) + 1;
                    }
                    else
                    {
                        throw new InvalidOperationException("version not found in blob metadata");
                    }
                    await blobClient.CreateSnapshotAsync(metadata);
                }
            }
            catch (RequestFailedException ex)
            {
                if (ex.Status != 404)
                {
                    throw ex;
                }
            }
            return(Convert.ToString(version));
        }
        public async Task UploadAsync_File_AccessTier(int?maximumThreadCount)
        {
            await using DisposingContainer test = await GetTestContainerAsync();

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

            using (var stream = new MemoryStream(data))
            {
                var path = Path.GetTempFileName();

                try
                {
                    File.WriteAllBytes(path, data);

                    await blob.UploadAsync(
                        path,
                        accessTier : AccessTier.Cool);
                }
                finally
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
            }

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

            Assert.AreEqual(AccessTier.Cool.ToString(), properties.Value.AccessTier);
        }
        public async Task UploadAsync_File_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))
            {
                var path = Path.GetTempFileName();

                try
                {
                    File.WriteAllBytes(path, data);

                    await blob.UploadAsync(path);
                }
                finally
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                }
            }

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

            Assert.AreEqual(BlobType.Block, properties.Value.BlobType);
        }
        public async Task UploadAsync_Stream_StorageTransferOptions(int?maximumThreadCount)
        {
            await using DisposingContainer test = await GetTestContainerAsync();

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

            using (var stream = new MemoryStream(data))
            {
                var options = new StorageTransferOptions {
                    MaximumConcurrency = maximumThreadCount
                };

                await Verify(stream => blob.UploadAsync(stream, transferOptions: options));

                async Task Verify(Func <Stream, Task <Response <BlobContentInfo> > > upload)
                {
                    using (var stream = new MemoryStream(data))
                    {
                        await upload(stream);
                    }

                    Response <BlobDownloadInfo> download = await blob.DownloadAsync();

                    using var actual = new MemoryStream();
                    await download.Value.Content.CopyToAsync(actual);

                    TestHelper.AssertSequenceEqual(data, actual.ToArray());
                }
            }

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

            Assert.AreEqual(BlobType.Block, properties.Value.BlobType);
        }
        // </Snippet_ReadContainerMetadata>

        //-------------------------------------------------
        // Set blob properties
        //-------------------------------------------------
        // <Snippet_SetBlobProperties>
        public static async Task SetBlobPropertiesAsync(BlobClient blob)
        {
            Console.WriteLine("Setting blob properties...");

            try
            {
                // Get the existing properties
                BlobProperties properties = await blob.GetPropertiesAsync();

                BlobHttpHeaders headers = new BlobHttpHeaders
                {
                    // Set the MIME ContentType every time the properties
                    // are updated or the field will be cleared
                    ContentType     = "text/plain",
                    ContentLanguage = "en-us",

                    // Populate remaining headers with
                    // the pre-existing properties
                    CacheControl       = properties.CacheControl,
                    ContentDisposition = properties.ContentDisposition,
                    ContentEncoding    = properties.ContentEncoding,
                    ContentHash        = properties.ContentHash
                };

                // Set the blob's properties.
                await blob.SetHttpHeadersAsync(headers);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine($"HTTP error code {e.Status}: {e.ErrorCode}");
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
Exemple #26
0
        /// <summary>
        /// Return a bool indicating whether a local file's content is the same as
        /// the content of a given blob.
        ///
        /// If the blob has the ContentHash property set, the comparison is performed using
        /// that (MD5 hash).  All recently-uploaded blobs or those uploaded by these libraries
        /// should; some blob clients older than ~2012 may upload without the property set.
        ///
        /// When the ContentHash property is unset, a byte-by-byte comparison is performed.
        /// </summary>
        public async Task <bool> IsFileIdenticalToBlobAsync(string localFileFullPath, BlobClient blob)
        {
            BlobProperties properties = await blob.GetPropertiesAsync();

            if (properties.ContentHash != null)
            {
                var localMD5 = CalculateMD5(localFileFullPath);
                var blobMD5  = Convert.ToBase64String(properties.ContentHash);
                return(blobMD5.Equals(localMD5, StringComparison.OrdinalIgnoreCase));
            }
            else
            {
                int bytesPerMegabyte = 1 * 1024 * 1024;
                if (properties.ContentLength < bytesPerMegabyte)
                {
                    byte[] existingBytes = new byte[properties.ContentLength];
                    byte[] localBytes    = File.ReadAllBytes(localFileFullPath);

                    using (MemoryStream stream = new MemoryStream(existingBytes, true))
                    {
                        await blob.DownloadToAsync(stream).ConfigureAwait(false);
                    }
                    return(localBytes.SequenceEqual(existingBytes));
                }
                else
                {
                    using (Stream localFileStream = File.OpenRead(localFileFullPath))
                    {
                        byte[] localBuffer    = new byte[bytesPerMegabyte];
                        byte[] remoteBuffer   = new byte[bytesPerMegabyte];
                        int    bytesLocalFile = 0;

                        do
                        {
                            long start          = localFileStream.Position;
                            int  localBytesRead = await localFileStream.ReadAsync(localBuffer, 0, bytesPerMegabyte);

                            HttpRange        range    = new HttpRange(start, localBytesRead);
                            BlobDownloadInfo download = await blob.DownloadAsync(range).ConfigureAwait(false);

                            if (download.ContentLength != localBytesRead)
                            {
                                return(false);
                            }
                            using (MemoryStream stream = new MemoryStream(remoteBuffer, true))
                            {
                                await download.Content.CopyToAsync(stream).ConfigureAwait(false);
                            }
                            if (!remoteBuffer.SequenceEqual(localBuffer))
                            {
                                return(false);
                            }
                        }while (bytesLocalFile > 0);
                    }
                    return(true);
                }
            }
        }
Exemple #27
0
        public async Task <BlobContainerInfo> CloneContainer(string sourceContainerName, string targetContainerName)
        {
            try
            {
                // create target container
                BlobContainerClient targetContainerClient = new BlobContainerClient(connectionString, targetContainerName);
                BlobContainerInfo   targetContainer       = await targetContainerClient.CreateAsync();

                // get source container
                BlobContainerClient sourceContainerClient = blobServiceClient.GetBlobContainerClient(sourceContainerName);
                // blobs from source container
                await foreach (BlobItem blob in sourceContainerClient.GetBlobsAsync())
                {
                    // create a blob client for the source blob
                    BlobClient sourceBlob = sourceContainerClient.GetBlobClient(blob.Name);
                    // Ensure that the source blob exists.
                    if (await sourceBlob.ExistsAsync())
                    {
                        // Lease the source blob for the copy operation
                        // to prevent another client from modifying it.
                        BlobLeaseClient lease = sourceBlob.GetBlobLeaseClient();

                        // Specifying -1 for the lease interval creates an infinite lease.
                        await lease.AcquireAsync(TimeSpan.FromSeconds(60));

                        // Get a BlobClient representing the destination blob with a unique name.
                        BlobClient destBlob = targetContainerClient.GetBlobClient(sourceBlob.Name);

                        // Start the copy operation.
                        await destBlob.StartCopyFromUriAsync(sourceBlob.Uri);

                        // Update the source blob's properties.
                        BlobProperties sourceProperties = await sourceBlob.GetPropertiesAsync();

                        if (sourceProperties.LeaseState == LeaseState.Leased)
                        {
                            // Break the lease on the source blob.
                            await lease.BreakAsync();
                        }
                    }
                }


                return(targetContainer);
            }
            catch (RequestFailedException e)
            {
                Console.WriteLine("HTTP error code {0}: {1}",
                                  e.Status, e.ErrorCode);
                Console.WriteLine(e.Message);
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
Exemple #28
0
 public BlobFileInfo(BlobClient blobClient)
 {
     _blobClient     = blobClient ?? throw new ArgumentNullException(nameof(blobClient));
     _blobProperties = _blobClient
                       .GetPropertiesAsync()
                       .ConfigureAwait(false)
                       .GetAwaiter()
                       .GetResult().Value;
 }
Exemple #29
0
        private static async Task CopyBlob()
        {
            blobClient = containerClient.GetBlobClient(fileName);
            if (await blobClient.ExistsAsync())
            {
                BlobLeaseClient leaseClient = blobClient.GetBlobLeaseClient();

                // Specifying -1 for the lease interval creates an infinite lease.
                await leaseClient.AcquireAsync(TimeSpan.FromSeconds(-1));

                // Get the source blob's properties and display the lease state.
                BlobProperties sourceProperties = await blobClient.GetPropertiesAsync();

                Console.WriteLine($"Lease state: {sourceProperties.LeaseState}");

                // Get a BlobClient representing the destination blob with a unique name.
                BlobClient destBlob = containerClient.GetBlobClient(Guid.NewGuid() + "-" + blobClient.Name);

                // Start the copy operation.
                await destBlob.StartCopyFromUriAsync(blobClient.Uri);

                // Get the destination blob's properties and display the copy status.
                BlobProperties destProperties = await destBlob.GetPropertiesAsync();

                Console.WriteLine($"Copy status: {destProperties.CopyStatus}");
                Console.WriteLine($"Copy progress: {destProperties.CopyProgress}");
                Console.WriteLine($"Completion time: {destProperties.CopyCompletedOn}");
                Console.WriteLine($"Total bytes: {destProperties.ContentLength}");

                // Update the source blob's properties.
                sourceProperties = await blobClient.GetPropertiesAsync();

                if (sourceProperties.LeaseState == LeaseState.Leased)
                {
                    // Break the lease on the source blob.
                    await leaseClient.BreakAsync();

                    // Update the source blob's properties to check the lease state.
                    sourceProperties = await blobClient.GetPropertiesAsync();

                    Console.WriteLine($"Lease state: {sourceProperties.LeaseState}");
                }
            }
        }
        public async Task <RavenStorageClient.Blob> GetBlobAsync(string blobName)
        {
            BlobClient blob = _client.GetBlobClient(blobName);

            var properties = await blob.GetPropertiesAsync(cancellationToken : _cancellationToken);

            var response = await blob.DownloadAsync(cancellationToken : _cancellationToken);

            return(new RavenStorageClient.Blob(response.Value.Content, properties.Value.Metadata, response.Value));
        }