如何删除amazon s3文件w / nodejs /angular

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); }); }; 

我无法弄清楚如何使这项工作(还)。

最初,我得到了一个“无configuration”的错误,所以我添加了上面的awsSdk.config json。 现在,它只是挂起/暂停没有错误。 我正在req.body.photo中获得预期的关键。

我的直觉是,我在我的configuration丢失的东西..

我在想什么/搞砸了?


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

从下面的答案更新代码:

 '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度:

厂:

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

angular度控制器:

 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

       

网上收集的解决方案 "如何删除amazon s3文件w / nodejs /angular"

我刚刚在节点中testing了它,它工作正常,显然你需要把你自己的accesskey,secretaccesskey,bucket和bucket key放进去:

 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"); }); 

或者,您可以使用Minio-Js客户端库,其开放源代码并与AWS S3兼容。

下面是remove-object.js的例子,你可以在这里find完整的列表

 var Minio = require('minio')

 var s3Client = new Minio({
   endPoint:'s3.amazonaws.com',
   accessKey:'YOUR-ACCESSKEYID',
   secretKey:'YOUR-SECRETACCESSKEY'
 })
 //删除一个对象名称my-objectname。
 s3Client.removeObject('my-bucketname','my-objectname',function(e){
  如果(e){
    返回console.log(e)
   }
  的console.log( “成功”)
 })

请用您自己的名称replace您的ACCESSKEYID和YOUR-SECRETACCESSKEY,并将endPointreplace为您创build存储桶的端点。

    us-east-1:'s3.amazonaws.com',
    us-west-1:'s3-us-west-1.amazonaws.com',
    us-west-2:'s3-us-west-2.amazonaws.com',
    eu-west-1:'s3-eu-west-1.amazonaws.com',
    sa-east-1:'s3-sa-east-1.amazonaws.com',
    eu-central-1:'s3-eu-central-1.amazonaws.com',
    ap-southeast-1:'s3-ap-southeast-1.amazonaws.com',
    ap-southeast-2:'s3-ap-southeast-2.amazonaws.com',
    ap-northeast-1:'s3-ap-northeast-1.amazonaws.com'

安装Monio-js

 $ npm install --save minio

希望能帮助到你。

免责声明:我为Minio工作。