views:

219

answers:

3

i need to write a code in python that will delete the required file from the amazon s3 bucket, i am able to make connections to the amazon s3 bucket and also able to save files, i just want to know how to delete a file? please help if anyone knows.

A: 

Via which interface? Using the REST interface, you just send a delete:

DELETE /ObjectName HTTP/1.1
Host: BucketName.s3.amazonaws.com
Date: date
Content-Length: length
Authorization: signatureValue

Via the SOAP interface:

<DeleteObject xmlns="http://doc.s3.amazonaws.com/2006-03-01"&gt;
  <Bucket>quotes</Bucket>
  <Key>Nelson</Key>
  <AWSAccessKeyId> 1D9FVRAYCP1VJEXAMPLE=</AWSAccessKeyId>
  <Timestamp>2006-03-01T12:00:00.183Z</Timestamp>
  <Signature>Iuyz3d3P0aTou39dzbqaEXAMPLE=</Signature>
</DeleteObject>

If you're using a Python library like this one, it should expose a "delete" feature (delete_key, in that particular case, which appears to be under-documented).

T.J. Crowder
yes, i am using that python library, but will that delete, the file ?should i do it this way:k.key = 'images/anon-images/small/'+filenamek.delete_key()is this correct ? please let me know.
Suhail
@Suhail: I haven't used that library, but from the source I linked, what it's actually doing is a `DELETE` call via the REST interface. So yes, despite the name "delete_key" (which I agree is unclear), it's really deleting the object *referenced* by the key.
T.J. Crowder
What about removing lot of files with a common prefix in name? Does S3 allow some bulk delete for such case, or deleting them one by one (which is slow) is the must?
Shaman
@Shaman: I'm not an S3 expert, but as far as I *know*, you can only delete a specific file. But you probably want to actually ask that as a question so it gets attention from S3 experts.
T.J. Crowder
@T.J. Crowder: Right after commenting here I've added such a question. It has 2 views yet :)
Shaman
A: 

Hi,

for now i have resolved the issue, by using the linux utility s3cmd, i used it like this in python:

delFile= 's3cmd -c /home/project/.s3cfg del s3://images/anon-images/small/' + filename

os.system(delFile)

Thank you Suhail

Suhail
A: 

found one more way to do it using the boto:

conn = S3Connection(AWS_ACCESS_KEY, AWS_SECERET_KEY)

b = Bucket(conn, S3_BUCKET_NAME)

k = Key(b)

k.key = 'images/my-images/'+filename

b.delete_key(k)
Suhail