简体   繁体   English

使用 s3boto3storage 和 django 读取和写入不同的 S3 存储桶

[英]Reading and Writing to different S3 buckets with s3boto3storage and django

I've been working off a series of tutorials and have my django photo gallery uploading images to an AWS S3 bucket named "sitename-org" using S3Boto3Storage.我一直在学习一系列教程,并让我的 django 照片库使用 S3Boto3Storage 将图像上传到名为“sitename-org”的 AWS S3 存储桶。 I have an AWS Lambda that creates a thumbnail from it and puts it in a different bucket named "sitename-org-resized".我有一个 AWS Lambda,它从中创建一个缩略图并将其放入名为“sitename-org-resized”的不同存储桶中。 I cannot figure out how to make a thumbnail member in Photo class that pulls the image (same filename) from the -resized bucket.我无法弄清楚如何在 Photo 类中创建一个缩略图成员,该成员从 -resized 存储桶中提取图像(相同的文件名)。 This was my latest attempt.这是我最近的尝试。

class Photo(models.Model):
   exclude = ('thumbnail',)

   title = models.CharField(max_length=100)
   image = models.ImageField(null=False, blank=False, upload_to='gallery_photos')
   date_uploaded = models.DateTimeField(default=timezone.now)
   thumbnail = models.ImageField(storage=S3Boto3Storage(bucket_name='sitename-org-resized'), null=True, blank=True)
   uploader = models.ForeignKey(User, on_delete=models.CASCADE)
   album = models.ForeignKey(PhotoAlbum, on_delete=models.SET_NULL, null=True, blank=True, related_name='photos')
   description = models.TextField()

   def save(self, *args, **kwargs): 
#    
      super().save(*args, **kwargs)
      thumbnail = self.image

Silly mistake.愚蠢的错误。 I was saving the object before assigning the thumbnail.我在分配缩略图之前保存了对象。 This works fine.这工作正常。

def save(self, *args, **kwargs): #override save method to resize image
   self.thumbnail = self.image
   super().save(*args, **kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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