Django uploads images, uses PIL to create thumbnails, and saves them to the storage of sea,
Upload Image Parsing:
The SAES settings are as follows:
Process uploaded files
Add the following configuration in setttings. py.
# Modify the maximum size that files can be stored in the memory during upload to 10mFILE_UPLOAD_MAX_MEMORY_SIZE = 10485760 # the local file system of sae is read-only, and change django's file storage backend to StorageDEFAULT_FILE_STORAGE = 'sae. ext. django. storage. backend. store' # Use media this bucketSTORAGE_BUCKET_NAME = 'Media '# ref: https://docs.djangoproject.com/en/dev/topics/files/
There are two situations:
- An image is a field in the database, for example:
class Picture(models.Model): """docstring for Picture""" user = models.ForeignKey(Customer) image = ThumbnailImageField(upload_to='goods_photo')
Then you just need to write it in the view function, at this time, SAE can automatically help you upload images to the goods_photo folder under the media domain in storage (remember to activate storage on SAE and create a file named you in settings. the STORAGE_BUCKET_NAME set by py, and create a folder named the value of upload_to under this domain)
def upload_photo(request): f = request.FILES['upload_file'] name=copy.copy(f.name) p = Picture() p.image = f user_id = request.POST['user_id'] user = Customer.objects.get(pk=user_id) p.user = user p.save()
- Directly upload images to storage
Refer to the storage api documentation of sea.
Now, I want to upload an image and generate a thumbnail. The image is a field in the database, the thumbnail accesses the thumbnail through a custom attribute thumb_url of the database to customize a ThumbnailImageField. The reference code is as follows:
# ThumbnailImageField.pyfrom django.db.models.fields.files import ImageField,ImageFieldFilefrom PIL import Imagedef _add_thumb(s): parts = s.split(".") parts.insert(-1,"thumb") if parts[-1].lower() not in ['jpeg','jpg']: parts[-1]='jpg' return ".".join(parts)class ThumbnailImageFieldFile(ImageFieldFile): def _get_thumb_url(self): return _add_thumb(self.url) thumb_url = property(_get_thumb_url) def save(self,name,content,save=True): super(ThumbnailImageFieldFile, self).save(name,content,save) def delete(self,save=True): super(ThumbnailImageFieldFile, self).delete(save) class ThumbnailImageField(ImageField): """docstring for ThumbnailImageField""" attr_class = ThumbnailImageFieldFile def __init__(self,*args,**kwargs): super(ThumbnailImageField, self).__init__(*args,**kwargs)
View functions are as follows:
def upload_photo(request): f = request.FILES['upload_file'] name=copy.copy(f.name) p = Picture() p.image = f user_id = request.POST['user_id'] user = Customer.objects.get(pk=user_id) p.user = user p.save() c = sae.storage.Connection() bucket = c.get_bucket('media') obj = bucket.get_object_contents("goods_photo/"+name) image = Image.open(StringIO(obj)) image.thumbnail((90,90),Image.ANTIALIAS) imgOut = StringIO() image.save(imgOut, 'jpeg') img_data = imgOut.getvalue() bucket.put_object('goods_photo/'+_add_thumb(name), img_data) imgOut.close()
View function details:
P. save () will consume the request. FILES ['upload _ file']. Therefore, we recommend that you first save the file to storage and then extract the image from storage for processing.
Name = copy. copy (f. name), the reason for using the shortest copy is that p. save () will consume the request. FILES ['upload _ file'], name = f. you do not know what the name is. You can also use deepcopy, but it is not necessary because name is a string.
Bucket. get_object_contents ("goods_photo/" + name), storage of sea does not support direct directory structure, but directory structure can be simulated by file name