Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
File.WriteAllBytes("D:/" + model.FileBase.FileName, model.FileBase.Data);
               byte[] bytes = Convert.FromBase64String(model.file);
               using (var inputStream = new MemoryStream(model.FileBase.Data))
               {
                   inputStream.Position = 0;
                   await s3Client.PutObjectAsync(new PutObjectRequest
                   {
                       InputStream = inputStream,
                       ContentType = MimeMapping.GetMimeMapping(model.fileName + model.fileExtension),
                       BucketName = model.bucketName,
                       Key = model.fileName,
                       FilePath = model.destination,
                       CannedACL = S3CannedACL.BucketOwnerFullControl
                   });
               }


What I have tried:

Able to save file on D drive but while posting to s3 bucket error shows
Please specify one of either an InputStream or a FilePath to be PUT as an S3 object.'


at Amazon.S3.Internal.AmazonS3PreMarshallHandler.ProcessPreRequestHandlers(IExecutionContext executionContext)
   at Amazon.S3.Internal.AmazonS3PreMarshallHandler.InvokeAsync[T](IExecutionContext executionContext)
   at Amazon.Runtime.Internal.CallbackHandler.<InvokeAsync>d__9`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Amazon.S3.Internal.AmazonS3ExceptionHandler.<InvokeAsync>d__1`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Amazon.Runtime.Internal.ErrorCallbackHandler.<InvokeAsync>d__5`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Amazon.Runtime.Internal.MetricsHandler.<InvokeAsync>d__1`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at WebApi.Controllers.S3BucketController.<Post>d__2.MoveNext() in C:\Users\Satya\Downloads\WebApi\Controllers\S3BucketController.cs:line 62
Posted
Updated 15-Feb-24 0:52am
v2

1 solution

The error message is slightly confusing, but once you see it, the problem is obvious:
Quote:
Please specify one of either an InputStream or a FilePath to be PUT as an S3 object.
The error is telling you that you have specified both an InputStream and a FilePath, which is not supported.

Remove the FilePath property from your PutObjectRequest object.
 
Share this answer
 
Comments
itsathere 15-Feb-24 7:01am    
I got your point. Thanks but

We have defined dynamic destination path in S3 bucket. So, how can we make sure that file uploaded on specific folder or not
Richard Deeming 15-Feb-24 7:22am    
FilePath specifies the local path of a file to upload, not the remote path where you want to store the file.

As far as I can see, S3 doesn't use folders to store files. Instead, you specify the full "path" of the object as its Key. You've already specified that, so removing the FileName property should just work.

NB: Be very careful if the model.FileName is in any way based on user input. Ideally, you'd want to validate that it doesn't contain any "invalid" characters which could change the behaviour of your code.
itsathere 15-Feb-24 8:43am    
Thanks for you information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900