private long GetBlobLength(bool refetch = false)
        {
            if (refetch || _blobLength <= 0)
            {
                if (_pageBlobClient.Exists())
                {
                    BlobProperties props = _pageBlobClient.GetProperties();
                    _blobLength = props.Metadata.TryGetValue("actuallength", out string actualLength)
                                                ? long.Parse(actualLength)
                                                : props.ContentLength;
                }
            }

            return(_blobLength);
        }
Esempio n. 2
0
        /// <summary>
        /// Download blob with blob Uri
        /// If blob is on a managed disk account, and server return 401 and requires a bearer token besides Sas Uri to download,
        /// will try to generate a bearer token and download again with both Sas Uri and bearer token.
        /// </summary>
        /// <param name="blobUri"></param>
        /// <param name="fileName"></param>
        internal void GetBlobContent(string blobUri, string fileName)
        {
            BlobClientOptions blobClientOptions = this.ClientOptions;
            BlobBaseClient    blobclient        = new BlobBaseClient(new Uri(blobUri), blobClientOptions);

            Track2Models.BlobProperties blobproperties;
            if (blobclient.AccountName.ToLower().StartsWith("md-")) // managed disk account, must be page blob
            {
                blobClientOptions.Diagnostics.LoggedHeaderNames.Add("WWW-Authenticate");
                blobclient = new PageBlobClient(new Uri(blobUri), blobClientOptions);

                try
                {
                    blobproperties = blobclient.GetProperties(null, this.CmdletCancellationToken).Value;
                }
                catch (global::Azure.RequestFailedException e) when(e.Status == 401)  // need diskRP bearer token
                {
                    string audience = Util.GetAudienceFrom401ExceptionMessage(e.Message);

                    if (audience != null)
                    {
                        WriteDebugLog(string.Format("Need bearer token with audience {0} to access the blob, so will generate bearer token and resend the request.", audience));
                        AzureSessionCredential customerToken = new AzureSessionCredential(DefaultContext, customAudience: audience);
                        blobclient = new PageBlobClient(new Uri(blobUri), customerToken, this.ClientOptions);
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
            else // need check blob type for none md account
            {
                blobproperties = blobclient.GetProperties(null, this.CmdletCancellationToken).Value;

                blobclient = Util.GetTrack2BlobClient(new Uri(blobUri), null, blobClientOptions, blobproperties.BlobType);
            }
            GetBlobContent(blobclient, fileName);
        }