gpt4 book ai didi

angularjs - 如何使用nodejs/angular删除Amazon s3文件

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:44 24 4
gpt4 key购买 nike

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 中获得了预期的 key 。

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

我错过了什么/搞砸了什么?

<小时/>

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

更新了以下答案的代码:

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

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

最佳答案

我刚刚在 Node 中测试了这个,它工作得很好,显然你需要输入你自己的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");
});

关于angularjs - 如何使用nodejs/angular删除Amazon s3文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37260161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com