To create a script that downloads files using the wget command and uploads them to an S3 bucket, you can use a combination of shell commands along with the AWS CLI (assuming you have AWS CLI installed and configured with the appropriate access rights).
Below is a sample shell script that demonstrates how to perform these actions:
- It uses
wget to download zip files from the specified URL.
- It uses the
aws s3 cp command to upload the downloaded files to an S3 bucket.
Make sure to replace your-s3-bucket-name with your actual S3 bucket name.
Shell Script
#!/bin/bash
# Set the URL from which to download files
URL="https://bulkdata.uspto.gov/data/patent/grant/redbook/fulltext/2024"
# Set the S3 bucket name (replace with your bucket name)
S3_BUCKET="your-s3-bucket-name"
# Create a directory for storing downloaded files if it doesn't exist
mkdir -p downloaded_files
# Change to the created directory
cd downloaded_files
# Download files using wget
echo "Downloading files from $URL..."
wget -r -np -l1 -nd -A zip "$URL"
# Check if download was successful
if [ $? -eq 0 ]; then
echo "Download completed. Uploading files to S3..."
# Upload each downloaded zip file to S3
for file in *.zip; do
if [ -f "$file" ]; then
aws s3 cp "$file" "s3://$S3_BUCKET/$file"
echo "Uploaded $file to s3://$S3_BUCKET/$file"
else
echo "No zip files found to upload."
fi
done
echo "Upload completed."
else
echo "Download failed."
exit 1
fi
# Optionally, clean up by removing downloaded files
# cd ..
# rm -rf downloaded_files
Instructions to Use the Script
-
Prerequisites:
- Make sure you have
wget and AWS CLI installed. You can install wget using your package manager (e.g., sudo apt-get install wget on Ubuntu) and install AWS CLI by following these instructions.
- Configure the AWS CLI with your credentials using
aws configure.
-
Save the Script:
- Copy the script above and save it to a file (e.g.,
download_and_upload.sh).
-
Make the Script Executable:
chmod +x download_and_upload.sh
-
Run the Script:
./download_and_upload.sh
Notes:
- The script uses
mkdir -p to create a directory for downloads if it doesn't exist, and it changes to that directory before executing the download.
- The loop iterates over each
.zip file in the downloaded_files directory and uploads it to the specified S3 bucket.
- There’s an optional cleanup section commented out at the end of the script to remove the downloaded files if you don’t need them locally after upload. Uncomment it if you want that behavior.
- You may need to modify the script according to your needs, such as handling errors or logging.