2012-03-23 4 views
13

s3 백엔드가 아니라 s3boto 백엔드를 사용하고 있습니다. django-storages docs에서django-storage 및 s3boto 백엔드를 사용하여 브라우저가 이미지를 캐시 할 수 있도록 이미지의 헤더를 요청하는 캐싱 정보를 어떻게 추가합니까?

은 당신의 settings.py 파일에 AWS_HEADERS 변수를 지정 말한다 :

당신이 각 파일과 함께 전송 헤더를 설정하려는 경우

(선택 사항) AWS_HEADERS 저장 :

# see http://developer.yahoo.com/performance/rules.html#expires 
AWS_HEADERS = { 
'Expires': 'Thu, 15 Apr 2010 20:00:00 GMT', 
'Cache-Control': 'max-age=86400', 
} 

이 나를 위해 작동하지 않습니다.

class Photo(models.Model): 
    """ 
     docstring for Photo 
     represents a single photo.. a photo can have many things associated to it like 
     a project, a portfolio, etc... 
    """ 

    def image_upload_to(instance, filename): 
     today = datetime.datetime.today() 
     return 'user_uploads/%s/%s/%s/%s/%s/%s/original/%s' % (instance.owner.username, today.year, today.month, today.day, today.hour, today.minute, filename) 

    def thumb_upload_to(instance, filename): 
     today = datetime.datetime.today() 
     return 'user_uploads/%s/%s/%s/%s/%s/%s/thumb/%s' % (instance.owner.username, today.year, today.month, today.day, today.hour, today.minute, filename) 

    def medium_upload_to(instance, filename): 
     today = datetime.datetime.today() 
     return 'user_uploads/%s/%s/%s/%s/%s/%s/medium/%s' % (instance.owner.username, today.year, today.month, today.day, today.hour, today.minute, filename) 



    owner = models.ForeignKey(User) 
    # take out soon 
    projects = models.ManyToManyField('Project', through='Connection', blank=True) 
    image = models.ImageField(upload_to=image_upload_to) 
    thumb = ThumbnailerImageField(upload_to=thumb_upload_to, resize_source=dict(size=(102,102), crop='center'),) 
    medium = ThumbnailerImageField(upload_to=medium_upload_to, resize_source=dict(size=(700,525),)) 
    title = models.CharField(blank=True, max_length=300) 
    caption = models.TextField(blank=True) 
    can_view_full_res = models.BooleanField(default=False) 
    is_portfolio = models.BooleanField(default=False) 
    created_time = models.DateTimeField(blank=False, auto_now_add=True) 
    disabled = models.DateTimeField(blank=True, null=True, auto_now_add=False) 
    cost = models.FloatField(default=0) 
    rating = models.IntegerField(default=0) 
    mature_content = models.BooleanField(default=False) 
    objects = ViewableManager() 

    def get_absolute_url(self): 
     return "/m/photo/%i/" % self.pk 

    def get_prev_by_time(self): 
     get_prev = Photo.objects.order_by('-created_time').filter(created_time__lt=self.created_time) 
     try: 
      return get_prev[0] 
     except IndexError: 
      return None 

    def get_next_by_time(self): 
     get_next = Photo.objects.order_by('created_time').filter(created_time__gt=self.created_time) 
     try: 
      return get_next[0] 
     except IndexError: 
      return None 

    def __unicode__(self): 
     return(self.title) 

이 나는 ​​이미지를 내 템플릿에 무엇인가 ... 여기

<img class='shadow' src='{{ object.medium.url }}'> 

은 요청 및 응답 헤더를 다음과 같습니다 :

Request URL:https://MY_UPLOAD_CONTAINER.s3.amazonaws.com/user_uploads/travismillward/2012/3/23/3/0/medium/_0677866898.jpg?Signature=s%2ByKsWDxrDJbyeVHd%2BDS3JlByts%3D&Expires=1332529522&AWSAccessKeyId=MY_ACCESS_KEYID 
Request Method:GET 
Status Code:200 OK 
Request Headersview parsed 
GET /user_uploads/travismillward/2012/3/23/3/0/medium/_0677866898.jpg?Signature=s%2ByKsWDxrDJbyeVHd%2BDS3JlByts%3D&Expires=1332529522&AWSAccessKeyId=MY_ACCESS_KEYID HTTP/1.1 
Host: MY_UPLOAD_CONTAINER.s3.amazonaws.com 
Connection: keep-alive 
Cache-Control: max-age=0 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11 
Accept: */* 
Referer: http://localhost:8000/m/photo/1/ 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Query String Parametersview URL encoded 
Signature:s+yKsWDxrDJbyeVHd+DS3JlByts= 
Expires:1332529522 
AWSAccessKeyId:MY_ACCESS_KEYID 
Response Headersview parsed 
HTTP/1.1 200 OK 
x-amz-id-2: wOWRRDi5TItAdiYSPf8X4z4I4v5/Zu8XLhwlxmZa8w8w1Jph8WQkenihVJI/ZKnV 
x-amz-request-id: THE_X_AMZ_REQUEST_ID 
Date: Fri, 23 Mar 2012 18:05:24 GMT 
Cache-Control: max-age=86400 
Last-Modified: Fri, 23 Mar 2012 09:00:13 GMT 
ETag: "6e34e718a349e0bf9e4aefc1afad3d7d" 
Accept-Ranges: bytes 
Content-Type: image/jpeg 
Content-Length: 91600 
Server: AmazonS3 
여기

내 모델입니다

주소 표시 줄에 이미지 경로를 붙여 넣으면 이미지가 캐시되고 304가 표시됩니다. 여기에 t가 있습니다. 호스 요청 및 응답 헤더 :

Request URL:https://MY_UPLOAD_CONTAINER.s3.amazonaws.com/user_uploads/travismillward/2012/3/23/3/0/medium/_0677866898.jpg?Signature=evsDZiw3QGsjPacG4CHn6Ji2dDA%3D&Expires=1332528782&AWSAccessKeyId=MY_ACCESS_KEYID 
Request Method:GET 
Status Code:304 Not Modified 
Request Headersview parsed 
GET /user_uploads/travismillward/2012/3/23/3/0/medium/_0677866898.jpg?Signature=evsDZiw3QGsjPacG4CHn6Ji2dDA%3D&Expires=1332528782&AWSAccessKeyId=MY_ACCESS_KEYID HTTP/1.1 
Host: MY_UPLOAD_CONTAINER.s3.amazonaws.com 
Connection: keep-alive 
Cache-Control: max-age=0 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.83 Safari/535.11 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Referer: http://localhost:8000/m/photo/1/ 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
If-None-Match: "6e34e718a349e0bf9e4aefc1afad3d7d" 
If-Modified-Since: Fri, 23 Mar 2012 09:00:13 GMT 
Query String Parametersview URL encoded 
Signature:evsDZiw3QGsjPacG4CHn6Ji2dDA= 
Expires:1332528782 
AWSAccessKeyId:MY_ACCESS_KEYID 
Response Headersview parsed 
HTTP/1.1 304 Not Modified 
x-amz-id-2: LfdHa10SdWnx4UH1rc62NfUDeiNVGRzBX73CR+6GDrXJgv9zo+vyQ9A3HCr1YLVa 
x-amz-request-id: THE_X_AMZ_REQUEST_ID 
Date: Fri, 23 Mar 2012 18:01:16 GMT 
Last-Modified: Fri, 23 Mar 2012 09:00:13 GMT 
ETag: "6e34e718a349e0bf9e4aefc1afad3d7d" 
Server: AmazonS3 

답변

19

감사합니다.이 대답을 주신 blackrobot에게 감사드립니다.

은 당신의 settings.py에 다음을 추가

난 같은 문제가, 내가 설정 한 한
AWS_QUERYSTRING_AUTH = False 
+1

'AWS_QUERYSTRING_AUTH = FALSE '와 여전히 도움이됩니다. s3에 대한 요청은 w.o auth 부분으로 만들어졌지만 AWS_HEADERS가 작동하고 있다는 것을 보여주는 것은 아무것도 없습니다. – Neara

+0

@ 알아보기 요청 헤더를 붙여 넣기 보관함에 복사/붙여 넣기하고 링크를 제공합니까? 나는 한번 볼게. – teewuane

+0

'Expires' 포맷에 오류가있어서 아마존이 사용하지 않았기 때문에 추측 할 수 있습니다. 수정 된 후에 다시 배포 한 후 설정에서 '캐시 제어'및 '만료'를 볼 수 있습니다. 나 좀 도와 줘서 고마워, 정말로 고마워. :) – Neara