var awsSdk = require('aws-sdk');

awsSdk.config = {
  "accessKeyId": "key",
  "secretAccessKey": "secret",
  "region": "us-east-1"
}

var s3 = new awsSdk.S3({
  accessKeyId: 'key',
  secretAcessKey: 'secret'
});

exports.awsDelete = function(req, res){
  s3.deleteObject({
    Bucket: 'bucket',
    Key: req.body.photo
  }, function(err,data){
    if (err) console.log('delete err', err);
    console.log(data);
  });
};


我还不知道如何使这项工作(至今)。

最初,我遇到了“无配置”错误,因此我在上面添加了awsSdk.config json。现在,它只是挂起/暂停而没有错误。我在req.body.photo中得到了预期的密钥。

我的预感是我的配置中缺少一些东西。

我想念/搞砸了什么?



更新资料
我已经添加了下面建议的代码,但是仍然没有运气。我将展示如何传递参数:

来自以下答案的更新代码:

'use strict';

var aws = require('./aws');

var amazon = require('aws-sdk');

amazon.config = new amazon.Config();
amazon.config.accessKeyId = aws.key;
amazon.config.secretAccessKey = aws.secret;
amazon.config.region = aws.region;

var s3 = new amazon.S3();

exports.awsDelete = function(req, res){
  var params = {
    Bucket: aws.bucket,
    Key: res.body.photo
  };
  s3.deleteObject(params, function(err, data) {
    if (err) console.log(err)
    else console.log("Successfully deleted myBucket/myKey");
 });
};


路线:

  app.post('/awsDelete', uploads.awsDelete);


前端角度:

厂:

angular.module('clientApp').factory('Uploads', function($http) {
  return {
    delete: function(data){
        console.log('delete fired');
        return $http.post('/awsDelete', data);
    }
  };
});


角度控制器:

angular.module('clientApp').controller('Distiller-editCtrl', function(Uploads){

$scope.item = {}

 $scope.delete = function(){
   Uploads.delete($scope.item).then(function(res){
    console.log(res)
    });
   };
 });




似乎是“某种作品”。但是有些事情使它花费了很长时间:

POST /awsDelete 200 120007ms


如果刷新页面,则也将其成功删除。
是否有人注意到我的代码中可能导致响应时间如此长的任何内容。

另外,没有获得“成功完成”的console.log

最佳答案

我刚刚在节点中对此进行了测试,并且工作正常,显然您需要输入自己的访问密钥,secretaccesskey,存储桶和存储桶密钥:

var AWS = require('aws-sdk');

AWS.config = new AWS.Config();
AWS.config.accessKeyId = "";
AWS.config.secretAccessKey = "";
AWS.config.region = "us-east-1";

var s3 = new AWS.S3();

var params = {
  Bucket: 'test537658ghdfshgfd',
  Key: '1.png'
};

s3.deleteObject(params, function(err, data) {
    if (err) console.log(err)
    else console.log("Successfully deleted myBucket/myKey");
});

07-24 09:39
查看更多