图片上传:iPhone客户端 - Django的 - S3 [英] Image upload: iPhone Client - Django - S3

查看:237
本文介绍了图片上传:iPhone客户端 - Django的 - S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于从客户端(在这种情况下,一个iPhone应用程序),以S3上传一个一般性的问题。我使用的Django写我的web服务在一个EC2实例。下面的方法是最低限度上传文件到S3,它工作得很好,更小的文件(JPG格式或PNG图片< 1 MB):

I have a general question regarding uploads from a client (in this case an iPhone App) to S3. I'm using Django to write my webservice on an EC2 instance. The following method is the bare minimum to upload a file to S3 and it works very well with smaller files (jpgs or pngs < 1 MB):

def store_in_s3(filename, content):
    conn = S3Connection(settings.ACCESS_KEY, settings.PASS_KEY) # gets access key and pass key from settings.py
    bucket = conn.create_bucket('somebucket')
    k = Key(bucket) # create key on this bucket
    k.key = filename
    mime = mimetypes.guess_type(filename)[0]
    k.set_metadata('Content-Type', mime)
    k.set_contents_from_string(content)
    k.set_acl('public-read')

def uploadimage(request, name):
    if request.method == 'PUT':
        store_in_s3(name,request.raw_post_data)
        return HttpResponse("Uploading raw data to S3 ...")
    else:
        return HttpResponse("Upload not successful!")

我是很新的这一切,所以我还是不明白这里发生的事情。难道的情况是:

I'm quite new to all of this, so I still don't understand what happens here. Is it the case that:

  • Django的接收文件并将其保存在我的EC2实例的内存?
  • 我应该避免使用raw_post_data和宁做块,以避免内存问题?
  • 一旦Django的已收到该文件,将其上的文件传递给函数store_in_s3?
  • 我怎么知道,如果上传到S3是成功的?

因此​​,在一般情况下,我不知道如果一行我的code将陆续执行。例如。当将返回的Htt presponse(上传原始数据S3 ...)火?虽然文件还在上传或之后被成功上传?

So in general, I wonder if one line of my code will be executed after another. E.g. when will return HttpResponse("Uploading raw data to S3 ...") fire? While the file is still uploading or after it was successfully uploaded?

感谢您的帮助。另外,我很感谢他们对待这个问题的任何文档。我看了一下在O'Reilly的Python中与放大器的章节; AWS食谱,但因为它是唯一的code样品,它并没有真正回答我的问题。

Thanks for your help. Also, I'd be grateful for any docs that treat this subject. I had a look at the chapters in O'Reilly's Python & AWS Cookbook, but as it's only code samples, it doesn't really answer my questions.

推荐答案

在内存中存储的Django小上传的文件。过了一定的规模,而且将其存储在磁盘上的临时文件。

Django stores small uploaded files in memory. Over a certain size, and it will store it on a temp file on disk.

是的,分块对内存的节省也非常有帮助:

Yes, chunking is helpful for memory savings as well:

for file_chunk in uploaded_file.chunks():
    saved_file.write(file_chunk)

所有的这些操作是同步的,将让Django的等待,直到该文件是完全上传之前,它会尝试将其存储在S3。 S3将完成其上传前将返回为好,这样可以保证,这将通过Django和到S3上载之前,您将收到您的Htt presponse()

All of these operations are synchronous, so Django will wait until the file is fully uploaded before it will attempt to store it in S3. S3 will have to complete its upload before it will return as well, so you are guaranteed that it will be uploaded through Django and to S3 before you will receive your HttpResponse()

Django的文件上传文档中有很多伟大的关于如何处理各种上传的情况信息。

Django's File Uploads documentation has a lot of great info on how to handle various upload situations.

这篇关于图片上传:iPhone客户端 - Django的 - S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆