lifuzu / react-native-uploader-s3

Another example to upload image to S3 with RN module - react-native-uploader
37 stars 3 forks source link

Not working for me, status 400 #5

Open satyagupta opened 8 years ago

satyagupta commented 8 years ago

I keep getting status 400 with following response.

<?xml version="1.0" encoding="UTF-8"?>\n<Error><Code>InvalidArgument</Code><Message>Malformed Unicode code sequence in the field.</Message><ArgumentName>formField</ArgumentName><RequestId>CA9372E2E9C1B13C</RequestId><HostId>pVNYeynJARTc5TCf/Gk9MPiY0bgaPpre8Fade6lgbx/aUbUm26X4LCfkXershgXwgDIFDhA9to0=</HostId></Error>

Am i missing anything?

Complete code:

`uploadFiles.push(this.state.files[i]); let s3_opts = { bucket: 'bridalbible', region: 'us-east-1', key: 'YYY', secret: 'XXXX', type: 'image/jpeg', path: 'profile/', acl: 'public-read', expires: new Date(Date.now() + 60000), length: 10485760, // 10M as maximal size }; let p = s3_policy(s3_opts); // console.log(p.policy); // console.log(p.signature);

let opts = {
  url: 'https://' + s3_opts.bucket + '.s3.amazonaws.com/',
  files: uploadFiles,
  method: 'POST',
  params: {
    key: Date.now(),
    acl: s3_opts.acl,
    'X-Amz-Signature': p.signature,
    'x-amz-credential': p.credential,
    'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
    'X-Amz-Date': p.date + 'T104657Z',
    'Content-Type': 'image/jpeg',
    'policy': p.policy,
    'success_action_status': '201',
    // 'x-amz-meta-uuid': '14365123651274'
  }
};

//console.log(opts);

RNUploader.upload( opts, (err, response) => {
    if( err ){
        console.log(err);
        return;
    }
    console.log(response);
    /*
    let status = response.status;
    let responseString = response.data;
    let json = JSON.parse( responseString );

    console.log('upload complete with status ' + status);*/
});`
satyagupta commented 8 years ago

@lifuzu Please reply

phoenixbox commented 8 years ago

@satyagupta

The key should be a bucket path location like uploads/ or something. If you want it nested to user for a date you could use moment.

import moment from 'moment';
const timeArray = moment().utc().format('L').split('/');
const year = timeArray[2];
const month = timeArray[0];
const day = timeArray[1];
const key = `uploads/${USER_ID}/${year}/${month}/${day}/`;

That way all of an the uploads for a user, can be found under their id and the target date.

Also I think the expires key might be of type Integer and not Date.

So 60000 might be the right value and then AWS will take care of creating the time that says Date.now() +60000