DEV Community

Cover image for How to delete file from S3 using API
8 1 2 1 2

How to delete file from S3 using API

I wanted to show, how you can delete data from S3 using API. With a simple curl, you can delete files from S3. In the following example, I delete the test_empty.txt file from the bucket named BUCKET_NAME from the eu-west-1 region. For correct operation, you must provide ADD_YOUR_KEY_ID and ADD_YOUR_SECRET_KEY_VALUE .

#!/usr/bin/env bash

access_key_id=ADD_YOUR_KEY_ID
secret_key=ADD_YOUR_SECRET_KEY_VALUE
file_path=test_empty.txt
bucket_name=BUCKET_NAME
region=eu-west-1



# metadata
content_Type="application/octet-stream"
date=`date -R`
full_path="/${bucket_name}/${file_path}"
signature_string="GET\n\n${content_Type}\n${date}\n${full_path}"
signature=`echo -en ${signature_string} | openssl sha1 -hmac ${secret_key} -binary | base64`

# save file in file_path ( -o, --output <file>)(-s, --silent)(-S disable progress meter but still show error messages)
curl -Sso ${file_path} \
  -H "Host: ${bucket_name}.s3.${region}.amazonaws.com" \
  -H "Date: ${date}" \
  -H "Content-Type: ${content_Type}" \
  -H "Authorization: AWS ${access_key_id}:${signature}" \
  -X "GET" https://${bucket_name}.s3.${region}.amazonaws.com/${file_path}
Enter fullscreen mode Exit fullscreen mode

By making a small change, you can download the file first and then remove it from S3. More information can be found on my blog.

I also recorded a video on YouTube in which I show 7 ways to delete data from S3 - https://youtu.be/SckbCTULsHM

You'd rather read than watch, no problem. On my blog you will also find an article in which I described 7 ways to delete data from S3 - https://lepczynski.it/en/aws_en/7-ways-to-delete-data-from-s3/

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Best Practices for Running  Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK cover image

Best Practices for Running Container WordPress on AWS (ECS, EFS, RDS, ELB) using CDK

This post discusses the process of migrating a growing WordPress eShop business to AWS using AWS CDK for an easily scalable, high availability architecture. The detailed structure encompasses several pillars: Compute, Storage, Database, Cache, CDN, DNS, Security, and Backup.

Read full post

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay