Wael Dhif
Wael Dhif

Reputation: 1

getting 400 bad request when trying to use S3 oracle object storage to upload files

i'm trying to uplaod files to OSI S3 object storage with .net framework 4.8. i tried working with a pre authenticated url which worked but for security purposes i need to make it work with using credential (access key , secret key) here's the implemented code i have:

try
            {
                var amazonS3Config = new AmazonS3Config
                {
                    ServiceURL = OCI_ENDPOINT,
                    ForcePathStyle = true,
                };

                var credentials = new BasicAWSCredentials(OCI_ACCESS_KEY, OCI_SECRET_KEY);
                var s3Client = new AmazonS3Client(credentials, amazonS3Config);
              
                memoryStream.Seek(0, SeekOrigin.Begin);
                if (memoryStream.Length == 0)
                {
                    throw new Exception("MemoryStream is empty, check file input.");
                }
                if (!memoryStream.CanRead)
                {
                    throw new Exception("Cannot read memoryStream.");
                }
                var putRequest = new PutObjectRequest
                {
                    BucketName = OCI_BUCKET_NAME,
                    Key = fileName,
                    InputStream = memoryStream
                };

                var response = await s3Client.PutObjectAsync(putRequest);
                string fileUrl = $"{OCI_ENDPOINT}/{OCI_BUCKET_NAME}/{fileName}";


                fileObj.FullURL = fileUrl;
                fileObj.FullPath = fileUrl;


                return new IResponse(true, "File uploaded successfully.");
            }
            catch (AmazonS3Exception s3Exception)
            {
                // Log the full exception details
                Trace.WriteLine($"AWS S3 Error: {s3Exception.Message}");
                Trace.WriteLine($"Status Code: {s3Exception.StatusCode}");
                Trace.WriteLine($"Request ID: {s3Exception.RequestId}");
                Trace.WriteLine($"Error Code: {s3Exception.ErrorCode}");
                Trace.WriteLine($"AWS Error Type: {s3Exception.ErrorType}");
       
                Trace.WriteLine($"Stack Trace: {s3Exception.StackTrace}");

                // Log the inner exception (if any)
                if (s3Exception.InnerException != null)
                {
                    Console.WriteLine("Inner Exception:");
                    Console.WriteLine(s3Exception.InnerException.ToString());
                }

                // Return a detailed error message
                string detailedErrorMessage = $"Error Code: {s3Exception.ErrorCode}\n" +
                                             $"Error Message: {s3Exception.Message}\n" +
                                             $"Request ID: {s3Exception.RequestId}\n" +
                                             $"Status Code: {s3Exception.StatusCode}\n" +
                                             $"Response Body: {s3Exception.ResponseBody}";

                return new IResponse(false, $"Error uploading file to S3: {detailedErrorMessage}");
            }
            catch (Exception ex)
            {
                // Log any other exceptions
                Console.WriteLine("Unexpected Exception:");
                Console.WriteLine(ex.ToString());

                return new IResponse(false, $"Unexpected error: {ex.Message}");
            }

and the error i get from response is "Error making request with Error Code BadRequest and Http Status Code BadRequest. No further error information was returned by the service." im sure it's not a permission issue as the credentials grant full control and variables are correct as i'm using them to retrieve files from S3 storage and the api returns the file without a problem, only uplaoding returns this error. after using fiddler to debug , i got this error : STREAMING-AWS4-HMAC-SHA256-PAYLOAD is not supported.

Upvotes: -1

Views: 22

Answers (0)

Related Questions