Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems uploading blob directly to s3

I have the following code:

var fd = new FormData();

        var key = "events/" + (new Date).getTime() + '-';

        fd.append('key', key);
        fd.append('acl', Acl);
        fd.append('Content-Type', "image/jpeg");
        fd.append('AWSAccessKeyId', AWSAccessKeyId);
        fd.append('policy', Policy);
        fd.append('name', "Policy13492345");
        fd.append('success_action_status', "201");
        fd.append('signature', Signature);          
        fd.append("file",  blob);
        fd.append("filename", fileName + ".jpg");
        var xhr = new XMLHttpRequest();



        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);

        xhr.open('POST', 'https://s3.amazonaws.com/' + Bucket + '/', true); 

        xhr.send(fd);

When this request goes through I get the following error:

<Code>AccessDenied</Code><Message>Invalid according to Policy: Policy Condition failed: ["starts-with", "$Filename", ""]</Message>

I have no idea what I am doing wrong, I generate my blob like so:

function dataURItoBlob(dataURI) {
                var binary = atob(dataURI.split(',')[1]);
                var array = [];
                for (var i = 0; i < binary.length; i++) {
                    array.push(binary.charCodeAt(i));
                }
                var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
                return new Blob([new Uint8Array(array)], { type: mimeString });
            }

This is my request:

------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="key"

events/1367541109750-
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="acl"

private
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="Content-Type"

image/jpeg
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="AWSAccessKeyId"

asdfasdfFASDFSDFAADSFHHVDQ
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="policy"

FsnY29udFuZ2UnLCAwLCAxMDAwMDAwMDBdLAogICAgICasdfasdfAgIFsgJ3N0YXJ0cy13aXRoJywgJyRrZXknLCAnJyBdLAogICAgICAgIFsgJ3N0YXJ0cy13aXRoJywgJyRDb250ZW50LVR5cGUnLCAnasdfJyBdLAo
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="name"

Policy134722343242345
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="success_action_status"

201
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="signature"

basdfasdftwa/9asdfasdfx3/zasdfadsft6g=
------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: image/jpeg


------WebKitFormBoundaryxh8thnHAmDhZQuXE
Content-Disposition: form-data; name="filename"

C:\fakepath\495845894.jpg
------WebKitFormBoundaryxh8thnHAmDhZQuXE--
like image 497
anthonypliu Avatar asked May 03 '13 00:05

anthonypliu


People also ask

How upload blob AWS S3?

To upload folders and files to an S3 bucketSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that you want to upload your folders or files to. Choose Upload.

Why am I getting an access denied error message when I upload files to my Amazon S3 bucket?

If you're getting Access Denied errors on public read requests that are allowed, check the bucket's Amazon S3 Block Public Access settings. Review the S3 Block Public Access settings at both the account and bucket level. These settings can override permissions that allow public read access.

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.

Can I upload ZIP file to S3 bucket?

S3 is just storage. Whatever file you upload is the file that is stored. You cannot upload a zip file then extract it once its in S3.


1 Answers

Figured out the issue, ordering for the formdata is important, you must follow the correct order in order for the data to post correctly.

like image 75
anthonypliu Avatar answered Oct 17 '22 01:10

anthonypliu