/// <summary>
        /// Adds AWS S3 Storage as the provider for Piranha CMS using specified options.
        /// </summary>
        /// <param name="services">The current service collection.</param>
        /// <param name="s3StorageOptions">Configuration items for <see cref="S3Storage"/>.</param>
        /// <param name="awsOptions">Configuration for the AWS S3 service client.</param>
        /// <param name="scope">The optional service scope. Default is singleton.</param>
        /// <returns>Returns back the <see cref="IServiceCollection"/> to continue the fluent system of service configuration.</returns>
        public static IServiceCollection AddPiranhaS3Storage(
            this IServiceCollection services,
            PiranhaS3StorageOptions s3StorageOptions,
            AWSOptions awsOptions,
            ServiceLifetime scope = ServiceLifetime.Singleton
            )
        {
            // DI , adding AWS Credentials if in service chain
            Func <IServiceProvider, object> factory = new S3StorageFactory(s3StorageOptions, awsOptions).CreateS3Storage;

            services.Add(new ServiceDescriptor(typeof(IStorage), factory, scope));
            return(services);
        }
 /// <summary>
 /// Creates a new <see cref="S3StorageSession"/> with a specified configuration.
 /// </summary>
 /// <param name="storageOptions"><see cref="PiranhaS3StorageOptions"/> used to configure the Piranda S3 storage.</param>
 /// <param name="awsOptions">The <see cref="AWSOptions"/> used to create the S3 service client.</param>
 /// <param name="logger">Namespace <see cref="ILogger"/> used for logging.</param>
 public S3StorageSession(PiranhaS3StorageOptions storageOptions, AWSOptions awsOptions, ILogger logger)
 {
     StorageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
     if (awsOptions != null)
     {
         S3Client = awsOptions.CreateServiceClient <IAmazonS3>();
     }
     else
     {
         S3Client = new AmazonS3Client();
     }
     Logger = logger;
 }
        /// <summary>
        /// Creates a <see cref="S3Storage"/> provider searching the service provider
        /// for configuration.
        /// </summary>
        /// <param name="provider">The dependency injection provider.</param>
        /// <returns><see cref="S3Storage"/> to be added to DI.</returns>
        internal S3Storage CreateS3Storage(IServiceProvider provider)
        {
            var loggerFactory = provider.GetService <ILoggerFactory>();
            var logger        = loggerFactory?.CreateLogger("Flagscript.PiranhaCms.Aws.S3Storage");

            // Obtain S3 Storage Options
            var s3StorageOptions = S3StorageOptions ?? provider.GetService <PiranhaS3StorageOptions>();

            if (s3StorageOptions == null)
            {
                var configuration = provider.GetService <IConfiguration>();
                if (configuration != null)
                {
                    s3StorageOptions = configuration.GetPiranhaS3StorageOptions();
                    if (s3StorageOptions != null)
                    {
                        logger?.LogInformation("Found Piranha S3 Options in IConfiguration");
                    }
                    else
                    {
                        s3StorageOptions = new PiranhaS3StorageOptions();
                    }
                }
            }

            // If storage configuration is still null, initialize an empty configuration
            // for environment variable fallback.
            if (s3StorageOptions == null)
            {
                s3StorageOptions = new PiranhaS3StorageOptions();
            }


            // Obtain Aws Options
            var awsOptions = AwsOptions ?? provider.GetService <AWSOptions>();

            if (awsOptions == null)
            {
                var configuration = provider.GetService <IConfiguration>();
                if (configuration != null)
                {
                    awsOptions = configuration.GetAWSOptions();
                    if (awsOptions != null)
                    {
                        logger?.LogInformation("Found AWS options in IConfiguration");
                    }
                }
            }

            return(CreateS3Storage(logger, s3StorageOptions, awsOptions));
        }
        /// <summary>
        /// Constructs an <see cref="PiranhaS3StorageOptions"/> class with the options
        /// specifed in the "Flagscript.PiranhaCms.Aws.S3Storage" section in the IConfiguration object.
        /// </summary>
        /// <param name="config"><see cref="IConfiguration"/> to check</param>
        /// <param name="configSection">The config section to extract <see cref="PiranhaS3StorageOptions"/> from.</param>
        /// <returns>The <see cref="PiranhaS3StorageOptions"/> containing the values set in configuration system.</returns>
        public static PiranhaS3StorageOptions GetPiranhaS3StorageOptions(this IConfiguration config, string configSection)
        {
            var options = new PiranhaS3StorageOptions();

            // Find the config section
            IConfiguration section;

            if (string.IsNullOrEmpty(configSection))
            {
                section = config;
            }
            else
            {
                section = config.GetSection(configSection);
            }

            // Not found
            if (section == null)
            {
                return(options);
            }

            // Extract Settings
            if (!string.IsNullOrEmpty(section["BucketName"]))
            {
                options.BucketName = section["BucketName"];
            }
            if (!string.IsNullOrEmpty(section["KeyPrefix"]))
            {
                options.KeyPrefix = section["KeyPrefix"];
            }
            if (!string.IsNullOrEmpty(section["PublicUrlRoot"]))
            {
                options.PublicUrlRoot = section["PublicUrlRoot"];
            }

            // Return options
            return(options);
        }
コード例 #5
0
 /// <summary>
 /// Creates a new <see cref="S3Storage"/> with a specified configuration.
 /// </summary>
 /// <param name="storageOptions"><see cref="PiranhaS3StorageOptions"/> used to configure the Piranda S3 storage.</param>
 /// <param name="awsOptions">The <see cref="AWSOptions"/> used to create the S3 service client.</param>
 /// <param name="logger">Namespace <see cref="ILogger"/> used for logging.</param>
 internal S3Storage(PiranhaS3StorageOptions storageOptions, AWSOptions awsOptions, ILogger logger)
 {
     StorageOptions = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
     AwsOptions     = awsOptions;
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="s3StorageOptions">
 /// The <see cref="S3StorageOptions"/> used to override the default options added using
 /// <see cref="PiranhaS3ServiceCollectionExtensions.AddPiranhaS3StorageOptions"/>.
 /// </param>
 /// <param name="awsOptions">
 /// The <see cref="AWSOptions"/> used to create the service client overriding
 /// the default AWS options added using <see cref="ServiceCollectionExtensions.AddDefaultAWSOptions(IServiceCollection, AWSOptions)"/>.
 /// </param>
 internal S3StorageFactory(PiranhaS3StorageOptions s3StorageOptions, AWSOptions awsOptions)
 {
     S3StorageOptions = s3StorageOptions;
     AwsOptions       = awsOptions;
 }
        /// <summary>
        /// Creates the AWS service client that implements the service client interface. The AWSOptions object
        /// will be searched for in the IServiceProvider.
        /// </summary>
        /// <param name="logger">Flagscript.PiranhaCms.Aws.S3Storage category logger.</param>
        /// <param name="s3StorageOptions"><see cref="PiranhaS3StorageOptions"/> used to configure the <see cref="S3Storage"/>.</param>
        /// <param name="awsOptions">The <see cref="AWSOptions"/> used to configure the S3 service client.</param>
        /// <returns>The configured <see cref="S3Storage"/>.</returns>
        internal S3Storage CreateS3Storage(ILogger logger, PiranhaS3StorageOptions s3StorageOptions, AWSOptions awsOptions)
        {
            var finalS3StorateOptions = new PiranhaS3StorageOptions
            {
                BucketName    = s3StorageOptions.BucketName,
                KeyPrefix     = s3StorageOptions.KeyPrefix,
                PublicUrlRoot = s3StorageOptions.PublicUrlRoot
            };

            // Obtain/Validate S3StorageOptions BucketName
            if (string.IsNullOrWhiteSpace(finalS3StorateOptions.BucketName))
            {
                finalS3StorateOptions.BucketName = Environment.GetEnvironmentVariable(PiranhaS3StorageOptions.BucketEnvironmentVariable);

                if (string.IsNullOrEmpty(finalS3StorateOptions.BucketName))
                {
                    throw new FlagscriptConfigurationException("Piranha S3 BucketName not configured");
                }
                else
                {
                    logger?.LogInformation("Piranha S3 BucketName found in environment variables");
                }
            }

            // Obtain/Default S3StorageOptions KeyPrefix
            if (string.IsNullOrWhiteSpace(finalS3StorateOptions.KeyPrefix))
            {
                finalS3StorateOptions.KeyPrefix = Environment.GetEnvironmentVariable(PiranhaS3StorageOptions.KeyPrefixEnvironmentVariable);

                if (string.IsNullOrEmpty(finalS3StorateOptions.KeyPrefix))
                {
                    logger?.LogInformation($"Piranha S3 KeyPrefix configured to default => {PiranhaS3StorageOptions.KeyPrefixDefault}");
                }
                else
                {
                    logger?.LogInformation("Piranha S3 KeyPrefix found in environment variables");
                }

                // Default if not found
                if (string.IsNullOrWhiteSpace(finalS3StorateOptions.KeyPrefix))
                {
                    finalS3StorateOptions.KeyPrefix = "uploads";
                }
            }

            // Obtain/Validate S3StorageOptions PublicUrlRoot
            if (string.IsNullOrWhiteSpace(finalS3StorateOptions.PublicUrlRoot))
            {
                finalS3StorateOptions.PublicUrlRoot = Environment.GetEnvironmentVariable(PiranhaS3StorageOptions.UrlRootEnvironmentVariable);

                if (string.IsNullOrEmpty(finalS3StorateOptions.PublicUrlRoot))
                {
                    throw new FlagscriptConfigurationException("Piranha S3 PublicUrlRoot not configured");
                }
                else
                {
                    logger?.LogInformation("Piranha S3 PublicUrlRoot found in environment variables");
                }
            }

            // Validate we have a valid public uri root.
            if (!Uri.IsWellFormedUriString(finalS3StorateOptions.PublicUrlRoot, UriKind.Absolute))
            {
                throw new FlagscriptConfigurationException("Piranha S3 PublicUrlRoot is not a valid endpoint");
            }

            // Validate we have a valid public uri prefix.
            if (!Uri.IsWellFormedUriString(finalS3StorateOptions.PublicUrlPrefix, UriKind.Absolute))
            {
                throw new FlagscriptConfigurationException("Piranha S3 PublicUrlPrefix is not a valid endpoint");
            }

            return(new S3Storage(finalS3StorateOptions, awsOptions, logger));
        }
 /// <summary>
 /// Adds AWS S3 Storage as the provider for Piranha CMS using specified options.
 /// </summary>
 /// <param name="services">The current service collection.</param>
 /// <param name="s3StorageOptions">Configuration items for <see cref="S3Storage"/>.</param>
 /// <param name="scope">The service scope to use.</param>
 /// <returns>Returns back the <see cref="IServiceCollection"/> to continue the fluent system of service configuration.</returns>
 public static IServiceCollection AdPiranhadS3Storage(this IServiceCollection services, PiranhaS3StorageOptions s3StorageOptions, ServiceLifetime scope)
 {
     return(AddPiranhaS3Storage(services, s3StorageOptions, null, scope));
 }
 /// <summary>
 /// Adds AWS S3 Storage as the provider for Piranha CMS using specified options as a singleton.
 /// </summary>
 /// <param name="services">The current service collection.</param>
 /// <param name="s3StorageOptions">Configuration items for <see cref="S3Storage"/>.</param>
 /// <returns>Returns back the <see cref="IServiceCollection"/> to continue the fluent system of service configuration.</returns>
 public static IServiceCollection AdPiranhadS3Storage(this IServiceCollection services, PiranhaS3StorageOptions s3StorageOptions)
 {
     return(AddPiranhaS3Storage(services, s3StorageOptions, null, ServiceLifetime.Singleton));
 }
 /// <summary>
 /// Adds a <see cref="PiranhaS3StorageOptions"/> object to the dependency injection.
 /// framework.
 /// </summary>
 /// <param name="services">The current service collection.</param>
 /// <param name="options">A <see cref="PiranhaS3StorageOptions"/> used to configure <see cref="S3Storage"/>.</param>
 /// <returns>Returns back the <see cref="IServiceCollection"/> to continue the fluent system of service configuration.</returns>
 public static IServiceCollection AddPiranhaS3StorageOptions(this IServiceCollection services, PiranhaS3StorageOptions options)
 {
     services.Add(new ServiceDescriptor(typeof(PiranhaS3StorageOptions), options));
     return(services);
 }