/// <inheritdoc />
        public async Task <IEnumerable <byte> > DownloadFileAsync(
            string absolutePath,
            CancellationToken cancellationToken)
        {
            byte[] toReturn = null;

            Uri       uri       = new Uri(absolutePath, UriKind.Absolute);
            CloudFile cloudFile = new CloudFile(uri, this.sourceStorageCredentials);

            await cloudFile.FetchAttributesAsync().ConfigureAwait(false);

            toReturn = new byte[cloudFile.Properties.Length];

            this.loggerWrapper.Debug(
                $"Downloading \"{absolutePath}\" ({toReturn.Length} " +
                $"byte(s))...");

            await cloudFile.DownloadToByteArrayAsync(toReturn, 0)
            .ConfigureAwait(false);

            this.loggerWrapper.Info(
                $"Downloaded \"{absolutePath}\" ({toReturn.Length} byte(s)).");

            return(toReturn);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads the BLOB storing with the given key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public byte[] ReadBlob(string key)
        {
            CloudFile cloudFile = GetCloudFileFromKey(key);

            try
            {
                if (!cloudFile.ExistsAsync().Result)
                {
                    return(null);
                }
                cloudFile.FetchAttributesAsync().Wait();

                byte[] content = new byte[cloudFile.Properties.Length];
                cloudFile.DownloadToByteArrayAsync(content, 0).Wait();

                return(content);
            }
            catch (Exception ex)
            {
                throw new LendsumException(S.Invariant($"The filename {key} cannot be read in {config.AzureSharedReference} "), ex);
            }
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public byte[] ReadAllBytes(string fileName, string[] path)
        {
            #region validation

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            #endregion

            CloudFileDirectory cloudFileDirectory = CloudFileShare.GetDirectoryReference(path: path);

            CloudFile cloudFile = cloudFileDirectory.GetFileReference(fileName);

            TaskUtilities.ExecuteSync(cloudFile.FetchAttributesAsync());

            var cloudFileBytes = new byte[cloudFile.Properties.Length];

            TaskUtilities.ExecuteSync(cloudFile.DownloadToByteArrayAsync(cloudFileBytes, 0));

            return(cloudFileBytes);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Download data to the stream.
 /// </summary>
 /// <param name="file">The cloud file.</param>
 /// <param name="target">The byte array where to write to.</param>
 /// <param name="index">The offset index.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The async task.</returns>
 public async Task <int> DownloadAsync(CloudFile file, byte[] target, int index, CancellationToken cancellationToken)
 {
     return(await file.DownloadToByteArrayAsync(target, index, cancellationToken));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Download data to the stream.
 /// </summary>
 /// <param name="file">The cloud file.</param>
 /// <param name="target">The byte array where to write to.</param>
 /// <param name="index">The offset index.</param>
 /// <returns>The async task.</returns>
 public async Task <int> DownloadAsync(CloudFile file, byte[] target, int index)
 {
     return(await file.DownloadToByteArrayAsync(target, index));
 }
        public async Task <File> GetFileAsync(
            FileMetaData fileMetaData,
            CancellationToken cancellationToken)
        {
            File toReturn = null;

            if (fileMetaData == null)
            {
                throw new ArgumentNullException(nameof(fileMetaData));
            }

            Uri location = fileMetaData.Location;

            this.loggerProvider.Info($"{nameof(location)} = {location}");

            CloudFile cloudFile = new CloudFile(
                location,
                this.storageCredentials);

            this.loggerProvider.Debug(
                $"Checking if {nameof(CloudFile)} at {location} exists...");

            bool exists = await cloudFile.ExistsAsync().ConfigureAwait(false);

            this.loggerProvider.Info($"{nameof(exists)} = {exists}");

            if (exists)
            {
                FileProperties fileProperties = cloudFile.Properties;

                string contentType = fileProperties.ContentType;

                // It appears that, the content type, whilst *available* on a
                // FileShare file, doesn't tend to be updated by file upload
                // clients as it should.
                // This is where we pull the content type from usually.
                // Therefore, if this is required, we may need to run a script
                // over existing files, or remember to supply the content type
                // (perhaps with azcopy) on initialisation of the storage.
                if (string.IsNullOrEmpty(contentType))
                {
                    this.loggerProvider.Warning(
                        $"Default content type (\"{DefaultContentType}\") " +
                        $"being used, as no content type available for this " +
                        $"particular file.");

                    contentType = DefaultContentType;
                }
                else
                {
                    this.loggerProvider.Info(
                        $"{nameof(contentType)} = \"{contentType}\"");
                }

                long length = fileProperties.Length;

                this.loggerProvider.Info(
                    $"File at \"{location}\" exists " +
                    $"({nameof(contentType)} = \"{contentType}\", " +
                    $"{nameof(length)} = {length}). Downloading content...");

                byte[] contentBytes = new byte[length];

                await cloudFile.DownloadToByteArrayAsync(
                    contentBytes,
                    0,
                    AccessCondition.GenerateEmptyCondition(),
                    this.fileRequestOptions,
                    this.operationContext,
                    cancellationToken)
                .ConfigureAwait(false);

                this.loggerProvider.Info(
                    $"{length} byte(s) downloaded. Stuffing results into a " +
                    $"{nameof(File)} instance, and returning.");

                string fileName = location.Segments.Last();

                toReturn = new File()
                {
                    ContentType  = contentType,
                    ContentBytes = contentBytes,
                    FileName     = fileName,
                };
            }
            else
            {
                this.loggerProvider.Warning(
                    $"File at \"{location}\" does not exist. Returning null.");
            }

            return(toReturn);
        }