/// <summary> /// Returns the information associated to a blob. /// </summary> /// <param name="blobPath"></param> /// <returns></returns> public async Task <BlobInfo> GetBlobInfoAsync(string blobPath) { BlobInfo result = null; CloudBlobClient blobClient = m_LazyBlobClient.Value; CloudBlob blob = new CloudBlob(GetAbsoluteBlobUri(blobPath), blobClient.Credentials); //`ExistsAsync` also refreshes properties and metadata at the same times if (await blob.ExistsAsync()) { result = AzureBlob.FromBlobProperties(GetBlobPath(blob), blob.Properties, blob.Metadata); } return(result); }
/// <summary> /// Opens a stream for reading from the blob. /// </summary> /// <param name="blobPath">The relative path, from the storage root, to the blob.</param> /// <exception cref="IdNotFoundException">The blob specified in <paramref name="blobPath"/> does not exist (<see cref="ErrorCodes.BlobNotFound"/>).</exception> /// <returns></returns> public async Task <BlobStreamDecorator> OpenBlobAsync(string blobPath) { CloudBlobClient blobClient = m_LazyBlobClient.Value; CloudBlob blob = new CloudBlob(GetAbsoluteBlobUri(blobPath), blobClient.Credentials); try { //blob properties and metadata are automatically fetched when opening the blob stream Stream blobStream = await blob.OpenReadAsync(); BlobInfo blobInfo = AzureBlob.FromBlobProperties(GetBlobPath(blob), blob.Properties, blob.Metadata); return(new BlobStreamDecorator(blobStream, blobInfo)); } catch (StorageException ex) when(ex.RequestInformation.ErrorCode == BlobErrorCodeStrings.BlobNotFound || ex.RequestInformation.ErrorCode == BlobErrorCodeStrings.ContainerNotFound) { if (ex.RequestInformation.ErrorCode == BlobErrorCodeStrings.ContainerNotFound) { m_Logger.LogWarning($"Container '{blob.Container.Name}' does not exist"); } throw new IdNotFoundException(ErrorCodes.BlobNotFound, blobPath); } }
/// <summary> /// Initializes a new instance of the class. /// </summary> /// <param name="stream"></param> /// <param name="blobInfo"></param> public BlobStreamDecorator(Stream stream, BlobInfo blobInfo) : this(stream, blobInfo.Metadata) { ContentType = blobInfo.ContentType; }