简体   繁体   English

使用 S3 存储桶中的对象创建 Django 对象

[英]Creating Django objects with objects in an S3 bucket

I've uploaded a large amount of images to S3, using the awcli utility.我使用 awcli 实用程序将大量图像上传到 S3。 I am trying to write a Django management command to iterate over them and create objects in the database for them.我正在尝试编写一个 Django 管理命令来迭代它们并在数据库中为它们创建对象。

So far, I've been able to get the object - but I'm stuck on what I should do to make the appropriate Django object.到目前为止,我已经能够获得 object - 但我仍然坚持我应该做些什么来制作合适的 Django object。

Obviously this code doesn't work but hopefully sheds some light on what I am trying to do.显然这段代码不起作用,但希望能对我正在尝试做的事情有所了解。

s3 = boto3.resource('s3', region_name='us-east-2')
bucket = s3.Bucket('photo-uploads')
object = bucket.Object('00004542/000045420020.jpg')

photo = Photo.objects.create(title='Some title', image=object)
photo.save()

I did this with a few extra steps, but i also made the assumption that since we have the objects stored in s3 we don't want to also store them as a BLOB in the database.我做了一些额外的步骤,但我也做了一个假设,因为我们将对象存储在 s3 中,我们不想将它们也作为 BLOB 存储在数据库中。 I'm only storing all the references we need to access the object on a given resource:我只存储访问给定资源上的 object 所需的所有引用:

here are your models:这是您的模型:

class Resource(models.Model):
    ...
    region_name = models.CharField(max_length=32, default='us-east-2')
    bucket_name = models.CharField(max_length=32, default='photo-uploads')

class Photo(models.Model):
    ...
    title = models.CharField(max_length=56)
    object_key = models.CharField(max_length=128) # make this however big you need to fit your worst case object key string
    resource = models.ForeignKey(Resource, on_delete=models.DO_NOTHING)

The ellipses are where you would put your primary key soup de jour (UUID, AutoField, etc)省略号是您放置主键的地方(UUID、AutoField 等)

With this we can ensure that we have all the information needed to retrieve the image from the bucket or generate a URL for the object with the object key and the bucket information.有了这个,我们可以确保我们拥有从存储桶中检索图像所需的所有信息,或者使用 object 密钥和存储桶信息为 object 生成 URL。

You dont need to store the physical s3 object in your database since you're already doing that in s3.您不需要将物理 s3 object 存储在数据库中,因为您已经在 s3 中这样做了。

Let me know if this makes sense.让我知道这是否有意义。

Then you would do this然后你会这样做

resource = Resource.objects.create(region_name='us-east-2', bucket_name='photo-uploads')
photo = Photo.objects.create(title="Some title", object_key='00004542/000045420020.jpg', resource=resource)

Which then yields us the ability to download the file from the info stored in the database using the Photo model.这使我们能够使用Photo model 从存储在数据库中的信息中下载文件。

import boto3
from .models import Photo

# get the first one since in my example we only have one, but you would use the Photo id for other usecases
photo = Photo.objects.first()

s3 = boto3.resource('s3', region_name=photo.resource.region_name)
bucket = s3.Bucket(photo.resource.bucket_name)
object = bucket.Object(photo.object_key)


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

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