关于在.NET Core中配置AWS开发工具包以将文件上传到S3存储桶的this指南。
我的app.settings.json文件包含:
{
"AWS": {
"Region": "us-west-1",
"AccessKey": "access_key",
"SecretKey": "secret_key"
}
...
}
这是我的StartUp类:
public class Startup
{
public IContainer ApplicationContainer { get; private set; }
public IConfiguration Configuration { get; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMvc();
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
services.AddAWSService<IAmazonS3>();
}
}
我相信这意味着我可以将客户端对象注入一个将使用app.settings值的类。我试图注入对象并像这样进行S3调用:
public class FileUploader
{
private readonly IAmazonS3 client;
public FileUploader(IAmazonS3 client)
{
this.client = client;
}
public async Task Upload()
{
var request = new PutObjectRequest
{
BucketName = "bucketName",
Key = "Test",
FilePath = "test_file.txt",
ContentType = "text/plain"
};
var cancellationToken = new CancellationToken();
var response = await client.PutObjectAsync(request, cancellationToken);
}
}
目前,当我尝试执行此操作时,应用程序会在尝试注入S3Client时挂起。最初,我没有尝试注入S3Client,而是在我的FileUploaded类中实例化它。我之所以收到错误,是因为我没有指定AccessKey等,而且我找不到设置方法。
我做错了什么?