6

I know we now can upload/post a media type field to the python-eve REST-API framework and it will be stored in mongodb. Is there a way we can change the storage of those media? E.g. a remote storage server e.g. amazon S3? So that we only store the URL of the image in mongodb instead of the whole image?

elixenide
  • 44,308
  • 16
  • 74
  • 100
John
  • 2,107
  • 3
  • 22
  • 39

1 Answers1

2

While the standard behaviour is to store on GridFS, you can also provide your own MediaStorage subclass which can store wherever you want to (file system, S3, etc.)

Something like this would do the trick:

from eve.io.media import MediaStorage

class AmazonS3MediaStorage(MediaStorage):
    """ Your actual implementation """
    pass

app = Eve(media=AmazonS3MediaStorage)

if __name__ == '__main__':
    app.run()

Check out the actual MediaStorage class for implementation details and/or see the actual GridFSMediaStorage class for reference.

Rotareti
  • 49,483
  • 23
  • 112
  • 108
Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33
  • 1
    @JohnZ if you come up with a S3 `MediaStorage` class and are interested in sharing it please get in touch with me, we could add it to the Eve repo or, at the very least, make an extension out of it. – Nicola Iarocci Feb 19 '14 at 07:10
  • I am trying to implement an FS Media storage (just for studying) and two things are hapenning: 1- The get is being called after the put always , 2- i am receiving "resourceWarning: unclosed file" on creation. should i return a stream or something like that? – FabioCosta Jan 28 '16 at 19:23
  • 2
    I've managed to make a MediaStorage using S3. Here's my code: https://github.com/gwainer/eve/commit/eaf1c0adc2ef6c7d66e429bc70104f5808b841d8 – gcw Jan 12 '18 at 01:31
  • @gcw nice, how about you refactor it into a stand-alone package? It could then join the official [extensions list](http://python-eve.org/extensions). – Nicola Iarocci Jan 12 '18 at 07:57
  • sure, let's do this. I will check how to package it correctly. – gcw Jan 12 '18 at 10:07
  • @gcw Has the s3 mediastorage implementation in the github link you have shared been integrated with eve?? if yes How do I use it with existing eve I have? Should I just update eve? I'm asking without having to copy your code to my project,etc. Is there a way to just import that implementation of your? – Eswar Jan 29 '19 at 07:35
  • @gcw The github code you shared has not worked for me. It kept throwing "fileobj must implement read". – Eswar Jan 29 '19 at 13:05
  • @Eswar what python version are you using? – gcw Feb 18 '19 at 13:34
  • @gcw I got it this uploading to s3 using boto3. I'm using python 3.6.5 version. thank you. – Eswar Feb 18 '19 at 13:43