2015年10月17日 星期六

aws nodejs S3 begin

照這個範例


https://aws.amazon.com/tw/sdk-for-node-js/
var AWS = require('aws-sdk'); AWS.config.update({ accessKeyId: "xxx secretAccessKey: "xxx" }); //要先設定key pair var s3 = new AWS.S3();
s3.createBucket({Bucket: 'myBucket'}, function() {//這個bucket要在後台先開好,不能用create的   var params = {Bucket: 'myBucket', Key: 'myKey', Body: 'Hello!'};   s3.putObject(params, function(err, data) {       if (err)           console.log(err)       else console.log("Successfully uploaded data to myBucket/myKey");    }); }); //因為無法createBucket跟本不能用

這設定這邊
https://console.aws.amazon.com/s3/home?region=us-west-2&bucket=demodemodemowolke&prefix=
1.開bucket
2. Add more permissions
 Add bucket policy
 Edit CORS Configuration


選edit CORS..
3.設下面這個,就可以在Local端玩S3了


   
        http://localhost
        PUT
        POST
        DELETE
        POST
        3000
        *
   



//code改這樣
var s3 = new AWS.S3();
    var params = {
      Bucket: 'demodemodemowolke' /* required */
      , Key: 'key.TTF', Body: file
    };

      s3.putObject(params, function(err, data) {

          if (err)    

              console.log(err)  

          else console.log("Successfully uploaded data to myBucket/myKey");

       });


沒有留言:

張貼留言