0

I have a django web service. I want to be able to accept images from an iOS app, save the image to the database(the image file itself should be belt in my s3 bucket).

It's super easy saving it through the admin, you just define the upload_to and set the bucket as the static_url, but I can not find any examples/documentaion on how to save an image sent from an app for example.

Can anyone point me in the right direction or give an example?

A bit more information as my question is vague:

class Image(models.Model):
    name = models.CharField(max_length = 255)
    caption = models.CharField(max_length = 255)
    image = models.ImageField(upload_to='uploads/',blank=True,null=True)
    rent_property = models.ForeignKey(RentProperty, related_name='Images')
    is_main_image = models.BooleanField(default=False)

setting.py

#Amazon Bucket
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = '################'
AWS_SECRET_ACCESS_KEY = '#####################'
AWS_STORAGE_BUCKET_NAME = 'string'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

this is my image class, what Im trying to do, is to get an image from the app, save it to to the database and the S3 storage, and link it to the correct foreign key.

My trouble is in understand how I make the file save to the S3 while saving the information to the database.

Yoav Schwartz
  • 2,017
  • 2
  • 23
  • 43

1 Answers1

0

I've answered a very similar question here : Best way to upload image from Mobile to Django server so yours might be seen as a possible dupplicate.

Would the image be linked to a database object (Image Model perhaps) ?

If so, adding to the code linked above :

@csrf_exempt
def handle_uploads(request):
    if request.method == 'POST':
        uploaded_file = request.FILES['file']
        file_name = uploaded_file.name
        # Write content of the file chunk by chunk in a local file (destination)
        with open('path/to/destination_dir/' + file_name, 'wb+') as destination:
            for chunk in uploaded_file.chunks():
                destination.write(chunk)
        # Create your object
        obj = Image.objects.create(file_field='path/to/destination_dir/' + file_name)
        obj.save()

    response = HttpResponse('OK')
    return response

Hope this helps,

Edit :


I may have misunderstood you, why are you asking how to save an image if you're planning on using an external storage ? Do you mean you want to save only the filename ? Or do you want Django to use seamlessly the S3 storage ? I believe it is well documented https://docs.djangoproject.com/en/1.6/howto/custom-file-storage/ but the code above would need some rework.

Found with very little effort : http://www.laurentluce.com/posts/upload-and-download-files-tofrom-amazon-s3-using-pythondjango/

It seems to be perfect for what you're trying to achieve. Just add the upload part to the code above and you should be right on tracks :)

Community
  • 1
  • 1
Ambroise
  • 1,649
  • 1
  • 13
  • 16
  • exactly, I may have not explained my self. my main problem, is how do I store the image "data" into the database and the image itself to the S3, just like when I save with the admin – Yoav Schwartz May 02 '14 at 14:11