Exemplo n.º 1
0
        /// <summary>
        /// Gets the Azure.Storage.Blobs.Models.BlobProperties object.
        /// </summary>
        /// <param name="blobUri">The blobUri to read.</param>
        /// <returns>BlobProperties.</returns>
        private async Task <BlobProperties> GetBlobPropertiesAsync(Uri blobUri)
        {
            _ = blobUri ?? throw new ArgumentNullException(nameof(blobUri));
            var blobBaseClient = new BlobBaseClient(blobUri, _tokenCredential);

            return(await blobBaseClient.GetPropertiesAsync().ConfigureAwait(false));
        }
        public static async Task <BlobProperties> FetchPropertiesOrNullIfNotExistAsync(this BlobBaseClient blob,
                                                                                       CancellationToken cancellationToken = default)
        {
            if (blob == null)
            {
                throw new ArgumentNullException(nameof(blob));
            }

            try
            {
                BlobProperties blobProperties = await blob.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                return(blobProperties);
            }
            catch (RequestFailedException exception)
            {
                // Remember specific error codes are not available for Fetch (HEAD request).

                if (exception.IsNotFound())
                {
                    return(null);
                }
                else if (exception.IsOk())
                {
                    // If the blob type is incorrect (block vs. page) a 200 OK is returned but the SDK throws an
                    // exception.
                    return(null);
                }
                else
                {
                    throw;
                }
            }
        }
Exemplo n.º 3
0
        public async Task SetImmutibilityPolicyAsync()
        {
            // Arrange
            await using DisposingImmutableStorageWithVersioningContainer vlwContainer = await GetTestVersionLevelWormContainer(TestConfigOAuth);

            BlobBaseClient blob = await GetNewBlobClient(vlwContainer.Container);

            BlobImmutabilityPolicy immutabilityPolicy = new BlobImmutabilityPolicy
            {
                ExpiresOn  = Recording.UtcNow.AddMinutes(5),
                PolicyMode = BlobImmutabilityPolicyMode.Unlocked
            };

            // The service rounds Immutability Policy Expiry to the nearest second.
            DateTimeOffset expectedImmutabilityPolicyExpiry = RoundToNearestSecond(immutabilityPolicy.ExpiresOn.Value);

            // Test SetImmutabilityPolicyAsync API and validate response.
            // Act
            Response <BlobImmutabilityPolicy> response = await blob.SetImmutabilityPolicyAsync(immutabilityPolicy);

            // Assert
            Assert.AreEqual(expectedImmutabilityPolicyExpiry, response.Value.ExpiresOn);
            Assert.AreEqual(immutabilityPolicy.PolicyMode, response.Value.PolicyMode);

            // Validate that we are correctly deserializing Get Properties response.
            // Act
            Response <BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();

            // Assert
            Assert.AreEqual(expectedImmutabilityPolicyExpiry, propertiesResponse.Value.ImmutabilityPolicy.ExpiresOn);
            Assert.AreEqual(immutabilityPolicy.PolicyMode, propertiesResponse.Value.ImmutabilityPolicy.PolicyMode);

            // Validate we are correctly deserializing Blob Items.
            // Act
            List <BlobItem> blobItems = new List <BlobItem>();

            await foreach (BlobItem blobItem in vlwContainer.Container.GetBlobsAsync(traits: BlobTraits.ImmutabilityPolicy))
            {
                blobItems.Add(blobItem);
            }

            // Assert
            Assert.AreEqual(1, blobItems.Count);
            Assert.AreEqual(expectedImmutabilityPolicyExpiry, blobItems[0].Properties.ImmutabilityPolicy.ExpiresOn);
            Assert.AreEqual(immutabilityPolicy.PolicyMode, blobItems[0].Properties.ImmutabilityPolicy.PolicyMode);

            // Validate we are correctly deserialzing Get Blob response.
            // Act
            Response <BlobDownloadInfo> downloadResponse = await blob.DownloadAsync();

            // Assert
            Assert.AreEqual(expectedImmutabilityPolicyExpiry, downloadResponse.Value.Details.ImmutabilityPolicy.ExpiresOn);
            Assert.AreEqual(immutabilityPolicy.PolicyMode, downloadResponse.Value.Details.ImmutabilityPolicy.PolicyMode);
        }
Exemplo n.º 4
0
 //TODO consider removing this.
 public async Task<string> SetupBlobMatchCondition(BlobBaseClient blob, string match)
 {
     if (match == ReceivedETag)
     {
         Response<BlobProperties> headers = await blob.GetPropertiesAsync();
         return headers.Value.ETag.ToString();
     }
     else
     {
         return match;
     }
 }
        public static async Task Run(
            [BlobTrigger("sample-container/sample-blob-1")] BlobBaseClient blobTriggerClient,
            [Blob("sample-container/sample-blob-2")] BlobBaseClient blobClient,
            ILogger logger)
        {
            BlobProperties blobTriggerProperties = await blobTriggerClient.GetPropertiesAsync();

            logger.LogInformation("Blob sample-container/sample-blob-1 has been updated on: {datetime}", blobTriggerProperties.LastModified);
            BlobProperties blobProperties = await blobClient.GetPropertiesAsync();

            logger.LogInformation("Blob sample-container/sample-blob-2 has been updated on: {datetime}", blobProperties.LastModified);
        }
Exemplo n.º 6
0
        //TODO consider removing this.
        public async Task <string> SetupBlobMatchCondition(BlobBaseClient blob, string match)
        {
            if (match == this.ReceivedETag)
            {
                var headers = await blob.GetPropertiesAsync();

                return(headers.Value.ETag.ToString());
            }
            else
            {
                return(match);
            }
        }
        private async Task StartCopyFromUri(long taskId, IStorageBlobManagement destChannel, Uri srcUri, BlobBaseClient destBlob)
        {
            bool destExist = true;

            Track2Models.BlobType?      destBlobType = Util.GetBlobType(destBlob);
            Track2Models.BlobProperties properties   = null;

            try
            {
                properties   = (await destBlob.GetPropertiesAsync(this.BlobRequestConditions, cancellationToken: this.CmdletCancellationToken).ConfigureAwait(false)).Value;
                destBlobType = properties.BlobType;
            }
            catch (global::Azure.RequestFailedException e) when(e.Status == 404)
            {
                destExist = false;
            }
            if (destBlobType != null)
            {
                ValidateBlobTier(Util.convertBlobType_Track2ToTrack1(destBlobType), pageBlobTier, standardBlobTier, rehydratePriority);
            }

            if (!destExist || this.ConfirmOverwrite(srcUri.AbsoluteUri.ToString(), destBlob.Uri.ToString()))
            {
                Track2Models.BlobCopyFromUriOptions options = new global::Azure.Storage.Blobs.Models.BlobCopyFromUriOptions();

                // The Blob Type and Blob Tier must match, since already checked they are match at the begin of ExecuteCmdlet().
                if (pageBlobTier != null)
                {
                    options.AccessTier = Util.ConvertAccessTier_Track1ToTrack2(pageBlobTier);
                }
                else if (standardBlobTier != null || rehydratePriority != null)
                {
                    options.AccessTier        = Util.ConvertAccessTier_Track1ToTrack2(standardBlobTier);
                    options.RehydratePriority = Util.ConvertRehydratePriority_Track1ToTrack2(rehydratePriority);
                }
                if (this.BlobTag != null)
                {
                    options.Tags = this.BlobTag.Cast <DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value);
                }
                options.SourceConditions = this.BlobRequestConditions;
                if (this.DestTagCondition != null)
                {
                    options.DestinationConditions = new Track2Models.BlobRequestConditions();
                    options.DestinationConditions.TagConditions = DestTagCondition;
                }
                Track2Models.CopyFromUriOperation copyId = await destBlob.StartCopyFromUriAsync(srcUri, options, this.CmdletCancellationToken).ConfigureAwait(false);

                this.OutputStream.WriteVerbose(taskId, String.Format(Resources.CopyDestinationBlobPending, destBlob.Name, destBlob.BlobContainerName, copyId));
                OutputStream.WriteObject(taskId, new AzureStorageBlob(destBlob, destChannel.StorageContext, properties, options: ClientOptions));
            }
        }
        /// <summary>
        /// Check for the latest status of the copy operation.
        /// </summary>
        /// <param name="cancellationToken">
        /// Optional <see cref="CancellationToken"/> to propagate
        /// notifications that the operation should be cancelled.
        /// </param>
        /// <param name="async" />
        /// <returns>The <see cref="Response"/> with the status update.</returns>
        private async Task <Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
        {
            // Short-circuit when already completed (which improves mocking
            // scenarios that won't have a client).
            if (HasCompleted)
            {
                return(GetRawResponse());
            }

            // Use our original CancellationToken if the user didn't provide one
            if (cancellationToken == default)
            {
                cancellationToken = _cancellationToken;
            }

            // Get the latest status
            Response <BlobProperties> update = async
                ? await _client.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false)
                : _client.GetProperties(cancellationToken: cancellationToken);

            // Check if the operation is no longer running
            if (Id != update.Value.CopyId ||
                update.Value.CopyStatus != CopyStatus.Pending)
            {
                _hasCompleted = true;
            }

            // Check if the operation succeeded
            if (Id == update.Value.CopyId &&
                update.Value.CopyStatus == CopyStatus.Success)
            {
                _value = update.Value.ContentLength;
            }
            // Check if the operation aborted or failed
            if (Id == update.Value.CopyId &&
                (update.Value.CopyStatus == CopyStatus.Aborted ||
                 update.Value.CopyStatus == CopyStatus.Failed))
            {
                _value = 0;
            }

            // Save this update as the latest raw response indicating the state
            // of the copy operation
            Response response = update.GetRawResponse();

            _rawResponse = response;
            return(response);
        }
Exemplo n.º 9
0
        public async Task SetLegalHoldAsync()
        {
            // Arrange
            await using DisposingImmutableStorageWithVersioningContainer vlwContainer = await GetTestVersionLevelWormContainer(TestConfigOAuth);

            BlobBaseClient blob = await GetNewBlobClient(vlwContainer.Container);

            // Act
            Response <BlobLegalHoldResult> response = await blob.SetLegalHoldAsync(true);

            // Assert
            Assert.IsTrue(response.Value.HasLegalHold);

            // Validate that we are correctly deserializing Get Properties response.
            // Act
            Response <BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();

            // Assert
            Assert.IsTrue(propertiesResponse.Value.HasLegalHold);

            // Validate we are correctly deserializing Blob Items.
            // Act
            List <BlobItem> blobItems = new List <BlobItem>();

            await foreach (BlobItem blobItem in vlwContainer.Container.GetBlobsAsync(traits: BlobTraits.LegalHold))
            {
                blobItems.Add(blobItem);
            }

            // Assert
            Assert.AreEqual(1, blobItems.Count);
            Assert.IsTrue(blobItems[0].Properties.HasLegalHold);

            // Validate we are correctly deserialzing Get Blob response.
            // Act
            Response <BlobDownloadInfo> downloadResponse = await blob.DownloadAsync();

            // Assert
            Assert.IsTrue(downloadResponse.Value.Details.HasLegalHold);

            // Act
            response = await blob.SetLegalHoldAsync(false);

            // Assert
            Assert.IsFalse(response.Value.HasLegalHold);
        }
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            // Try to get the blobUri from the request parameters:
            string blobUri = req?.Query["blobUri"];

            blobUri ??= HttpUtility.UrlDecode(blobUri);

            // Replace with the blobUri from the body, if it exists:
            using (var sr = new StreamReader(req?.Body))
            {
                string requestBody = await sr.ReadToEndAsync().ConfigureAwait(false);

                dynamic data = JsonConvert.DeserializeObject(requestBody);
                blobUri ??= data?.blobUri;
            }

            // If we have a blobUri, try to get the ContentLength:
            long contentLength = -1;

            try
            {
                if (Uri.TryCreate(blobUri, UriKind.Absolute, out Uri requestedUri))
                {
                    var blobBaseClient = new BlobBaseClient(requestedUri, _tokenCredential);
                    var props          = await blobBaseClient.GetPropertiesAsync().ConfigureAwait(false);

                    contentLength = props.Value.ContentLength;
                }
            }
            catch (Azure.RequestFailedException e) when(e.ErrorCode == "AuthorizationPermissionMismatch")
            {
                return(new BadRequestObjectResult(new { error = $"BlobBaseClient.GetPropertiesAsync requires the identity principal to have role 'Storage Blob Data Reader' on resource (file, container, resource-group, or subscription).\n\n\nException.Message:\n\n{e.Message}" }));
            }
            catch (Exception e)
            {
                return(new BadRequestObjectResult(new { error = $"Exception.Message:\n{e.Message}\n\nInnerException.Message:\n{e.InnerException?.Message}" }));
            }

            log.LogInformation($"BlobUri: {blobUri}, ContentLength: {contentLength}");

            return(new OkObjectResult(contentLength));
        }
Exemplo n.º 11
0
        public async Task SetLegalHoldAsync()
        {
            // Arrange
            BlobBaseClient blob = await GetNewBlobClient(_containerClient);

            // Act
            Response <BlobLegalHoldResult> response = await blob.SetLegalHoldAsync(true);

            // Assert
            Assert.IsTrue(response.Value.HasLegalHold);

            // Validate that we are correctly deserializing Get Properties response.
            // Act
            Response <BlobProperties> propertiesResponse = await blob.GetPropertiesAsync();

            // Assert
            Assert.IsTrue(propertiesResponse.Value.HasLegalHold);

            // Validate we are correctly deserializing Blob Items.
            // Act
            List <BlobItem> blobItems = new List <BlobItem>();

            await foreach (BlobItem blobItem in _containerClient.GetBlobsAsync(traits: BlobTraits.LegalHold, prefix: blob.Name))
            {
                blobItems.Add(blobItem);
            }

            // Assert
            Assert.AreEqual(1, blobItems.Count);
            Assert.IsTrue(blobItems[0].Properties.HasLegalHold);

            // Validate we are correctly deserialzing Get Blob response.
            // Act
            Response <BlobDownloadInfo> downloadResponse = await blob.DownloadAsync();

            // Assert
            Assert.IsTrue(downloadResponse.Value.Details.HasLegalHold);

            // Act
            response = await blob.SetLegalHoldAsync(false);

            // Assert
            Assert.IsFalse(response.Value.HasLegalHold);
        }
Exemplo n.º 12
0
        private async Task <bool> ExistsAsync(string fullPath, CancellationToken cancellationToken = default)
        {
            (BlobContainerClient container, string path) = await GetPartsAsync(fullPath, true).ConfigureAwait(false);

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

            BlobBaseClient client = container.GetBlobBaseClient(path);

            try
            {
                await client.GetPropertiesAsync(cancellationToken : cancellationToken).ConfigureAwait(false);
            }
            catch (RequestFailedException ex) when(ex.ErrorCode == "BlobNotFound")
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 13
0
        public async Task DeleteImmutibilityPolicyAsync()
        {
            // Arrange
            BlobBaseClient blob = await GetNewBlobClient(_containerClient);

            BlobImmutabilityPolicy immutabilityPolicy = new BlobImmutabilityPolicy
            {
                ExpiresOn  = Recording.UtcNow.AddSeconds(5),
                PolicyMode = BlobImmutabilityPolicyMode.Unlocked
            };

            await blob.SetImmutabilityPolicyAsync(immutabilityPolicy);

            // Act
            await blob.DeleteImmutabilityPolicyAsync();

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

            Assert.IsNull(propertiesResponse.Value.ImmutabilityPolicy.ExpiresOn);
            Assert.IsNull(propertiesResponse.Value.ImmutabilityPolicy.PolicyMode);
        }
Exemplo n.º 14
0
        public async Task DeleteImmutibilityPolicyAsync()
        {
            // Arrange
            await using DisposingImmutableStorageWithVersioningContainer vlwContainer = await GetTestVersionLevelWormContainer(TestConfigOAuth);

            BlobBaseClient blob = await GetNewBlobClient(vlwContainer.Container);

            BlobImmutabilityPolicy immutabilityPolicy = new BlobImmutabilityPolicy
            {
                ExpiresOn  = Recording.UtcNow.AddSeconds(5),
                PolicyMode = BlobImmutabilityPolicyMode.Unlocked
            };

            await blob.SetImmutabilityPolicyAsync(immutabilityPolicy);

            // Act
            await blob.DeleteImmutabilityPolicyAsync();

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

            Assert.IsNull(propertiesResponse.Value.ImmutabilityPolicy.ExpiresOn);
            Assert.IsNull(propertiesResponse.Value.ImmutabilityPolicy.PolicyMode);
        }
Exemplo n.º 15
0
        private async Task CopyFromUri(long taskId, IStorageBlobManagement destChannel, Uri srcUri, BlobBaseClient destBlob)
        {
            bool destExist = true;

            Track2Models.BlobType?      destBlobType = Util.GetBlobType(destBlob);
            Track2Models.BlobProperties properties   = null;

            try
            {
                properties   = (await destBlob.GetPropertiesAsync(this.BlobRequestConditions, cancellationToken: this.CmdletCancellationToken).ConfigureAwait(false)).Value;
                destBlobType = properties.BlobType;
            }
            catch (global::Azure.RequestFailedException e) when(e.Status == 404)
            {
                destExist = false;
            }
            if (destBlobType != null)
            {
                ValidateBlobTier(Util.convertBlobType_Track2ToTrack1(destBlobType), null, standardBlobTier, rehydratePriority);
            }

            if (!destExist || this.ConfirmOverwrite(srcUri.AbsoluteUri.ToString(), destBlob.Uri.ToString()))
            {
                Track2Models.BlobCopyFromUriOptions options = new Track2Models.BlobCopyFromUriOptions();

                // The Blob Type and Blob Tier must match, since already checked before
                if (standardBlobTier != null || rehydratePriority != null)
                {
                    options.AccessTier        = Util.ConvertAccessTier_Track1ToTrack2(standardBlobTier);
                    options.RehydratePriority = Util.ConvertRehydratePriority_Track1ToTrack2(rehydratePriority);
                }
                options.SourceConditions = this.BlobRequestConditions;

                BlockBlobClient             srcBlockblob  = new BlockBlobClient(srcUri, ClientOptions);
                Track2Models.BlobProperties srcProperties = srcBlockblob.GetProperties(cancellationToken: this.CmdletCancellationToken).Value;

                //Prepare progress handler
                string           activity        = String.Format("Copy Blob {0} to {1}", srcBlockblob.Name, destBlob.Name);
                string           status          = "Prepare to Copy Blob";
                ProgressRecord   pr              = new ProgressRecord(OutputStream.GetProgressId(taskId), activity, status);
                IProgress <long> progressHandler = new Progress <long>((finishedBytes) =>
                {
                    if (pr != null)
                    {
                        // Size of the source file might be 0, when it is, directly treat the progress as 100 percent.
                        pr.PercentComplete   = 0 == srcProperties.ContentLength ? 100 : (int)(finishedBytes * 100 / srcProperties.ContentLength);
                        pr.StatusDescription = string.Format("Percent: {0}%.", pr.PercentComplete);
                        Console.WriteLine(finishedBytes);
                        this.OutputStream.WriteProgress(pr);
                    }
                });

                switch (destBlobType)
                {
                case Track2Models.BlobType.Block:

                    BlockBlobClient destBlockBlob = (BlockBlobClient)Util.GetTrack2BlobClientWithType(destBlob, Channel.StorageContext, Track2Models.BlobType.Block, ClientOptions);

                    Track2Models.CommitBlockListOptions commitBlockListOptions = new Track2Models.CommitBlockListOptions();
                    commitBlockListOptions.HttpHeaders                    = new Track2Models.BlobHttpHeaders();
                    commitBlockListOptions.HttpHeaders.ContentType        = srcProperties.ContentType;
                    commitBlockListOptions.HttpHeaders.ContentHash        = srcProperties.ContentHash;
                    commitBlockListOptions.HttpHeaders.ContentEncoding    = srcProperties.ContentEncoding;
                    commitBlockListOptions.HttpHeaders.ContentLanguage    = srcProperties.ContentLanguage;
                    commitBlockListOptions.HttpHeaders.ContentDisposition = srcProperties.ContentDisposition;
                    commitBlockListOptions.Metadata = srcProperties.Metadata;
                    try
                    {
                        commitBlockListOptions.Tags = srcBlockblob.GetTags(cancellationToken: this.CmdletCancellationToken).Value.Tags;
                    }
                    catch (global::Azure.RequestFailedException e) when(e.Status == 403 || e.Status == 404 || e.Status == 401)
                    {
                        if (!this.Force && !OutputStream.ConfirmAsync("Can't get source blob Tags, so source blob tags won't be copied to dest blob. Do you want to continue the blob copy?").Result)
                        {
                            return;
                        }
                    }

                    long     blockLength = GetBlockLength(srcProperties.ContentLength);
                    string[] blockIDs    = GetBlockIDs(srcProperties.ContentLength, blockLength, destBlockBlob.Name);
                    long     copyoffset  = 0;
                    progressHandler.Report(copyoffset);
                    foreach (string id in blockIDs)
                    {
                        long blocksize = blockLength;
                        if (copyoffset + blocksize > srcProperties.ContentLength)
                        {
                            blocksize = srcProperties.ContentLength - copyoffset;
                        }
                        destBlockBlob.StageBlockFromUri(srcUri, id, new global::Azure.HttpRange(copyoffset, blocksize), null, null, null, cancellationToken: this.CmdletCancellationToken);
                        copyoffset += blocksize;
                        progressHandler.Report(copyoffset);
                    }
                    destBlockBlob.CommitBlockList(blockIDs, commitBlockListOptions, this.CmdletCancellationToken);

                    break;

                case Track2Models.BlobType.Page:
                case Track2Models.BlobType.Append:
                default:
                    throw new ArgumentException(string.Format("The cmdlet currently only support souce blob and destination blob are both block blob. The dest blob type is {0}.", destBlobType));
                }

                OutputStream.WriteObject(taskId, new AzureStorageBlob(destBlob, destChannel.StorageContext, null, options: ClientOptions));
            }
        }