When I get object list with checkbox at browser, I can delete multiple objects like this; If I check an object, firstly appear an trash icon, wait for click and when clicked it calls jquery delete function inside getChecked function.
$('#trash').click(function () {$.del();});
And this is getChecked function;
getChecked = function () {
var result = $('input[type="checkbox"]:checked');
sec = $('input[type="checkbox"]:checked');
if (result.length > 0) {
var wbchecked = "";
params = {Bucket: 'exBucket'};
params.Delete = {};
params.Delete.Objects = [];
result.each(function () {
var paramsdel = {Bucket: 'exBucket', Prefix: result.val()};
s3.listObjects(paramsdel, function (err, data) {
if (err)
return console.log(err);
data.Contents.forEach(function (content) {
params.Delete.Objects.push({Key: content.Key});
});
});
wbchecked = $(this).val();
params.Delete.Objects.push({Key: wbchecked});
});
$("#trash").fadeIn();
$("#copy").fadeIn();
jQuery.del = function () {
var s3 = new AWS.S3();
s3.deleteObjects(params, function (err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
} else {
console.log(data); // successful response
$("#outputBox").html(result.length + " file deleted.");
$("#trash").fadeOut();
$("#move").fadeOut();
$("#copy").fadeOut();
$("#link").fadeOut();
}
});
};
} else {
$("#outputBox2").html('');
$("#trash").fadeOut();
$("#move").fadeOut();
$("#copy").fadeOut();
$("#link").fadeOut();
};
};
Here, if checked one or more object, getChecked function push them with this parametrized structure, (this is in getChecked function of course);
params = {Bucket: 'exBucket'};
params.Delete = {};
params.Delete.Objects = [];
If clicked object is a folder, first get listObjects inside it and each object get added to push. So, trash button delete all object when clicked.
Additionally, when checked an object, an icon appears named "copy" like trash. Its duty will be copy all checked objects to specified or selectable folder. How can I add this inside to the getChecked function?
For ex. in How to copy/move all objects in Amazon S3 from one prefix to other using the AWS SDK for Node.js, Aditya Manohar answered with a nice script;
var AWS = require('aws-sdk');
var async = require('async');
var bucketName = 'foo';
var oldPrefix = 'abc/';
var newPrefix = 'xyz/';
var s3 = new AWS.S3({params: {Bucket: bucketName}, region: 'us-west-2'});
var done = function(err, data) {
if (err) console.log(err);
else console.log(data);
};
s3.listObjects({Prefix: oldPrefix}, function(err, data) {
if (data.Contents.length) {
async.each(data.Contents, function(file, cb) {
var params = {
CopySource: bucketName + '/' + file.Key,
Key: file.Key.replace(oldPrefix, newPrefix)
};
s3.copyObject(params, function(copyErr, copyData){
if (copyErr) {
console.log(err);
}
else {
console.log('Copied: ', params.Key);
cb();
}
});
}, done);
}
});
How can I configure it for my getChecked function? Or any another suggestion?
Aucun commentaire:
Enregistrer un commentaire