从Node.JS(JavaScript)擦除AWS/S3对象 [英] Erasing AWS/S3 objects from Node.JS (JavaScript)

查看:158
本文介绍了从Node.JS(JavaScript)擦除AWS/S3对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用mLab作为数据库的Node.JS应用程序,还有一个使用AWS/S3来存储文件的应用程序.我刚刚实现了一项功能,该功能允许用户从mLab数据库中删除信息,效果很好.

I have a Node.JS app using mLab as a DB and also AWS/S3 to store files. I just implemented a functionality allowing the user to erase information from the mLab DB, this works fine.

我仍然要做的一件事是从AWS/S3中删除可能的相关信息,而我还不知道该怎么做.

The one thing I still have to do is to erase possible relevant information from AWS/S3, and this I don't know how to do yet.

我已经浏览了网上,看到了几件事,但是没有什么令人满意的.

I have already browsed the net and seen a couple of things, but nothing quite satisfying.

有人可以准确地告诉我(如果可能的话,简单明了)我需要做什么(必要的npm模块,如果有的话,等等...),以便能够从我的AWS/S3存储桶中删除对象.如果有的话,关于该主题的好的教程也将很好.

Can someone tell exactly (if possible clearly and simply) what I need to do (necessary npm module if there is, etc...) to be able to delete objects from my AWS/S3 bucket. A good tutorial on the subject would also be fine if there is any.

推荐答案

AWS提供了设置任何适合您的凭据.接下来安装sdk:

AWS provides an SDK for JavaScript. First, ensure that you have set your credentials in whatever way makes sense for you. Next install the sdk:

npm i aws-sdk

删除对象桶:

const AWS = require('aws-sdk');
const s3 = new AWS.S3();

const params = {
  Bucket: 'examplebucket', 
  Key: 'objectkey.jpg'
};

s3.deleteObject(params, function(err, data) {
  if (err) {
    console.log(err, err.stack); // an error occurred
  else {
    console.log(data);           // successful response
  }
});

一些注意事项:

  • 还有一个 deleteObjects 一次调用即可删除多个对象的功能.
  • 可选,但建议使用,如果您使用的是最新版本的节点,则可以使用util.promisify 将AWS sdk使用的回调样式转换为Promise.
  • There is also a deleteObjects function that can delete multiple objects with one call.
  • Optional but recommended, if you are using a recent version of node, you can use util.promisify to turn the callback style that the AWS sdk uses into promises.

这篇关于从Node.JS(JavaScript)擦除AWS/S3对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆