コード例 #1
0
        private static string GetBlobName(UploadFileProperties uploadFileProperties)
        {
            var fileName = uploadFileProperties.FileName;

            if (!uploadFileProperties.EnforceNameUniqueness)
            {
                if (string.IsNullOrEmpty(fileName))
                {
                    throw new ArgumentException("File name should be set");
                }

                return(fileName);
            }

            try
            {
                if (!string.IsNullOrEmpty(fileName))
                {
                    // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
                    Path.GetFileName(fileName);
                }
            }
            catch (Exception)
            {
                throw new ArgumentException("Invalid file name for upload");
            }

            return($"{Guid.NewGuid().ToString().ToLower()}_{fileName}");
        }
コード例 #2
0
        public async Task <string> UploadAsync(string containerName, UploadFileProperties uploadFileProperties, Stream streamToUpload)
        {
            var containerReference = GetBlobContainer(containerName);
            var blobName           = GetBlobName(uploadFileProperties);

            var blobReference = containerReference.GetBlockBlobReference(blobName);

            blobReference.Metadata.Add(FileNameMetadata, uploadFileProperties.FileName);
            blobReference.Properties.ContentType        = uploadFileProperties.ContentType;
            blobReference.Properties.ContentDisposition = uploadFileProperties.ContentDisposition;

            Func <Task <string> > uploadDelegate = async() =>
            {
                await blobReference.UploadFromStreamAsync(streamToUpload);

#if NET452
                Log.Information("File {FileId} was uploaded to the storage", blobName);
#endif

#if NETSTANDARD2_0
                logger.LogInformation("File {FileId} was uploaded to the storage", blobName);
#endif
                return(blobName);
            };

            try
            {
                // try to upload file assuming container exists
                return(await uploadDelegate());
            }
            catch (StorageException e)
            {
                // case when container was not created yet
                if (e.RequestInformation.HttpStatusCode == 404)
                {
#if NET452
                    Log.Warning(e, "Exception while uploading a file");
#endif

#if NETSTANDARD2_0
                    logger.LogWarning(e, "Exception while uploading a file");
#endif
                    await EnsureContainerExists(containerReference);

                    return(await uploadDelegate());
                }

#if NET452
                Log.Error(e, "Failed to upload a file to the storage");
#endif

#if NETSTANDARD2_0
                logger.LogError(e, "Failed to upload a file to the storage");
#endif
                throw;
            }
        }
コード例 #3
0
        public async Task <FileStorageWriteStream> GetWriteStreamAsync(string containerName, UploadFileProperties uploadFileProperties)
        {
            var containerReference = GetBlobContainer(containerName);
            var blobName           = GetBlobName(uploadFileProperties);

            var blobReference = containerReference.GetBlockBlobReference(blobName);

            blobReference.Metadata.Add(FileNameMetadata, uploadFileProperties.FileName);
            blobReference.Properties.ContentType        = uploadFileProperties.ContentType;
            blobReference.Properties.ContentDisposition = uploadFileProperties.ContentDisposition;

            Func <Task <FileStorageWriteStream> > openStreamDelegate = async() =>
            {
                var stream = await blobReference.OpenWriteAsync();

#if NET452
                Log.Information("File {FileId} write stream to the storage was opened", blobName);
#endif

#if NETSTANDARD2_0
                logger.LogInformation("File {FileId} write stream to the storage was opened", blobName);
#endif
                return(new FileStorageWriteStream {
                    Id = blobName, Stream = stream
                });
            };

            try
            {
                // try to open stream assuming container exists
                return(await openStreamDelegate());
            }
            catch (StorageException e)
            {
                // case when container was not created yet
                if (e.RequestInformation.HttpStatusCode == 404)
                {
#if NET452
                    Log.Warning(e, "Exception while opening a write stream to the storage");
#endif

#if NETSTANDARD2_0
                    logger.LogWarning(e, "Exception while opening a write stream to the storage");
#endif

                    await EnsureContainerExists(containerReference);

                    return(await openStreamDelegate());
                }

#if NET452
                Log.Error(e, "Failed to open a write stream to the storage");
#endif

#if NETSTANDARD2_0
                logger.LogError(e, "Failed to open a write stream to the storage");
#endif
                throw;
            }
        }