2013-07-06 8 views
7

मैं अपनी स्थिर फ़ाइलों के लिए django-storages और amazon s3 का उपयोग कर रहा हूं। प्रलेखन के बाद, मैं अपने settings.pyDjango-Storages बदले गए स्थिर फ़ाइलों का पता नहीं लगा

STATIC_URL = 'https://mybucket.s3.amazonaws.com/' 

ADMIN_MEDIA_PREFIX = 'https://mybucket.s3.amazonaws.com/admin/' 

INSTALLED_APPS += (
    'storages', 
) 

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage' 
AWS_ACCESS_KEY_ID = 'mybucket_key_id' 
AWS_SECRET_ACCESS_KEY = 'mybucket_access_key' 
AWS_STORAGE_BUCKET_NAME = 'mybucket' 
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage' 

में इन सेटिंग्स को रख दिया और पहली बार मैं स्थिर सब कुछ इकट्ठा भाग गया सही ढंग से काम किया है और मेरी स्टैटिक फ़ाइलें मेरी S3 बाल्टी पर अपलोड की गई।

लेकिन, मेरा स्टैटिक फ़ाइलें में परिवर्तन करने और python manage.py collectstatic इस चल तथ्य यह है कि स्थिर फ़ाइलों

-----> Collecting static files 
    0 static files copied, 81 unmodified. 

संशोधित किया गया हालांकि, अगर मैं बदल स्थिर फ़ाइल का नाम बदलने के बावजूद outputted है के बाद, बदल स्थिर फ़ाइल सही ढंग से है मेरी एस 3 बाल्टी में कॉपी किया गया।

django-storages मेरी बदली गई स्थिर फ़ाइलों को अपलोड क्यों नहीं कर रहा है? क्या कोई कॉन्फ़िगरेशन समस्या है या समस्या गहरी है?

उत्तर

12

सामूहिक रूप से फाइलों को छोड़ देता है यदि "लक्ष्य" फ़ाइल स्रोत फ़ाइल से "छोटी" है। ऐसा लगता है कि अमेज़ॅन एस 3 स्टोरेज आपके लिए फाइल की गलत तिथि देता है।

आप [कोड] [1] की जांच कर सकते हैं और सर्वर प्रतिक्रियाओं को डीबग कर सकते हैं। शायद टाइमज़ोन के साथ एक समस्या है।

या फिर आप

+0

धन्यवाद इस काम किया।मैं यह देखने के लिए इंतजार कर रहा हूं कि कोई मेरी समस्या का सही समाधान पोस्ट करता है लेकिन यदि नहीं, तो आपके पास +50 :) – bab

+0

हो सकता है - स्क्वायर मेरे लिए s3 के साथ काम नहीं कर रहा है। यदि मैं मैन्युअल रूप से s3 से फ़ाइलों को हटा देता हूं, तो वे सभी पुनः प्रतिलिपि बनाई गई हैं, हालांकि। – mgojohn

5

https://github.com/antonagestam/collectfast

एकत्रित readme.txt से पहले ऐसा collectstatic करने के लिए कि सभी फाइलों को S3 पर नष्ट हो जाती हैं --clear तर्क दे सकते हैं: कस्टम प्रबंधन आदेश से MD5 योग और ETag तुलना एस 3 और यदि दोनों एक ही स्किप फ़ाइल प्रतिलिपि हैं। यदि आप स्रोत नियंत्रण प्रणाली के रूप में गिट का उपयोग कर रहे हैं जो टाइमस्टैम्प अपडेट करता है तो यह स्थिर तेजी से एकत्रित करता है।

0

यह प्रश्न थोड़ा पुराना है, लेकिन अगर यह भविष्य में किसी की मदद करता है, तो मुझे लगा कि मैं अपना अनुभव साझा करूंगा। अन्य धागे में मिली सलाह के बाद मैंने पुष्टि की कि, मेरे लिए, यह वास्तव में समय क्षेत्र में एक अंतर के कारण हुआ था। मेरा django समय गलत नहीं था लेकिन ईएसटी और एस 3 पर सेट किया गया था जीएमटी पर सेट किया गया था। परीक्षण में, मैं django-storages 1.1.5 पर वापस लौटा जो कि सामूहिक काम करने लगते थे। आंशिक रूप से व्यक्तिगत वरीयता के कारण, मैं ए के लिए अनिच्छुक था) django-storages के तीन संस्करणों को वापस रोल करें और किसी भी संभावित बग फिक्स को खो दें या बी) मेरे प्रोजेक्ट के घटकों के लिए समय क्षेत्र को बदलने के लिए जो अनिवार्य रूप से एक सुविधा समारोह में उबाल जाता है (हालांकि एक महत्वपूर्ण एक)।

मैंने उपर्युक्त परिवर्तनों के बिना सामूहिक रूप से समान कार्य करने के लिए एक छोटी सी लिपि लिखी। इसे आपके ऐप के लिए थोड़ा संशोधित करने की आवश्यकता होगी, लेकिन इसे मानक मामलों के लिए काम करना चाहिए यदि इसे ऐप स्तर पर रखा गया है और 'static_dirs' को आपके प्रोजेक्ट के ऐप्स के नाम से बदल दिया गया है। यह 'पायथन जो भी_you_call_it.py -e environment_name के साथ टर्मिनल के माध्यम से चलाया जाता है (इसे अपने aws बाल्टी पर सेट करें)। इस लाइन के साथ एक विशिष्ट सेटिंग्स के साथ

TIME_ZONE = 'UTC' 

भागो collectstatic:

import sys, os, subprocess 
import boto3 
import botocore 
from boto3.session import Session 
import argparse 
import os.path, time 
from datetime import datetime, timedelta 
import pytz 

utc = pytz.UTC 
DEV_BUCKET_NAME = 'dev-homfield-media-root' 
PROD_BUCKET_NAME = 'homfield-media-root' 
static_dirs = ['accounts', 'messaging', 'payments', 'search', 'sitewide'] 

def main(): 
    try: 
     parser = argparse.ArgumentParser(description='Homfield Collectstatic. Our version of collectstatic to fix django-storages bug.\n') 
     parser.add_argument('-e', '--environment', type=str, required=True, help='Name of environment (dev/prod)') 
     args = parser.parse_args() 
     vargs = vars(args) 
     if vargs['environment'] == 'dev': 
      selected_bucket = DEV_BUCKET_NAME 
      print "\nAre you sure? You're about to push to the DEV bucket. (Y/n)" 
     elif vargs['environment'] == 'prod': 
      selected_bucket = PROD_BUCKET_NAME 
      print "Are you sure? You're about to push to the PROD bucket. (Y/n)" 
     else: 
      raise ValueError 

     acceptable = ['Y', 'y', 'N', 'n'] 
     confirmation = raw_input().strip() 
     while confirmation not in acceptable: 
      print "That's an invalid response. (Y/n)" 
      confirmation = raw_input().strip() 

     if confirmation == 'Y' or confirmation == 'y': 
      run(selected_bucket) 
     else: 
      print "Collectstatic aborted." 
    except Exception as e: 
     print type(e) 
     print "An error occured. S3 staticfiles may not have been updated." 


def run(bucket_name): 

    #open session with S3 
    session = Session(aws_access_key_id='{aws_access_key_id}', 
     aws_secret_access_key='{aws_secret_access_key}', 
     region_name='us-east-1') 
    s3 = session.resource('s3') 
    bucket = s3.Bucket(bucket_name) 

    # loop through static directories 
    for directory in static_dirs: 
     rootDir = './' + directory + "/static" 
     print('Checking directory: %s' % rootDir) 

     #loop through subdirectories 
     for dirName, subdirList, fileList in os.walk(rootDir): 
      #loop through all files in subdirectory 
      for fname in fileList: 
       try: 
        if fname == '.DS_Store': 
         continue 

        # find and qualify file last modified time 
        full_path = dirName + "/" + fname 
        last_mod_string = time.ctime(os.path.getmtime(full_path)) 
        file_last_mod = datetime.strptime(last_mod_string, "%a %b %d %H:%M:%S %Y") + timedelta(hours=5) 
        file_last_mod = utc.localize(file_last_mod) 

        # truncate path for S3 loop and find object, delete and update if it has been updates 
        s3_path = full_path[full_path.find('static'):] 
        found = False 
        for key in bucket.objects.all(): 
         if key.key == s3_path: 
          found = True 
          last_mode_date = key.last_modified 
          if last_mode_date < file_last_mod: 
           key.delete() 
           s3.Object(bucket_name, s3_path).put(Body=open(full_path, 'r'), ContentType=get_mime_type(full_path)) 
           print "\tUpdated : " + full_path 
        if not found: 
         # if file not found in S3 it is new, send it up 
         print "\tFound a new file. Uploading : " + full_path 
         s3.Object(bucket_name, s3_path).put(Body=open(full_path, 'r'), ContentType=get_mime_type(full_path)) 
       except: 
        print "ALERT: Big time problems with: " + full_path + ". I'm bowin' out dawg, this shitz on u." 


def get_mime_type(full_path): 
    try: 
     last_index = full_path.rfind('.') 
     if last_index < 0: 
      return 'application/octet-stream' 
     extension = full_path[last_index:] 
     return { 
      '.js' : 'application/javascript', 
      '.css' : 'text/css', 
      '.txt' : 'text/plain', 
      '.png' : 'image/png', 
      '.jpg' : 'image/jpeg', 
      '.jpeg' : 'image/jpeg', 
      '.eot' : 'application/vnd.ms-fontobject', 
      '.svg' : 'image/svg+xml', 
      '.ttf' : 'application/octet-stream', 
      '.woff' : 'application/x-font-woff', 
      '.woff2' : 'application/octet-stream' 
     }[extension] 
    except: 
     'ALERT: Couldn\'t match mime type for '+ full_path + '. Sending to S3 as application/octet-stream.' 

if __name__ == '__main__': 
    main() 
2

एक सेटिंग सिर्फ collectstatic सिंक के लिए फ़ाइल, इस config के साथ बनाएं

python manage.py collectstatic --settings=settings.collectstatic 
+1

इसने मेरी समस्या हल की। – kmomo

संबंधित मुद्दे