正在旋转开发中的自拍照图像,但是在生产中保存到S3存储桶时却不起作用?

问题描述 投票:1回答:2

我正在尝试解决正在生产但未投入开发的图像旋转错误。在此问题上停留了几天,非常感谢任何对如何解决此问题有任何见识的人的帮助!

==上下文==

我正在编写具有个人资料照片上传功能的Django网络应用。为了克服自拍EXIF问题(方向错误),我添加了一个使用后保存接收器旋转图像的功能。它在开发中效果很好(当将图像保存和存储在本地时),但是现在我已经进入生产环境(Heroku服务器;将图像保存在S3存储桶中),该函数将引发FileNotFound错误-[Errno 2]文件或目录:“ https://db-devsite1.s3.amazonaws.com/media/uploadImage/81c01af5-030f-42ed-b413-91eb8941675b.JPG”,即使它是图像的正确文件路径。其他一切仍然很好。

== View ==

def rotateimage (request):

    if request.method == 'POST':
        form = uploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('rotateimage')
    else:
        form = uploadImageForm()

==型号==

import os
from django.db import models
from catalog.utilities import rotate_image
from django.urls import reverse
from io import BytesIO
from django.core.files import File
from PIL import Image, ExifTags
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings

class uploadImage(models.Model):
    uploadImage = models.ImageField(upload_to='uploadImage', blank=True, null=True)
    thumbnail = models.ImageField(upload_to='rotateImage', blank=True, null=True)

@receiver(post_save, sender=uploadImage, dispatch_uid="update_image_profile")
def update_image(sender, instance, **kwargs):
  if instance.uploadImage:
    fullpath = settings.MEDIA_ROOT + instance.uploadImage.url
    rotate_image(fullpath)

==实用程序==

from PIL import Image, ExifTags

def rotate_image(filepath):

    dev_test = "Off"

    if dev_test == "Off":
        try:
            image = Image.open(filepath)
            for orientation in ExifTags.TAGS.keys():
              if ExifTags.TAGS[orientation] == 'Orientation':
                    break
            exif = dict(image._getexif().items())

            if exif[orientation] == 3:
                image = image.rotate(180, expand=True)
            elif exif[orientation] == 6:
                image = image.rotate(270, expand=True)
            elif exif[orientation] == 8:
                image = image.rotate(90, expand=True)
            image.save(filepath)
            image.close()
        except (AttributeError, KeyError, IndexError):
            # cases: image don't have getexif
            pass
    else:
        try:
            image = Image.open(filepath)
            image = image.rotate(180, expand=True)
            image.save(filepath)
            image.close()
        except (AttributeError, KeyError, IndexError):
            # cases: image don't have getexif
            pass

==设置==

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

AWS_LOCATION = 'static'
AWS_ACCESS_KEY_ID = CONFIG['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = CONFIG['AWS_SECRET_ACCESS_KEY']
AWS_STORAGE_BUCKET_NAME = CONFIG['AWS_STORAGE_BUCKET_NAME']
AWS_S3_CUSTOM_DOMAIN='%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME

AWS_S3_OBJECT_PARAMETERS = {
     'CacheControl': 'max-age=86400',
}
DEFAULT_FILE_STORAGE = 'dbDevSite.storage_backends.MediaStorage'
STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'catalog/static'),
]
STATIC_URL='https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'
STATICFILES_FINDERS = ('django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder',)
AWS_DEFAULT_ACL = None
AWS_PRELOAD_METADATA=True

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'catalog/static/media')

# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

***编辑-更新为完整错误

Environment:


Request Method: POST
Request URL: https://db-devsite.herokuapp.com/catalog/rotateimage

Django Version: 3.0.1
Python Version: 3.8.0
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'catalog.apps.CatalogConfig',
 'storages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/app/catalog/views.py", line 18, in rotateimage
    form.save()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/forms/models.py", line 459, in save
    self.instance.save()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/models/base.py", line 745, in save
    self.save_base(using=using, force_insert=force_insert,
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/models/base.py", line 793, in save_base
    post_save.send(
  File "/app/.heroku/python/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 173, in send
    return [
  File "/app/.heroku/python/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 174, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/app/catalog/models.py", line 31, in update_image
    rotate_image(fullpath)
  File "/app/catalog/utilities.py", line 9, in rotate_image
    image = Image.open(filepath)
  File "/app/.heroku/python/lib/python3.8/site-packages/PIL/Image.py", line 2766, in open
    fp = builtins.open(filename, "rb")

Exception Type: FileNotFoundError at /catalog/rotateimage
Exception Value: [Errno 2] No such file or directory: 'https://db-devsite1.s3.amazonaws.com/media/uploadImage/81c01af5-030f-42ed-b413-91eb8941675b_Kupxl37.JPG'

这里的编辑是有效的后保存功能的代码解决方案

@receiver(post_save, sender=uploadImage, dispatch_uid="update_image_profile")
def update_image(sender, instance, **kwargs):
  if instance.uploadImage:

      # Download instance.uploadImage from S3 to temp folder
      s3_client = boto3.client('s3')
      bucket_name = settings.AWS_STORAGE_BUCKET_NAME
      subfolder_name = 'media/'
      target_image = str(instance.uploadImage)
      image_path = subfolder_name + target_image
      image_name = '/tmp/image.jpg'

      s3_client.download_file(bucket_name, image_path, image_name)

      # Rotate image in temp folder
      rotate_image(image_name)

      # Upload rotated image from temp folder back to S3
      s3_client.upload_file(image_name, bucket_name, image_path)
python django amazon-s3 image-uploading production-environment
2个回答
1
投票

如果您的代码成功处理了旋转,则唯一缺少的步骤是下载/上传图像。

要下载:

import boto3

s3_client = boto3.client('s3')

s3_client.download_file('mybucket', 'user1.jpg', '/tmp/image.jpg')

然后,使用现有代码旋转图像,最后将其上传到所需的目标位置:

s3_client.upload_file('/tmp/image.jpg', 'mybucket', 'user1.jpg')

0
投票

fullpath = settings.MEDIA_ROOT + instance.uploadImage.url

其中MEDIA_ROOT = os.path.join(BASE_DIR,'目录/静态/媒体')和instance.uploadImage.url不清楚,但我想它是一个URL

因此,它正在本地磁盘上查找包含url的路径-我可以想象该路径不存在,因此错误2“没有这样的文件或目录”

© www.soinside.com 2019 - 2024. All rights reserved.