2011-06-29 19 views
10

Cuando intento eliminar un cubo usando las líneas:cómo eliminar una versión S3 de un cubo usando boto y pitón

conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) 

print conn.delete_Bucket('BucketNameHere').message 

Me dice el cubo traté de eliminar no está vacía.

El cucharón no tiene llaves. Pero tiene versiones.

¿Cómo puedo eliminar las versiones?

puedo ver la lista de versiones usando bucket.list_versions()

Java tiene un método deleteVersion en su conexión s3. He encontrado que el código aquí:

http://bytecoded.blogspot.com/2011/01/recursive-delete-utility-for-version.html

él hace esta línea para eliminar la versión:

s3.deleteVersion(new DeleteVersionRequest(bucketName, keyName, versionId)); 

¿Hay algo comparable en boto?

Respuesta

20

Boto es compatible con depósitos versionados después de la versión 1.9c. Así es como funciona:

import boto 

s3 = boto.connect_s3() 

#Create a versioned bucket 
bucket = s3.create_bucket("versioned.example.com") 
bucket.configure_versioning(True) 

#Create a new key and make a few versions 
key = bucket.new_key("versioned_object") 
key.set_contents_from_string("Version 1") 
key.set_contents_from_string("Version 2") 

#Try to delete bucket 
bucket.delete() ## FAILS with 409 Conflict 

#Delete our key then try to delete our bucket again 
bucket.delete_key("versioned_object") 
bucket.delete() ## STILL FAILS with 409 Conflict 

#Let's see what's in there 
list(bucket.list()) ## Returns empty list [] 

#What's in there including versions? 
list(bucket.list_versions()) ## Returns list of keys and delete markers 

#This time delete all versions including delete markers 
for version in bucket.list_versions(): 
    #NOTE we're still using bucket.delete, we're just adding the version_id parameter 
    bucket.delete_key(version.name, version_id = version.version_id) 

#Now what's in there 
list(bucket.list_versions()) ## Returns empty list [] 

#Ok, now delete the bucket 
bucket.delete() ## SUCCESS!! 
+0

Gracias! Eso funciono. – ChickenFur

+0

Acabo de ahorrarme un montón de problemas. Me estaba volviendo loco! – Sirex

+1

Deberías usar delete_keys, no delete_key. es super duper más rápido. Vea esto para la solución equivalente, pero usando delete_keys: http://stackoverflow.com/questions/29809105/how-do-i-delete-a-versioned-bucket-in-aws-s3-using-the-cli – grayaii

Cuestiones relacionadas