2017-06-06 3 views
1

Ich unterzeichne die URL auf meinem Server und sende sie zurück an den Client, was gut funktioniert. Dies ist, wie die Funktionbekomme 400 Schlechte Anfrage beim Hochladen auf aws s3 bucket

const aws = require('aws-sdk'), 
    config = require('config'), 
    crypto = require('crypto'); 


module.exports = async function(file_type) { 

    aws.config.update({accessKeyId: config.AWS_ACCESS_KEY, secretAccessKey: config.AWS_SECRET_KEY}) 

    const s3 = new aws.S3(); 

    try { 
     if (!file_type === "image/png") { 
      return ({success: false, error: 'Please provide a valid video format'}); 
     } 
     let buffer = await crypto.randomBytes(12); 

     let key = buffer.toString('hex'); 

     let options = { 
      Bucket: config.AWS_S3_BUCKET, 
      Key: key, 
      Expires: 60, 
      ContentType: file_type, 
      ACL: 'public-read', 
     } 

     let data = await s3.getSignedUrl('putObject', options); 
     console.log('data was', data) 
     return ({ 
      success: true, 
      signed_request: data, 
      url: ('https://s3.amazonaws.com/' + config.AWS_S3_BUCKET + '/' + key), 
      key, 
     }); 
    } catch (error) { 
     console.log('the error was', error) 
     return ({ 
      success: false, 
      error: error.message, 
     }) 
    } 
} 

sieht also das funktioniert gut und wickelt mich wie

https://mybucket.s3.amazonaws.com/a33b4a43f23fc41de9ddck1k?AWSAccessKeyId=ADIFJDGPMRFRGLXSYWPQ&Content-Type=image%2Fpng&Expires=1496716543&Signature=0zcx%2BFzWUoeFD02RF2CQ2o0bLmo%3D&x-amz-acl=public-read

dann eine URL immer wenn ich diese URL auf dem Client zurück .. Ich sende eine PUT-Anfrage mit Hilfe von Axios mit einer Funktion wie -

Das einzige ist wieder erhalte ich (400) Bad Request

Antwort

0

Raten schlecht Kopf Sie

zur Verfügung gestellt für mich

Works

function upload(file, signedRequest, done) { 
    const xhr = new XMLHttpRequest(); 
    xhr.open('PUT', signedRequest); 
    xhr.setRequestHeader('x-amz-acl', 'public-read'); 
    xhr.onload =() => { 
    if (xhr.status === 200) { 
     done(); 
    } 
    }; 

    xhr.send(file); 
} 
+0

Ich versuche es mit axios zu tun. Oben habe ich es so gemacht, also bin ich einfach wieder mit ihr gegangen lol – joe