2012-02-10 2 views
7

s3boto 백엔드와 함께 django 저장소를 사용하고 있습니다. 이 문제에 따라 http://code.larlet.fr/django-storages/issue/5/s3botostorage-set-content-type-header-acl-fixed-use-http-and-disable-query-auth-by 콘텐츠 유형이 'application/octet-stream'인 여러 파일 (모두)이 있습니다. <class 'boto.s3.key.Key'>이라는 인스턴스가 있다고 가정하면 어떻게 content_type을 설정할 수 있습니까?boto를 사용하여 이미 s3에있는 파일에 content_type을 설정하십시오.

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'> 

답변

16

파일을 만든 후에는 파일과 관련된 콘텐츠 형식 (또는 기타 메타 데이터)을 수정할 방법이 없습니다. 그러나 서버 측에서 파일을 복사하고 프로세스에서 메타 데이터를 수정할 수 있습니다.

https://gist.github.com/1791086

내용 :

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 

미치

여기에 도움이 GitHub의에 요점이다
관련 문제