Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 bucket MalformedXML error when uploading

I am trying to upload a picture to my Amazon S3 bucket. Here is the code for my XMLHttpRequest:

var form_data = new FormData();
form_data.append(filename, file);
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://bucket-name.s3.amazonaws.com', true);
xhr.send(form_data);

I have configured the CORS to be as follows:

<CORSConfiguration>
  <CORSRule>
    <AllowedOrigin>http://localhost:3000</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <AllowedHeader>*</AllowedHeader>
  </CORSRule>
</CORSConfiguration>

Unfortunately, I keep getting the following error when I attempt to upload to it:

PUT https://bucket-name.s3.amazonaws.com 400 (Bad Request)
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>MalformedXML</Code><Message>The XML you provided was not well-formed or 
did not validate against our published schema</Message><RequestId>6188AA51D1EE6B38</RequestId>
<HostId>f3d5Aj8bMyMOJywRnYKH/tBXRHCDWFvNzcb4ejs9F4/IulP1P2n0XoN1mDq7LpQgL/RIsW1c6RA=</HostId></Error>

Does anyone know what I am doing wrong?

like image 567
user3579220 Avatar asked Aug 05 '15 17:08

user3579220


People also ask

What is the maximum size we can upload in S3 bucket?

You can upload any file type—images, backups, data, movies, etc. —into an S3 bucket. The maximum size of a file that you can upload by using the Amazon S3 console is 160 GB. To upload a file larger than 160 GB, use the AWS CLI, AWS SDK, or Amazon S3 REST API.

What is the minimum file size that you can upload into S3?

Individual Amazon S3 objects can range in size from a minimum of 0 bytes to a maximum of 5 TB. The largest object that can be uploaded in a single PUT is 5 GB. For objects larger than 100 MB, customers should consider using the Multipart Upload capability.

What is S3 bucket Misconfiguration?

Public write access is another S3 bucket misconfiguration that can lead to data loss and security breaches. An S3 bucket with public write access means that you grant write access, including upload or delete to anyone on the internet. They can add, delete or replace objects within your S3 buckets without restriction.


1 Answers

It looks like you're sending the PUT request to the bucket itself, without an object name. S3 interprets this as a PUT BUCKET request, and expects the body to be a CreateBucketConfiguration XML document.

To upload a file, your XHR open call should look more like:

xhr.open('PUT', 'https://bucket-name.s3.amazonaws.com/' + filename, true);

There's an example request in the documentation.

like image 62
pmcoltrane Avatar answered Sep 25 '22 05:09

pmcoltrane