Published on 20 Nov, 2021
In this article I only cover functions on objects. In the perspective of S3 files and folders are both objects, and folder is an object that contains in its key a
First of all, there are two maintained versions of the AWS SDK, version 2 and version 3.
It is a bit confusing, because on the documentation page of version 2 they sometimes refer to latest version, which however means the last version of version 2.
Version 3 moved to a new documentation page.
Also some links in the version 3 documentation refer to the version 2 documentation so be aware of it, and maybe search manual for informations in the version 3 documentation.
First of all installation
To use the aws-sdk you have to install it, here is the npm repository link.
npm install --save @aws-sdk/client-s3
Changes in rollup
TL;DR: You may not have this issue, it can relate to my enviroment. By the way, I am using svelte and rollup for building and bundling.
I had some problems with a crypto module that the AWS SDK is using. I got an error that the variable
exports is not defined.
I used already the
commonjs plugin, but it doesn't help.
Uncaught ReferenceError: exports is not defined
After a while and with help we found a dirty fix, I adjusted my
rollup.conf.js and add a replace statement to define exports.
Rename a s3 object
As there is no rename or move method on the s3 client, you have to copy and delete the old file (seriously no joke).
Be aware that the copy will increase your used storage and can be costful.
It could look like this.
List top level objects in current folder
If you just want to list the objects in the current folder, you have specify
/ as delimiter in the ListObjectsV2Command, but then folders will not more contained in
Contents attribute of the response, but in the
Just see the example on npmjs @aws-sdk/lib-storage.
If you want to delete a folder with content, you first have to delete the content of the folder.
For this the command
DeleteObjectsCommand exists. Search for all objects inside a folder and give the list to this command.
After you have done this, just delete the folder.
Sadly there is no full text search, you can just search for a prefix with the
Download files from s3 is pretty easy, but you need to use a signedUrl.
Originally published at https://ngotzmann.net.