Use Amazon S3 from ASP.NET Core on Elastic Beanstalk¶
This recipe adds Amazon S3 object storage to an ASP.NET Core application running on Elastic Beanstalk. It uses the AWS SDK for .NET and relies on the instance profile instead of static credentials.
Prerequisites¶
- Running .NET Elastic Beanstalk environment.
- S3 bucket already created.
- Instance profile policy permitting S3 object operations.
What You'll Build¶
You will build:
- SDK registration for Amazon S3.
- A small upload example.
- A configuration pattern using environment properties for the bucket name.
Steps¶
- Set the bucket name as an environment property.
- Add the SDK package.
- Register the S3 client.
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
- Add an example upload endpoint.
app.MapPost("/s3-check", async (IAmazonS3 s3, IConfiguration configuration) =>
{
using var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("ok"));
await s3.PutObjectAsync(new PutObjectRequest
{
BucketName = configuration["S3_BUCKET_NAME"],
Key = "health/guide.txt",
InputStream = stream
});
return Results.Ok(new { s3 = "uploaded" });
});
- Deploy and verify access.
flowchart LR
A[ASP.NET Core App] --> B[Instance Profile Credentials]
B --> C[Amazon S3]
D[S3_BUCKET_NAME Property] --> A Verification¶
Use these checks after deployment:
Expected outcomes:
- The application uploads to the correct bucket.
- The instance profile provides credentials automatically.
- No access keys are stored in source or environment properties.