0

ファイルをs3にアップロードするプロジェクトに取り組んでいます。XMLHttpRequest を使用してポスト リクエストを s3 に送信すると、403 Forbidden ステータスが返されます。firebug で行ったリクエストを確認すると、「Request Method: OPTIONS」が表示され、POST リクエストであると想定されます。

以下は私のコードです:

function GetXmlHttpObject () {

     var xmlHttp = null;

     try {
       // Firefox, Opera 8.0+, Safari, IE 7+
       xmlHttp = new XMLHttpRequest();
     } catch (e) {
       // Internet Explorer - old IE - prior to version 7
       try {
          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
          xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       }
     }

     return xmlHttp;
    }

function uploadFile() {
    console.log(AWSAccessKeyId);
    var file = document.getElementById('file').files[0];
    var fd = new FormData();

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

    fd.append('key', key);
    fd.append('acl', 'public-read'); 
    fd.append('Content-Type', file.type);      
    fd.append('AWSAccessKeyId', AWSAccessKeyId);
    fd.append('policy', policyBase64)
    fd.append('signature', signature);

    fd.append("file",file);

    var xhr = GetXmlHttpObject();

    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://'+bucket+'.s3.amazonaws.com/', true); //MUST BE LAST LINE BEFORE YOU SEND 
    xhr.setRequestHeader("Content-type","multipart/form-data");
    xhr.send(fd);
  }

私はここで立ち往生しています。T__T。

4

1 に答える 1

2

You are triggering a preflighted request. The browser is making an OPTIONS request to make sure it has permission (via CORS) to access the server before making the POST request.

You must configure your bucket to support CORS with preflighted requests as described in Amazon's own documentation.

于 2013-10-09T12:45:29.963 に答える