Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set CacheControl header on S3 object when use AmazonS3Client.PutObject #1410

Closed
vachumakov opened this issue Sep 24, 2019 · 8 comments
Closed
Labels
bug This issue is a bug. p2 This is a standard priority issue s3

Comments

@vachumakov
Copy link

I tied to put object on S3 with CacheControl header has ',' character and got "The request signature we calculated does not match the signature you provided" exception.

Expected Behavior

It should be possible to put object on s3 with header CacheControl = "max-age=3600,s-maxage=60"

Current Behavior

If user puts value with "," character (for example: max-age=7200,s-maxage=60) to PutObjectRequest.Headers.CacheControl, unhandled exception appears:
Amazon.S3.AmazonS3Exception: The request signature we calculated does not match the signature you provided. Check your key and signing method

Steps to Reproduce (for bugs)

Here is my sample:

var s3 = new AmazonS3Client();
var request = new PutObjectRequest {
  BucketName = "my-bucket",
  Key = "my-object.txt",
  CannedACL = S3CannedACL.PublicRead,
  ContentBody = "my body",
  Headers = {
    CacheControl = "max-age=7200,s-maxage=60"
  }
};
s3.PutObjectAsync(request).GetAwaiter().GetResult();

Context

I have S3 bucket. On the top of this bucket I have CloudFront and I want to control cloudfront cache behavior with a help of two http headers max-age and s-maxage

Your Environment

  • AWSSDK.S3 version used: 3.3.104.31
  • Operating System and version: Windows 10
  • Visual Studio version: 2017
  • Targeted .NET platform: netcoreapp2.1

.NET Core Info

  • Output of dotnet --info:
.NET Core SDK (reflecting any global.json):
 Version:   2.2.104
 Commit:    73f036d4ac

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17134
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.2.104\

Host (useful for support):
  Version: 2.2.2
  Commit:  a4fd7b2c84

.NET Core SDKs installed:
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.2.104 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

  • Contents of project.json/project.csproj:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AWSSDK.S3" Version="3.3.104.31" />
  </ItemGroup>
</Project>
@klaytaybai
Copy link
Contributor

Hi @vachumakov, this appears to be the same issue as #823. I was able to reproduce your problem, and then I got it to work by adding the suggested space after the comma. We'll take another look at this problem so users don't have to worry about the space.

@klaytaybai klaytaybai added bug This issue is a bug. guidance Question that needs advice or information. labels Sep 25, 2019
@mcrio
Copy link

mcrio commented Nov 2, 2019

Thanks @klaytaybai Handling the spaces issue on the AWS SDK side would be very helpful.

@ashishdhingra ashishdhingra added s3 and removed guidance Question that needs advice or information. labels Sep 11, 2020
@ashishdhingra
Copy link
Contributor

Check this issue after fix for #1685 is implemented.

@ashishdhingra
Copy link
Contributor

ashishdhingra commented Mar 8, 2021

Not fixed in latest version of AWSSDK.S3 (3.5.9.3) and AWSSDK.Core (3.5.3.5). Adding space after comma in CacheContol header value max-age=7200,s-maxage=60 works as suggested in #823 (comment).

The extra space appears to be added by .NET Core while adding headers to System.Net.Http.HttpRequestMessage object at HttpWebRequestMessage.SetRequestHeaders(). The issue might be due to CoreFx code https://github.com/dotnet/corefx/blob/b3f5ee00b936e167665ae6d3d3994998f33b8c21/src/System.Net.Http/src/System/Net/Http/Headers/NameValueHeaderValue.cs#L183.

May be changing line at

set { this["Cache-Control"] = value; }
to set { this["Cache-Control"] = (value != null? value.Replace(",", ", ") : value); } could be the fix. But the change needs to be tested across .NET Framework/.NET Core versions.

@github-actions
Copy link

We have noticed this issue has not received attention in 1 year. We will close this issue for now. If you think this is in error, please feel free to comment and reopen the issue.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 20, 2022
@ashishdhingra
Copy link
Contributor

Do not close

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Mar 21, 2022
@ashishdhingra ashishdhingra added p2 This is a standard priority issue queued and removed B labels Nov 2, 2022
@dscpinheiro
Copy link
Contributor

I know it's been a while, but this problem doesn't happen anymore in the latest version of the AWSSDK.S3 package (which is 3.7.310 as of today).

I tried running your original example and the PutObject request succeeded without a signature mismatch error.

Copy link

github-actions bot commented Jul 3, 2024

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue s3
Projects
None yet
Development

No branches or pull requests

5 participants