通过预先签名的URL上载AWS S3会返回400错误请求

时间:2019-06-17 15:53:26

标签: javascript node.js reactjs amazon-s3

我正在尝试通过向预先签名的URL发出 PUT请求将文件上传到 RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); StringRequest postRequest = new StringRequest(com.android.volley.Request.Method.POST, YOUR_URL, new Response.Listener<String>() { @Override public void onResponse(String response) { try { // do work here } catch (JSONException e) { e.printStackTrace(); Log.d("Response", "failed: " + response); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.d("Error.Response", error.toString()); } } ) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); //add your parameters here as key-value pairs params.put("title", title); return params; } }; queue.add(postRequest); 。我已经在s3仪表板上配置了CORS。

反应码

AWS s3

Node.js代码

  const submit = async () => {
    const response = await axios.get("http://localhost:8080/api/upload");
    console.log(response.data);
    const upload = await axios.put(response.data.url, file, {
      headers: {
        "Access-Control-Allow-Origin": "*",
        "Content-Type": file.type
      }
    });
  };

当我尝试向预先签名的URL发出const s3 = new AWS.S3({ accessKeyId: config.aws.accessKeyId, secretAccessKey: config.aws.secretAccessKey }); app.get("/api/upload", (req, res) => { const key = `${uuid()}.jpeg`; s3.getSignedUrl( "putObject", { Bucket: "bucket-name", ContentType: "image/jpeg", Key: key }, (err, url) => { res.status(200).json({ key, url }); } ); }); 请求时,出现下一个错误:

  

输入   https://bucket-name.s3.amazonaws.com/dd7eb480-9115-11e9-bb14-75395bf4d226?AWSAccessKeyId=AKIAZPROFMOUP5TZZOIM&Content-Type=%2A&Expires=1560786770&Signature=c2ap3CcLKj%2FUD8yHtiHTNTnWJT4%3D   400(错误请求)createError.js:17未捕获(已承诺)错误:   请求失败,状态码为400       在createError(createError.js:17)       在解决时(settle.js:19)       在XMLHttpRequest.handleLoad(xhr.js:60)

1 个答案:

答案 0 :(得分:0)

创建s3实例的方式将存储区替换为存储区的区域:

AWS.config.update({region: 'REGION'});

const s3 = new AWS.S3({
  apiVersion: '2006-03-01',
  accessKeyId: keys.accessKeyId,
  secretAccessKey: keys.secretAccessKey
});