简体   繁体   中英

Calling the S3BotoStorage Save() method from a subclass

I am trying to implement a new Storage class that subclasses the S3BotoStorage from Django-Storages. (http://code.larlet.fr/django-storages/src/83fa2f0ba20c/storages/backends/s3boto.py)

Whenever the save method of my new class is being called, I want to do some other stuff and then call the S3BotoStorage._save() method. Like this:

class CustomStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        super(CustomStorage, self).__init__(*args, **kwargs)

    def _save(self,*args, **kwargs):
        #Will do stuff there
        print >> sys.stderr, "%s" % (self.bucket)
        super(CustomStorage, self)._save(*args, **kwargs)

If I don't have this CustomStorage._save() method, everything works well (ie the S3BotoStorage._save(name,content) is called and everything uploads to S3). If I have this new CustomStorage.save method however, I get a 500 error. (It does get called though, as my error message appears in the terminal). I can't see any call stack or anything.

I tried:

def save(self,*args, **kwargs):
def save(self,name, content):

Neither of these also worked.

Any ideas?!

Thanks!

Storage._save should return the name of the file being saved. Your _save does not. You should return the value from the super call.

class CustomStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        super(CustomStorage, self).__init__(*args, **kwargs)

    def _save(self,*args, **kwargs):
        #Will do stuff there
        print >> sys.stderr, "%s" % (self.bucket)
        return super(CustomStorage, self)._save(*args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM