These are some useful scripts to work with AWS.
In order to execute any of this scripts, you must install boto3 and you must set up your authentication credentials.
List of scripts:
===
The script count-objects.py
counts the total objects within a bucket.
Optionally, a prefix may be passed in order to count the total objects
that starts with that prefix.
Usage:
$ python count-objects.py -h
usage: count-objects.py [-h] -b BUCKET_NAME [-p PREFIX]
Command to count the number of files within a bucket in AWS
optional arguments:
-h, --help show this help message and exit
-b BUCKET_NAME, --bucket-name BUCKET_NAME
Bucket name
-p PREFIX, --prefix PREFIX
Prefix for files
Examples of usage.
$ python count-objects.py -b bucket_name
Output:
There are a total of 3099 object(s) in bucket_name bucket
$ python count-objects.py -b bucket_name -p xxxx
Output:
There are a total of 10 object(s) with prefix xxxx out of 3099 in bucket_name bucket
===
The script update-content-type.py
updates the Content-Type of all
objects within a bucket. Optionally, it creates a backup of the
original object.
The workflow for changing the content-type of an existing object is:
- Backup the object creating a new one with another name and applying the metadata and permissions required
- Remove the original object
- Restore the backed up object into the original one, applying the metadata and permissions required
- (Optional) Remove the backup file
Usage:
$ python update-content-type.py -h
usage: update-content-type.py [-h] -b BUCKET_NAME -c CONTENT_TYPE -p
BACKUP_PREFIX [--backup]
Command to change the Content-Type of files within a bucket of AWS
optional arguments:
-h, --help show this help message and exit
-b BUCKET_NAME, --bucket-name BUCKET_NAME
Bucket name
-c CONTENT_TYPE, --content-type CONTENT_TYPE
Content-Type to set files to
-p BACKUP_PREFIX, --backup-prefix BACKUP_PREFIX
Prefix for backups
--backup Whether to preserve the backups created
Examples of usage.
$ python update-metadata.py -b bucket_name -p xxxx -c audio/mpeg
Output:
A total of 3099 objects within bucket bucket_name have been updated
$ python update-metadata.py -b bucket_name -p xxxx -c audio/mpeg --backup
Output:
A total of 3099 objects within bucket bucket_name have been updated