How to delete Amazon s3 file w/ nodejs / angular - angularjs

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);
});
};
I can't figure out how to make this work (yet).
initially, I was getting a "no config" error, so I added the awsSdk.config json above. Now, it's just getting hung / pausing with no error. I am getting the expected key in req.body.photo.
My hunch is that i'm missing something in my config..
What am I missing / screwing up?
Update
I've added the code suggested below, but still no luck. I'll show how i'm passing my parameter:
updated code from answer below:
'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");
});
};
route:
app.post('/awsDelete', uploads.awsDelete);
Front end Angular:
factory:
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)
});
};
});
Seems it 'sort of works'. But something is making it take an extremely long time:
POST /awsDelete 200 120007ms
If I refresh the page, that causes it to successfully delete it as well.
Does anyone notice anything in my code that could be causing such a long response time.
Also, not getting the "successfully completed" console.log

I just tested this in node and it worked fine, obviously you need to put in your own accesskey, secretaccesskey, bucket and 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");
});

Alternatively you can use Minio-Js client library, its Open Source and compatible with AWS S3.
Below is remove-object.js example, you can find complete list here
var Minio = require('minio')
var s3Client = new Minio({
endPoint: 's3.amazonaws.com',
accessKey: 'YOUR-ACCESSKEYID',
secretKey: 'YOUR-SECRETACCESSKEY'
})
// Remove an object name my-objectname.
s3Client.removeObject('my-bucketname', 'my-objectname', function(e) {
if (e) {
return console.log(e)
}
console.log("Success")
})
Please replace YOUR-ACCESSKEYID and YOUR-SECRETACCESSKEY with your own also replace the endPoint to the one you have your bucket is created.
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'
Installing Monio-js
$ npm install --save minio
Hope it helps.
Disclaimer: I work for Minio.

Related

How to delete a file on s3 with: Meteor and aws-sdk

From my Meteor app, I can upload an image on my s3 bucket and now I want to remove it. To do that, I'm using aws-sdk with:
import AWS from 'aws-sdk;
And this code:
AWS.config.update({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: region
});
let s3 = new AWS.S3();
let params = {
Bucket: 'aaaa', // bucket 'name'
Key: 'images/qqwd468767-9awdw' // 'path/imageKey'
};
console.log(params); // {Bucket: 'aaaa', Key: images/qqwd468767-9awdw }
Meteor.wrapAsync(
s3.deleteObject(params, function (error, data) {
if (error) {
console.log(error);
} else {
console.log(data);
}
})
);
It's not working I get this error message :
TypeError: stream.setTimeout is not a function
at features.constructor.handleRequest (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:178932)
at executeSend (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:174057)
at Request.SEND (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:174071)
at Request.callListeners (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:173612)
at Request.emit (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:173588)
at Request.emit (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:175024)
at Request.transition (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:174363)
at AcceptorStateMachine.runTo (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:175171)
at modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:175183
at Request.<anonymous> (modules.js?hash=4f9d58166294ad7d39d307939c58d106525401c5:174379)
I don't get if it's because of Meteor or my code. I tried to add in my CORS configuration : <AllowedMethod>DELETE</AllowedMethod> because it makes sense to allow delete, but still not working.
Hey try this Meteor method to delete file
delete_s3: function(key) {
const AWS = require('aws-sdk');
AWS.config.update({accessKeyId: Meteor.settings.private.s3.AccessKey, secretAccessKey: Meteor.settings.private.s3.SecretKey, region: Meteor.settings.private.s3.region});
var s3 = new AWS.S3();
var params = {
Bucket: Meteor.settings.private.s3.bucket,
Key: key
};
var deleteObject = Meteor.wrapAsync(s3.deleteObject(params, function(error, data) {
if (error) {
console.log(error);
} else {
console.log(data);
}
})); }

HTTP requests fail for AngularJS/Express using Mongoose when it works with MongoJS

Need some help figuring out whats wrong with my code that's giving me 500 internal server error. Have been trying to figure out root cause myself since last 3 days to no avail. I've started learning the MEAN stack and have been experimenting with MongoJs and Mongoose. It's the latter setup that is not working. I've pasted below
a) gulpfile.js
b) Mongoose model/schema file (modelA.js),
c) httprequests.js file containing http requests using mongojs and mongoose {on the sideline - is it a bad idea to have MongoJS and Mongoose codes in the same application???} and
d) angular script file (phone.js) . With the current coding my page (not mentioned below) using mongojs is working perfectly.
e) Error messages from Terminal and Chrome console
I'm new to MEAN and must be doing something basically wrong. Any help is sincerely appreciated.
Gulpfile.js:
var gulp = require('gulp');
var browserSync = require('browser-sync').create();
// historyapifallback to help overcome page refersh issue in angularjs
var historyApiFallback = require('connect-history-api-fallback');
gulp.task('serve', function() {
browserSync.init({
port: 3001,
proxy: {
target: "localhost:3000",
ws: true,
middleware: [ historyApiFallback() ]
}
});
gulp.watch("./**/*.*").on('change', browserSync.reload);
});
gulp.task('default',['serve'], function() {
var httpRequests = require('./scripts/mongo-mongodb/httprequests');
});
modelA.js
var mongoose = require('../../../node_modules/mongoose/lib');
var Schema = mongoose.Schema;
var phoneSchema = new Schema({
age : Number,
carrier : String,
id : String,
imageUrl : String,
name : String,
company: String,
snippet : String
});
/* global db */
module.exports = mongoose.model('Phone', phoneSchema);
httprequests.js
// load express module
var express = require("express");
// bootstrap express
var app = express();
//serve up index.html
app.use(express.static("./"));
// body parser for post requests
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Mongojs codes follows...
var mongojs = require('mongojs');
var db = mongojs('test',['technology']);
// get all data from db
app.get('/techstack',function(req,res) {
db.technology.find({},function(err,docs) {
if (err) throw err;
// console.log("im here");
// console.log(docs);
res.json(docs);
});
});
// get data from db for specific id
app.get('/techstack/:id',function(req,res) {
var id = req.params.id;
db.technology.findOne({_id: mongojs.ObjectId(id)},function(err,docs) {
if (err) console.error;
console.log(id);
console.log("gulpfile");
res.json(docs);
});
});
app.post('/techstack', function(req,res) {
console.log(req.body);
db.technology.insert(req.body,function(err,docs) {
if (err) console.error;
res.json(docs);
});
});
app.delete('/techstack/:id', function(req,res) {
var id = req.params.id;
db.technology.remove({_id: mongojs.ObjectId(id)},function(err,docs) {
// console.log(id);
// db.technology.remove({ _id: id},function(err,docs) {
if (err) console.error;
res.json(docs);
});
});
app.put('/techstack/:id', function(req,res) {
var id = req.params.id;
db.technology.findAndModify(
{
query: {_id:mongojs.ObjectId(id)},
update: {$set: {sl:req.body.sl, name:req.body.name, status:req.body.status, complexity:req.body.complexity}},
new: true
}, function (err, docs) {
if (err) console.error;
res.json(docs);
});
});
//
// Mongoose codes follow...
var mongoose = require('../../../node_modules/mongoose/lib');
mongoose.connect('mongodb://localhost/mean_db');
// var uri ='mongodb://localhost/mean_db';
// global.db = mongoose.createConnection(uri);
// var routes = require('../mongo-mongoose/routes');
var Phone = require('./modelA');
var router = express.Router(); // get an instance of the express Router
// // middleware to use for all requests
router.use(function(req, res, next) {
// do logging
console.log('Something is happening.');
next(); // make sure we go to the next routes and don't stop here
});
router.get('/',function(req,res) {
Phone.find({},function(err, docs) {
if (err)
res.send(err);
res.send(docs);
});
});
router.post('/',function(req, res) {
var phone = new Phone(); // create a new instance of the Phone model
// save the phone and check for errors
phone.save(req.body,function(err,docs) {
if (err)
res.send(err);
res.json(docs);
});
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /phone-list
app.use('/phone-list', router);
app.listen(3000);
console.log('Listening on port 3000');
phone.js
'use strict';
var phone = angular.module('phone',[]);
phone.controller('View1Ctrl', function($scope,$http,$log,$location) {
$scope.phone = {
age : 0,
carrier : "",
id : "",
imageUrl : "",
name : "",
company: "",
snippet : ""
};
$http.get('/phone-list')
.then(function (response) {
// console.log(response.data);
$scope.phoneList = response.data;
}
);
$scope.insertPhone = function () {
$http.post('/phone-list', $scope.phone)
.then(function (response) {
console.log("inside post in phone.js");
$scope.phoneList = response.data;
$route.reload();
});
};
Error Messages:
On terminal
14:18:51] Using gulpfile ~/github/mean-project/app/gulpfile.js
[14:18:51] Starting 'serve'...
[14:18:52] Finished 'serve' after 129 ms
[14:18:52] Starting 'default'...
Listening on port 3000
[14:18:52] Finished 'default' after 686 ms
[BS] Proxying: http://localhost:3000
[BS] Access URLs:
----------------------------------
Local: http://localhost:3001
External: http://10.0.1.12:3001
----------------------------------
UI: http://localhost:3003
UI External: http://10.0.1.12:3003
----------------------------------
Something is happening.
Something is happening.
Something is happening.
Something is happening.
Something is happening.
Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^ TypeError: callback is not a function
at /Users/akhileshmohan/github/mean-project/node_modules/mongoose/lib/model.js:228:5
at /Users/akhileshmohan/github/mean-project/node_modules/mongoose/lib/model.js:135:7
at /Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/collection.js:504:5
at /Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/collection.js:666:5
at handleCallback (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12)
at /Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:473:9
at handleCallback (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/utils.js:96:12)
at resultHandler (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:420:5)
at commandCallback (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:1194:9)
at Callbacks.emit (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:119:3)
at null.messageHandler (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:358:23)
at Socket.<anonymous> (/Users/akhileshmohan/github/mean-project/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:292:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
On browser console
angular.js:12011 POST http://localhost:3001/phone-list net::ERR_EMPTY_RESPONSE
(anonymous function) # angular.js:12011
n # angular.js:11776
(anonymous function) # angular.js:11571
(anonymous function) # angular.js:16383
$eval # angular.js:17682
$digest # angular.js:17495
$delegate.__proto__.$digest # VM95:844
$apply # angular.js:17790
$delegate.__proto__.$apply # VM95:855
(anonymous function) # angular.js:25890
Sf # angular.js:3497
d # angular.js:3485

MongooseJS Update API

I am having a 404 issue with my NodeJS API. I don't know if I am quite doing it right, I tried referring to documentation, and I feel like it's close.
MongoDB Schema
var User = mongoose.Schema({
local: {
email: String,
password: String,
handle: String,
pic: {data: Buffer, contentType: String}
}
});
NodeJS UPDATE API
app.post('/api/users', function(req, res, user) {
User.update({email : user.email,
password : user.password,
handle : user.handle,
pic : user.pic},
{$set: {
email : req.body.email,
password : req.body.email,
handle : req.body.handle,
pic : req.body.pic,
done : false
}
}, function(err, users) {
if(err) {
res.send(err);
}
res.redirect('/profile');
});
});
Controller POST API call
$scope.editProfile = function() {
$http.post('/api/users', $scope.editFormData)
.success(function(data) {
console.log(data);
})
.error(function(data) {
console.log('Error: ' + data);
});
};
Any suggestions?
You are not doing a post call correct. You can't pass your post object in the URL. Your node post should look like this.
app.post('/api', upload.array(), function(req, res) {
var body = req.body; //body will be your post object
});
For a post to work you need to make sure you have the proper things added to your Node Project. The above example is using ExpressJS with require('body-parser') and require('multer'). The example you are showing will never show as a true path. For reference here is how you would do a get in node.
app.get('/getcall/*', function(){
// the * denotes any singleton parameter you wanted to pass in.
})
Here are the references I use in all my node projects. These are the basics.
var express = require('express'),
bodyParser = require('body-parser'),
multer = require('multer'),
helmet = require('helmet'),
upload = multer(),
path = require('path'),
request = require('request'),
app = express(),
http = require('http');
Also as for your angular call an $http.post looks like this and you should be using .then instead of .success.
$http.post('/api', $scope.editFormData)
.then(function successCallback(resp) {
console.log(resp.data)
}, function errorCallback(resp) {
console.log(resp)
});

POST and GET images to/out of MongoDB in Angular

I´m making an angular application, which gives users the possibilities, to manage their projects. I´ve got nodeJS & express serveside and MongoDB as my database.
I want to achieve, that a user can upload media(images under 16MB, so no need to use GridFS) to their projects and that you can display which project, has which media attached.
I´m not getting the images, nor an error. How can I pass the project_Id from angular, to my route, to find the media attached to the project? Is this way of trying to POST and GET the media the right way?
The model for projects and for media:
var mediaSchema = mongoose.Schema({
media : {data: Buffer, contentType: String},
project_id : String,
updated_at : {type: Date, default: Date.now }
});
var projectSchema = mongoose.Schema({
author : String,
name : String,
description : String,
tags : String,
updated_at : {type: Date, default: Date.now },
active : {type: Boolean, default: false}
});
The routing
var Media = require('./models/media.js');
//GET all the media
app.get('/uploads/', function(req, res, next){
Media.find(function (err, media){
if (err) return next (err);
res.json(media);
});
});
//GET one item
app.get('/uploads/media/:projectId', function(req, res, next){
Media.findOne(req.params , function (err, media){
if (err) return next (err);
res.json(media);
});
});
Managing the uploads
app.use(multer({ dest: './uploads/',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
done=true;
}
}));
var Media = require('./app/models/media.js');
//POST media to the upload directory
app.post('/uploads/', function(req, res){
if(done==true){
Media.create(req.body, function(err, post){
console.log(req.files);
res.end('File uploaded');
});
}
});
Angular Controller
var app = angular.module('myApp', []);
app.controller('projectCtrl', function($scope, $http) {
$scope.myVar = false;
$scope.toggle = function() {
$scope.myVar = !$scope.myVar
};
$http.get('/profile/project/').then(function (res){
$scope.projects = res.data;
});
//GET Media
var projectId = {{projects._id}};
$http.get('/uploads/media' + projectId).succes(function(data){
console.log('Medien-Daten erhalten');
$scope.media = data;
});
});
Kind regards from Germany,
David
Update (new Errors)
Trying to implement a solution, I get problems with my scopes. When I´m adding the $http.get for the media, my other scope seem to fetch no data... they are shown like this:
Update 2(scope error fixed)
Fixed the error in the controller. I hadn´t defined a var for projectId, that caused the error.
Managed to make the GET request work, and my application is looking for entries in the database. But i can´t manage to see any..
Your use of .find is incorrect in the get all function.
See http://mongoosejs.com/docs/api.html#query_Query-find
Media.find({}, function (err, media){
if (err) return next (err);
res.json(media);
});
This will return all documents.

AngularJS reload data after PUT request

Should be a fairly easy one here for anyone who knows Angular. I am trying to update the data that is displayed after I make a PUT request to update the object. Here is some code:
Post service (services/post.js)
'use strict';
angular.module('hackaboxApp')
.factory('Post', function($resource) {
return $resource('/api/posts/:id', {id : '#id'}, {
'update': { method: 'PUT' }
})
});
Server side controller function that gets executed when trying to update data (lib/controllers/api.js)
exports.editsave = function(req, res, next) {
var posty = req.body;
console.log(posty._id.toString() + " this is posty");
function callback (err, numAffected) {
console.log(err + " " + numAffected);
if(!err) {
res.send(200);
//res.redirect('/forum');
}
}
Post.update(posty, { id: posty._id.toString() }, callback);
};
This is the console output for the above code:
53c54a0d4960ddc11495d7d7 this is posty
null 0
So as you can see, it isn't affecting any of the MongoDB documents, but it also isn't producing errors.
This is what happens on the client (Angular) side when a post is updated:
$scope.saveedit = function() {
console.log($scope.post._id + " post id");
// Now call update passing in the ID first then the object you are updating
Post.update({ id:$scope.post._id }, $scope.post, function() {$location.path('/forum')});
};
After the redirect, $location.path('/forum'), none of the data is displayed as being updated...when I look in the database...nothing has changed either...it is like I am missing the step to save the changes...but I thought that update (a PUT request) would do that for me.
I use ng-init="loadposts()" when the /forum route is loaded:
$scope.loadposts = function() {
$http.get('/api/posts').success(function (data) {$scope.posts = data});
};
Shouldn't all the new data be loaded after this? Any help would be appreciated. Thanks!
Your server side output indicate that the update query doesn't match any document in the database.
I'm guessing that you are using Mongoose in NodeJS server side code to connect to mongodb.
If that the case, your update statement seems incorrect.
Instead of { id: .. } it should be { _id: .. }
Also the conditions object and updated object are swapped.
The statement should be like this:
Post.update({ _id: posty._id.toString() }, posty, callback);
If you are not using Mongoose, please eloborate more on which library you are using or better than that, show the code where the Post variable is defined in your server side code.
Ok I got it.
the problem is that you are not using the Angular resource api correct.
This code need to be changed:
$scope.saveedit = function() {
console.log($scope.post._id + " post id");
Post.update({ id:$scope.post._id }, $scope.post, function() {$location.path('/forum')});
};
Into:
// Update existing Post
$scope.saveedit = function() {
var editedpost = new Post($scope.post); //New post object
editedpost.$update(function() {
$location.path('/forum');
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
And as for the server code (taken from my own working module):
exports.update = function (req, res) {
var post == req.post;
post = _.extend(post, req.body);
post.save(function (err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
});
} else {
res.jsonp(post);
}
});
};

Resources