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

ContentType parameter is misleading #1273

Open
garnier-kappa opened this issue Feb 27, 2025 · 0 comments
Open

ContentType parameter is misleading #1273

garnier-kappa opened this issue Feb 27, 2025 · 0 comments

Comments

@garnier-kappa
Copy link

Summary : Be more strict about what "ContentType" can be passed as an argument to PutObjectArgs

Issue 1 - Content type

See the problematic code we had :

var args = new PutObjectArgs()
               .WithBucket(GetFullBucketName(bucketName))
               .WithObject(objectPath)
               .WithStreamData(stream)
               .WithObjectSize(stream.Length)
               .WithContentType("whatever"); // Here

await _client.PutObjectAsync(args);

WithContentType(string type) accepts any string its given, but it should not.
It had for us the side effect of breaking the MultiPart upload of large files (> 5MB).

It was very hard to debug, because the error we got was an XML inside the Library : (sensitive info blanked with "---")

<?xml version="1.0" encoding="UTF-8"?>  
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message><Key>---</Key>
<BucketName>---</BucketName>
<Resource>---</Resource>
<RequestId>182801E71B31948E</RequestId>
<HostId>---</HostId>
</Error>

Issue 2

It was even harder to debug because of the stack.

"System.InvalidOperationException: There is an error in XML document (2, 2).\n ---> System.InvalidOperationException: <Error xmlns='http://s3.amazonaws.com/doc/2006-03-01/'> was not expected.\n

at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderInitiateMultipartUploadResult.Read3_InitiateMultipartUploadResult()\n 
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)\n 
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)\n --- End of inner exception stack trace ---\n 
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)\n 
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)\n 

at Minio.Helper.Utils.DeserializeXml[T](Stream stream)\n 
at Minio.DataModel.Response.NewMultipartUploadResponse..ctor(HttpStatusCode statusCode, String responseContent)\n 
at Minio.MinioClient.NewMultipartUploadAsync(NewMultipartUploadPutArgs args, CancellationToken cancellationToken)\n 

at Minio.MinioClient.PutObjectAsync(PutObjectArgs args, CancellationToken cancellationToken)\n 

XML deserialization error indicates another issue in the code.
It is located here :

using var stream = Encoding.UTF8.GetBytes(responseContent).AsMemory().AsStream();

The code returns an empty stream.
DeserializeXml<T>(Stream stream) returns a default InitiateMultipartUploadResult, that ends up throwing because the namespace is not recognized.

Proposed fixes

  • Create an Enum / Use an existing Enum from .net to enforce the ContentType
  • Read the response content like await response.Content.ReadAsStingAsync(), and use the result inside your exising method DeserializeXml<T>(string xml) (the one taking string, not stream)
  • Overall better exceptions based on the content of the XML, because it's very time consuming to find the actual issue

These issues were experienced using the latest version of the nuget package to this date, 6.0.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant