npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

s3-bucket-multipart-toolkit

v0.0.23

Published

S3 Bucket Listing and Versioning removal made easy

Downloads

131

Readme

s3-bucket-toolkit

CircleCI

S3 Bucket APIs calls made easy

  • Provide Promisified AWS API methods
  • Use recommended listObjectsV2
  • Paginate or list all items from the bucket
  • Upload multiple files
  • Delete all Versions and Markers from a particular file

Install

$ npm i s3-bucket-toolkit --save

Configure

const AWSBucket = require('s3-bucket-toolkit');

const bucket = new AWSBucket({
  accessKeyId: 'your-access-key-here',
  secretAccessKey: 'your-secret-here',
  region: 'us-east-1',
  bucketACL: 'public-read',
  bucketName: 'my-bucket',
  pagingDelay: 500, // (optional) set a global delay in between s3 api calls, default: 500ms
});

Usage

Get Upload URL

Get upload URL

bucket.getUploadUrl({
  ContentType: 'application/javascript',
  Key: 'your-dir/test.js'
}).then(function(res){
  /* Signed URL => res.signedUrl */
}).catch(function(err){
  /* err */
});

/*
Result:
{
  signedUrl: 'https://your-bucket.s3.amazonaws.com/your-dir/test.js?AWSAccessKeyId=...'
}
*/

Upload File

bucket.uploadFile({
  filePath: './test/upload-test.txt',
  Key: 'upload-test.txt'
}).then(function(res){
  /* res.url => S3 upload url */
}).catch(function(err){
  /* err */
});

/*
 Result:
{ response: { ETag: '"abc.."' },
  url: 'https://my-bucket.s3.amazonaws.com/upload-test.txt' }
*/

Copy File

copyFile(Object, Boolean) function can copy files between buckets or in the same bucket. Pass a second parameter as true when copying files inside the same bucket (will automatically append the bucket prefix). Otherwise pass the entire bucket URL.

bucket.copyFile({
  CopySource: 'upload-test.txt',
  Key: 'upload-test-copied.txt'
}, true).then(function(res){
  /* res.url => S3 copy url */
}).catch(function(err){
  /* err */
});

/*
 Result:
{ response: { ETag: '"abc..."', CopySourceVersionId: 'def...', CopyObjectResult: { ... } },
  url: 'https://your-bucket-name.s3.amazonaws.com/upload-test-copied.txt' } }
*/

Copy File Multipart

copyFileMultipart(Object, Boolean, fileSize) function can copy files greater than 5mb between buckets or in the same bucket. Pass a second parameter as true when copying files inside the same bucket (will automatically append the bucket prefix). Otherwise pass the entire bucket URL.

bucket.copyFileMultipart({
  CopySource: 'upload-test.txt',
  Key: 'upload-test-copied.txt',
}, true, 50000000).then(function(res){
  /* res.url => S3 copy url */
}).catch(function(err){
  /* err */
});

/*
 Result:
{ response: { ETag: '"abc..."', CopySourceVersionId: 'def...', CopyObjectResult: { ... } },
  url: 'https://your-bucket-name.s3.amazonaws.com/upload-test-copied.txt' } }
*/

Upload Multiple Files

bucket.uploadMultipleFiles({
  files: [{
    filePath: './test/upload-test-1.txt',
    Key: 'upload-test-1.txt'
  }, {
    filePath: './test/upload-test-2.txt',
    Key: 'upload-test-2.txt'
  }]
}).then(function(res){
  /* res => Array of file upload status responses */
}).catch(function(err){
  /* err */
});

/*
 Result:
[ { response:
   { ETag: '"abc..."',
     VersionId: '123..' },
  url: 'https://your-bucket.s3.amazonaws.com/upload-test-1.txt' },
{ response:
   { ETag: '"def.."',
     VersionId: '456...' },
  url: 'https://your-bucket.s3.amazonaws.com/upload-test-2.txt' } ]
*/

List Files

bucket.listFiles({
  limit: 2, // items per page by default 1000 (optional)
  delay: 10, // delay between pages by default 500 (optional)
}).then(function(files){
  /* files => array containing all files from the bucket */
}).catch(function(err){
  /* err */
});

/*
Result:
[ { Key: 'upload-test-1.txt',
    LastModified: 2018-05-04T06:49:48.000Z,
    ETag: '"abc..."',
    Size: 44,
    StorageClass: 'STANDARD' },
  { Key: 'upload-test-2.txt',
    LastModified: 2018-05-04T06:49:49.000Z,
    ETag: '"def..."',
    Size: 44,
    StorageClass: 'STANDARD' },
    ...
]
*/

List Paged Files

Pagination, see more parameters listObjectsV2 docs

bucket.listPagedFiles({
  limit: 2, // optional items per page is a custom implementation of MaxKeys by default 1000
bucket.listFiles().then(function(res){
  /* res.contents => bucket contents */
}).catch(function(err){
  /* err */
});

/*
Result:
{ IsTruncated: false,
  Contents:
   [ { Key: 'upload-test.txt',
       LastModified: 2018-04-15T22:48:27.000Z,
       ETag: '"abc..."',
       Size: 26,
       StorageClass: 'STANDARD' } ],
  Name: 'my-bucket',
  Prefix: '',
  MaxKeys: 1000,
  CommonPrefixes: [],
  KeyCount: 1 }
*/

List File Versions

The param Key is being replaced internally in fovor of Prefix, see the docs s3.listObjectVersions

bucket.listFileVersions({
  Key: 'upload-test-versioned.txt', // file versioned key or prefix (mandatory)
  limit: 2, // items per page by default 1000 (optional)
  delay: 10, // delay between pages by default 500 (optional)
}).then(function(res){
  /* res.Versions => Versions found */
}).catch(function(err){
  /* err */
});

/*
Result:
{
  Versions:
   [ { ETag: '"abc..."',
       Size: 44,
       StorageClass: 'STANDARD',
       Key: 'upload-test.txt',
       VersionId: 'null',
       IsLatest: true,
       LastModified: 2018-05-02T12:53:29.000Z,
       Owner: [Object] } ],
  DeleteMarkers: []
}
*/

Delete Files

bucket.deleteFiles({
    files: ['upload-test.txt']
  }).then(function(res){
  /* res.Deleted => Deleted contents */
  done();
}).catch(function(err){
  /* err */
});

/*
Result:
{ Deleted: [ { Key: 'upload-test.txt' } ], Errors: [] }
*/

Delete All Versions from a Single File

Delete all versions for a given file

bucket.deleteAllVersions({
  Key: 'upload-test.txt',
}).then(function(res){
  /* res.Deleted => Deleted versions of single file */
  done();
}).catch(function(err){
  /* err */
});

/*
Result:
{ Deleted:
   [ { Key: 'upload-test.txt',
       VersionId: 'abc...' } ],
  Errors: [] }
*/

Delete All Markers from a Single File

Delete all Markers for a given file

bucket.deleteAllMarkers({
  Key: 'upload-test.txt',
}).then(function(res){
  /* res.Deleted => Deleted markers of single file */
}).catch(function(err){
  /* err */
});

Delete All Versions and Markers from a Single File

Delete all Versions and Markers for a given file

bucket.deleteAllVersionsAndMarkers({
  Key: 'upload-test.txt',
}).then(function(res){
  /* res.Deleted => Deleted versions and markers of single file */
}).catch(function(err){
  /* err */
});

Delete Specific Versions from files

bucket.deleteFilesVersioned({
  files: [{
      Key: 'upload-test.txt',
      // 'null' means latest version
      VersionId: 'null'
    }, {
      // you can repeat the same file
      Key: 'upload-test.txt',
      VersionId: 'abc...'
    }, {
      // several times
      Key: 'upload-test.txt',
      VersionId: 'def...'
    }, {
      // OR you can mix passing multiple files and versions
      Key: 'other-file.txt',
      VersionId: 'ghi...'
  }]
}).then(function(res){
  /* res.Deleted => Deleted versions and files specified */
  done();
}).catch(function(err){
  /* err */
});

/*
Result:
{ Deleted:
   [ { Key: 'upload-test.txt',
       VersionId: 'abc...' } ],
  Errors: [] }
*/

Get All buckets

Get All buckets for this account

bucket.getAllBuckets().then(function(res) {
  /* Buckets => res.Buckets */
}).catch(function(err) {
  /* err */
});

/*
Result:
{
  Buckets:
   [ { Name: 'my-bucket',
       CreationDate: 2018-03-19T17:49:05.000Z } ],
  Owner:
   { DisplayName: 'cris',
     ID: '...' }
}
*/

References