/// <summary>Gets a blob from Azure Storage as just raw bytes with metadata</summary>
        /// <param name="containerName">container name</param>
        /// <param name="blobName">blob name</param>
        /// <returns>Wrapped raw bytes with some metadata</returns>
        public RawFileWrapper GetRawBlob(string containerName, string blobName)
        {
            RawFileWrapper results = new RawFileWrapper();

            // validate input
            if (String.IsNullOrWhiteSpace(containerName) || String.IsNullOrWhiteSpace(blobName))
            {
                return(results);
            }

            containerName = containerName.Trim();
            blobName      = blobName.Trim();

            // Get a reference to a share and then create it
            BlobContainerClient container = new BlobContainerClient(this.ConnectionString, containerName);

            // check the container exists
            Response <bool> exists = container.Exists();

            if (!exists.Value)
            {
                return(results);
            }

            // set options
            BlobOpenReadOptions op = new BlobOpenReadOptions(false);

            // read the blob to an array
            BlobClient blob = container.GetBlobClient(blobName);

            using Stream stream = blob.OpenRead(op);
            results.Data        = new byte[stream.Length];
            stream.Read(results.Data, 0, results.Data.Length);
            stream.Close();

            // get the properties
            BlobProperties props = blob.GetProperties().Value;

            if (props == null)
            {
                return(results);
            }

            results.ContentType = props.ContentType;

            // get a filename
            if (props.Metadata.ContainsKey("filename"))
            {
                results.Filename = props.Metadata["filename"].ToString();
            }
            else
            {
                results.Filename = blob.Name;
            }

            return(results);
        }
        /// <inheritdoc />
        public async Task <Stream> GetFileAsync(
            VersionedInstanceIdentifier versionedInstanceIdentifier,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(versionedInstanceIdentifier, nameof(versionedInstanceIdentifier));

            BlockBlobClient blob = GetInstanceBlockBlob(versionedInstanceIdentifier);

            Stream stream = null;
            var    blobOpenReadOptions = new BlobOpenReadOptions(allowModifications: false);

            await ExecuteAsync(async() =>
            {
                stream = await blob.OpenReadAsync(blobOpenReadOptions, cancellationToken);
            });

            return(stream);
        }
        public async Task <WarehouseDayStatistics?> GetWarehouseDayStatisticsAsync(string warehouseId, DateTimeOffset day)
        {
            var blobName = GetBlobName(warehouseId, day);
            var client   = _blobContainerClient.GetBlobClient(blobName);
            var exists   = await client.ExistsAsync();

            if (!exists.Value)
            {
                return(null);
            }

            var options = new BlobOpenReadOptions(false);
            var stream  = await client.OpenReadAsync(options);

            try
            {
                return(await JsonSerializer.DeserializeAsync <WarehouseDayStatistics>(stream));
            }
            catch (JsonException je)
            {
                throw new InvalidOperationException("Could not read document that is not warehouse stats", je);
            }
        }
Beispiel #4
0
        public static async Task <Stream> TryBindStreamAsync(BlobBaseClient blob, CancellationToken cancellationToken, string eTag = null)
        {
            Stream rawStream;

            try
            {
                if (eTag != null)
                {
                    BlobOpenReadOptions readOptions = new BlobOpenReadOptions(allowModifications: false)
                    {
                        Conditions = new BlobRequestConditions()
                        {
                            IfMatch = new ETag(eTag),
                        },
                    };
                    rawStream = await blob.OpenReadAsync(readOptions, cancellationToken : cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    rawStream = await blob.OpenReadAsync(cancellationToken : cancellationToken).ConfigureAwait(false);
                }
            }
            catch (RequestFailedException exception)
            {
                // Testing generic error case since specific error codes are not available for FetchAttributes
                // (HEAD request), including OpenRead.
                if (!exception.IsNotFound())
                {
                    throw;
                }

                return(null);
            }

            return(rawStream);
        }
Beispiel #5
0
 public AzureTourAccessor(AzureTourOptions options, BlobServiceClient service, ILogger <AzureTourAccessor> logger)
 {
     _container   = service.GetBlobContainerClient(options.ContainerName);
     _readOptions = new BlobOpenReadOptions(allowModifications: false);
     _logger      = logger;
 }
Beispiel #6
0
 public AzureThumbnailAccessor(ThumbnailOptions options, BlobServiceClient service)
 {
     _options             = options;
     _container           = service.GetBlobContainerClient(options.ContainerName);
     _blobOpenReadOptions = new BlobOpenReadOptions(allowModifications: false);
 }