2012-02-10 12 views
7

Sto usando gli archivi django con il backend s3boto. Come da questo numero, http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by ho un sacco di file (tutti) che hanno il tipo di contenuto 'application/octet-stream'. Dato che ho un'istanza di <class 'boto.s3.key.Key'>, come posso impostare content_type?Usando boto, imposta content_type su file che sono già presenti su s3

In [29]: a.file.file.key.content_type 
Out[29]: 'application/octet-stream' 

In [30]: mimetypes.guess_type(a.file.file.key.name)[0] 
Out[30]: 'image/jpeg' 

In [31]: type(a.file.file.key) 
Out[31]: <class 'boto.s3.key.Key'> 

risposta

16

Non esiste alcun modo per modificare il tipo di contenuto (o qualsiasi altro metadato) associato a un file dopo che è stato creato. È possibile, tuttavia, copiare il file sul lato server e modificare i metadati nel processo. Ecco un succo su GitHub che dovrebbe aiutare:

https://gist.github.com/1791086

Contenuto:

import boto 

s3 = boto.connect_s3() 
bucket = s3.lookup('mybucket') 
key = bucket.lookup('mykey') 

# Copy the key onto itself, preserving the ACL but changing the content-type 
key.copy(key.bucket, key.name, preserve_acl=True, 
    metadata={'Content-Type': 'text/plain'}) 

key = bucket.lookup('mykey') 
print key.content_type 

Mitch

Problemi correlati