-
I am trying to instantiate the AmazonS3Client via DI in a ASP.NET 6 project and it seems any version above 3.7.10.1 will just throw a forbidden error when trying to access s3. I cannot find any information on what might have changed after this version? This is debugging via vs2022, the credentials file is configured under %userprofile%/.aws/credentials, this is also deployed to an ECS instance and I'm seeing the same issue. Methods mainly utilised are
Registering the service with DI:
(services.AddDefaultAWSOptions(Configuration.GetAWSOptions()) or the like is not used, and didn't anyway change the outcome) In appsettings.json there is the following:
If the version is <= v3.7.10.1 then it works fine, just anything above this gives the error.. What am I missing? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
@ajangles Good afternoon. Thanks for starting the discussion. Few things to note:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.Extensions.NETCore.Setup" Version="3.7.7" />
<PackageReference Include="AWSSDK.S3" Version="3.7.104.28" />
</ItemGroup>
</Project> Program.cs using Amazon.S3;
using Microsoft.Extensions.Configuration;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddDefaultAWSOptions(builder.Configuration.GetAWSOptions());
builder.Services.AddAWSService<IAmazonS3>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run(); Pages\Index.cshtml @page
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
@if (Model.ObjectMetadata != null)
{
<b>Object Content Length:</b> @Model.ObjectMetadata.ContentLength
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div> Pages\Index.cshtml.cs using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace ASPNetCore6S3DITest.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly IAmazonS3 _amazonS3;
public IndexModel(ILogger<IndexModel> logger, IAmazonS3 amazonS3)
{
_logger = logger;
_amazonS3 = amazonS3;
}
[BindProperty]
public GetObjectMetadataResponse ObjectMetadata { get; set; } = default;
public async Task<IActionResult> OnGetAsync(string bucketName, string key)
{
if (!string.IsNullOrEmpty(bucketName) && !string.IsNullOrEmpty(key) && _amazonS3 != null)
{
ObjectMetadata = await _amazonS3.GetObjectMetadataAsync(bucketName, key);
}
return Page();
}
}
} The code works fine and the content length is displayed on the home page when query string parameters
Forbidden Response normally means credentials used do not have access to invoke service API operation. You may also turn on verbose logging while debugging locally using below statements before making S3 call (or preferably before Amazon.AWSConfigs.LoggingConfig.LogResponses = Amazon.ResponseLoggingOption.Always;
Amazon.AWSConfigs.LoggingConfig.LogTo = Amazon.LoggingOptions.SystemDiagnostics;
Amazon.AWSConfigs.AddTraceListener("Amazon", new System.Diagnostics.ConsoleTraceListener()); Thanks, |
Beta Was this translation helpful? Give feedback.
-
Hi Ashish, thank you for your reply, the verbose logging helped me find the answer. It seems that <= v3.7.10.1 you could set the path in the BucketName property, but that now no longer works var request = new GetObjectMetadataRequest()
{
BucketName = bucketName, //eg - bucket/folderOne/folderTwo
Key = fileKey, //eg - someFile.pdf
}; I saw that it was escaping some of the '/' characters in the path incorrectly in the verbose logging:
Changing the request to the following has resolved the issue: var request = new GetObjectMetadataRequest()
{
BucketName = bucketName, //eg - bucket
Key = fileKey, //eg - folderOne/folderTwo/someFile.pdf
}; |
Beta Was this translation helpful? Give feedback.
-
Hello! Reopening this discussion to make it searchable. |
Beta Was this translation helpful? Give feedback.
Hi Ashish, thank you for your reply, the verbose logging helped me find the answer. It seems that <= v3.7.10.1 you could set the path in the BucketName property, but that now no longer works
I saw that it was escaping some of the '/' characters in the path incorrectly in the verbose logging: