multer-s3

Streaming multer storage engine for AWS S3

Multer S3

Streaming multer storage engine for AWS S3.

This project is mostly an integration piece for existing code samples from Multer's storage engine documentationwith s3fsas the substitution piece for file system. Existing solutions I found required buffering the multipart uploads into the actual filesystem which is difficult to scale.

Installation

npm install --save multer-s3

Usage

var aws = require('aws-sdk')
var express = require('express')
var multer = require('multer')
var multerS3 = require('multer-s3')

var app = express()
var s3 = new aws.S3({ /* ... */ })

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

app.post('/upload', upload.array('photos', 3), function(req, res, next) {
  res.send('Successfully uploaded ' + req.files.length + ' files!')
})

File information

Each file contains the following information exposed by multer-s3:

KeyDescriptionNote
sizeSize of the file in bytes
bucketThe bucket used to store the fileS3Storage
keyThe name of the fileS3Storage
aclAccess control for the fileS3Storage
contentTypeThe mimetypeused to upload the fileS3Storage
metadataThe metadataobject to be sent to S3S3Storage
locationThe S3 urlto access the fileS3Storage
etagThe etagof the uploaded file in S3S3Storage
contentDispositionThe contentDispositionused to upload the fileS3Storage
storageClassThe storageClassto be used for the uploaded file in S3S3Storage
versionIdThe versionIdis an optional param returned by S3 for versioned buckets.S3Storage

Setting ACL

ACL valuescan be set by passing an optional aclparameter into the multerS3object.

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    acl: 'public-read',
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Available options for canned ACL.

ACL OptionPermissions added to ACL
privateOwner gets FULL_CONTROL. No one else has access rights (default).
public-readOwner gets FULL_CONTROL. The AllUsersgroup gets READaccess.
public-read-writeOwner gets FULL_CONTROL. The AllUsersgroup gets READand WRITEaccess. Granting this on a bucket is generally not recommended.
aws-exec-readOwner gets FULL_CONTROL. Amazon EC2 gets READaccess to GETan Amazon Machine Image (AMI) bundle from Amazon S3.
authenticated-readOwner gets FULL_CONTROL. The AuthenticatedUsersgroup gets READaccess.
bucket-owner-readObject owner gets FULL_CONTROL. Bucket owner gets READaccess. If you specify this canned ACL when creating a bucket, Amazon S3 ignores it.
bucket-owner-full-controlBoth the object owner and the bucket owner get FULL_CONTROLover the object. If you specify this canned ACL when creating a bucket, Amazon S3 ignores it.
log-delivery-writeThe LogDeliverygroup gets WRITEand READ_ACPpermissions on the bucket. For more information on logs.

Setting Metadata

The metadataoption is a callback that accepts the request and file, and returns a metadata object to be saved to S3.

Here is an example that stores all fields in the request body as metadata, and uses an idparam as the key:

var opts = {
    s3: s3,
    bucket: config.originalsBucket,
    metadata: function (req, file, cb) {
      cb(null, Object.assign({}, req.body));
    },
    key: function (req, file, cb) {
      cb(null, req.params.id + ".jpg");
    }
  };

Setting Cache-Control header

The optional cacheControloption sets the Cache-ControlHTTP header that will be sent if you're serving the files directly from S3. You can pass either a string or a function that returns a string.

Here is an example that will tell browsers and CDNs to cache the file for one year:

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    cacheControl: 'max-age=31536000',
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Setting Custom Content-Type

The optional contentTypeoption can be used to set Content/mime type of the file. By default the content type is set to application/octet-stream. If you want multer-s3 to automatically find the content-type of the file, use the multerS3.AUTO_CONTENT_TYPEconstant. Here is an example that will detect the content type of the file being uploaded.

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

You may also use a function as the contentType, which should be of the form function(req, file, cb).

Setting StorageClass

storageClass valuescan be set by passing an optional storageClassparameter into the multerS3object.

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    acl: 'public-read',
    storageClass: 'REDUCED_REDUNDANCY',
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Setting Content-Disposition

The optional contentDispositionoption can be used to set the Content-Dispositionheader for the uploaded file. By default, the contentDispositionisn't forwarded. As an example below, using the value attachmentforces the browser to download the uploaded file instead of trying to open it.

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    acl: 'public-read',
    contentDisposition: 'attachment',
    key: function (req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Using Server-Side Encryption

An overview of S3's server-side encryption can be found in the [S3 Docs] (http://docs.aws.amazon.com/AmazonS3/latest/dev/serv-side-encryption.html); be advised that customer-managed keys (SSE-C) is not implemented at this time.

You may use the S3 server-side encryption functionality via the optional serverSideEncryptionand sseKmsKeyIdparameters. Full documentation of these parameters in relation to the S3 API can be found [here] (http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#upload-property) and [here] (http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html).

serverSideEncryptionhas two valid values: 'AES256' and 'aws:kms'. 'AES256' utilizes the S3-managed key system, while 'aws:kms' utilizes the AWS KMS system and accepts the optional sseKmsKeyIdparameter to specify the key ID of the key you wish to use. Leaving sseKmsKeyIdblank when 'aws:kms' is specified will use the default KMS key. Note:You must instantiate the S3 instance with signatureVersion: 'v4'in order to use KMS-managed keys [[Docs]] (http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingAWSSDK.html#specify-signature-version), and the specified key must be in the same AWS region as the S3 bucket used.

var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'some-bucket',
    acl: 'authenticated-read',
    contentDisposition: 'attachment',
    serverSideEncryption: 'AES256',
    key: function(req, file, cb) {
      cb(null, Date.now().toString())
    }
  })
})

Testing

The tests mock all access to S3 and can be run completely offline.

npm test

HomePage

https://github.com/badunk/multer-s3#readme

Repository

https+https://github.com/badunk/multer-s3


上一篇:rentophila
下一篇:zmok-server

相关推荐

  • 选择某类的最后一个元素——CSS3伪类选择器走过的坑

    众所周知,在CSS3规范中,添加了许多了伪类选择器,这些选择器代替了不少JS的工作,让我们从以前通过获取DOM节点进而进行样式修改的操作,变得更加便捷高效。 然而我在近期开发使用的过程中,遇到了不少坑...

    4 个月前
  • 进入 CSS3 动画

    我最近有机会深入研究一些CSS3动画。 我使用了像animate.css这样的库,用javascript完成了动画,但从未做过任何自定义的CSS3工作 原文(https://codeguide.cn/...

    10 个月前
  • 超详细教程:纯CSS3写一个摇头晃脑的小哥

    1.制作背景 1.1作为一个刚刚打算要入行的准前端,并没有什么基础,暂时是按照网上的前辈们的指导,按部就班地学习中。首先就要学习CSS3的使用。前期,做过几个比较简单的网站首页的仿制,现在想要试试...

    3 年前
  • 背景&边框之魔法武器CSS3

    简述 在页面的构建过程中,离不开背景、边框,背景和边框就是最基本的设置了 UI为了实现一些特定的视觉效果,单一的纯色背景已经不能满足现有的效果了。越来越多的图片背景开始盛行。

    2 年前
  • 纯css3漂亮的checkbox和radio美化效果插件Pretty.css插件

    插件介绍 pretty.css是一款纯css3漂亮的checkbox和radio美化效果。pretty.css可以和多种字体图标结合使用,对原生的checkbox和radio进行美化,还可以制作按...

    1 年前
  • 纯css3实现二维码扫描特效

    先看效果: image.png(/public/upload/d8689daebae0f323d1eba4dd459abc23) 第一步,实现网格背景: 第二部实现扫码线以及渐变背景特...

    4 个月前
  • 纯 CSS3 实现漂亮的 input 输入框动画样式库-Text input love

    分享一个用纯 CSS3 实现的,漂亮的 input 输入框动画样式库Text input love。 点击每个输入框都用不同的动画效果,始终显示标签label,并显示 placeholder(占位符...

    2 年前
  • 神奇的CSS3混合模式

    对于前端开发人员应该都很熟悉Photoshop的图层混合模式,就是几个图层按不同的模式进行混合,实现不同的图像效果。但是当我们前端同学在切这些效果图的时候,基本上就是一刀切的,即使是文字,当字体很炫...

    2 年前
  • 用css动态实现圆环百分比分配——初探css3动画

    最近的小程序项目有个设计图要求做一个圆环,两种颜色分配,分别代表可用金额和冻结金额。要是就直接这么显示,感觉好像挺没水平??于是我决定做个动态! 在mdn把新特性 都看了一遍,不禁感叹css牛逼!这...

    2 年前
  • 用css3实现惊艳面试官的背景即背景动画(高级附源码)

    clipboard.png(https://img.javascriptcn.com/df0a784006fe932cfd3de7e2bf52e26c "clipboard.png") 我们传统的前...

    9 个月前

官方社区

扫码加入 JavaScript 社区