Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 delete object inside directory

I have the following snippet:

import boto3

session = boto3.Session(
    aws_access_key_id="id",
    aws_secret_access_key="secret",
    region_name="us-east-1"
)

s3 = session.resource("s3")
obj = s3.Object("mybucket", "test.txt")


obj.delete()

It works fine if the file is on the root of the bucket, but I need to delete a file inside a directory. My file is under: mybucket/media/private/test.txt

Adding the path to "mybucket" or "test.txt" in the s3.Object() is not working

like image 427
Marco Herrarte Avatar asked Apr 28 '18 06:04

Marco Herrarte


1 Answers

The keyname in S3 contains also the directory path, there are no real directories in buckets.
Do it like this:

s3 = session.resource("s3")
obj = s3.Object("mybucket", "media/private/test.txt")
obj.delete()
like image 103
Evhz Avatar answered Nov 19 '22 10:11

Evhz