在AWS上存储用户映像 [英] Storing user images on AWS

查看:111
本文介绍了在AWS上存储用户映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ionic2实现一个简单的应用程序,该应用程序调用使用Flask构建的API.设置配置文件时,我向用户提供了上传自己的图像的选项. 我想到了将它们存储在S3存储桶中并通过CloudFront为它们提供服务.

I'm implementing a simple app using ionic2, which calls an API built using Flask. When setting up the profile, I give the option to the users to upload their own images. I thought of storing them in an S3 bucket and serving them through CloudFront.

经过一番研究,我只能找到有关以下信息:

After some research I can only find information about:

  • 使用python从本地存储中上传图像.
  • 使用javascript从HTML文件选择器上传图像.

当前端与API交互时,我找不到有关如何处理Blob/文件的任何信息.当我开始研究这些选择时,我想到的是:

I can't find anything about how to deal with blobs/files when you have a front end interacting with an API. When I started researching the options I had thought of were:

  1. 将文件发布到客户端的Amazon并返回 CloudFront网址直接到达后端.我不太喜欢这个 一个是因为这将涉及到某种秘密 客户端(也许不是那么危险,但是我宁愿拥有它 在后端).
  2. 将图像上传到服务器,并以某种方式告知后端有关 我们希望后端选择哪个文件.我不太热衷 这种方法要么是因为客户需要了解 关于服务器本身(不仅是API).
  3. 对图像进行编码(我对base64有一定的了解,但缺乏 我认为这是完全错误的示例),然后将其发布到后端, 它将处理所有S3上传/存储的CloudFront URL.
  1. Post the file to Amazon on the client side and return the CloudFront url directly to the back end. I am not too keen on this one because it would involve having some kind of secret on the client side (maybe is not that dangerous, but I would rather have it on the back end).
  2. Upload the image to the server and somehow tell the back end about which file we want the back end to choose. I am not too keen on this approach either because the client would need to have knowledge about the server itself (not only the API).
  3. Encode the image (I have tought of base64, but with the lack of examples I think that it is plain wrong) and post it to back end, which will handle all the S3 upload/store CloudFront URL.

我觉得所有这些方法都是错误的,但是我想不出(或找到)正确的方法.

I feel like all these approaches are plain wrong, but I can't think (or find) what is the right way of doing it.

我应该如何处理?

推荐答案

让服务器生成 预签名URL ,供客户端将图像上传到该URL.这意味着服务器可以控制URL的外观,并且不会暴露任何秘密,但是客户端可以将图像直接上传到S3.

Have the server generate a pre-signed URL for the client to upload the image to. That means the server is in control of what the URLs will look like and it doesn't expose any secrets, yet the client can upload the image directly to S3.

在使用boto3的Python 看起来像这样:

s3 = boto3.client('s3', aws_access_key_id=..., aws_secret_access_key=...)
params = dict(Bucket='my-bucket', Key='myfile.jpg', ContentType='image/jpeg')
url = s3.generate_presigned_url('put_object', Params=params, ExpiresIn=600)

ContentType是可选的,并且客户端在上载到url期间必须设置相同的Content-Type HTTP标头;我发现限制已知的文件类型非常方便.

The ContentType is optional, and the client will have to set the same Content-Type HTTP header during upload to url; I find it handy to limit the allowable file types if known.

这篇关于在AWS上存储用户映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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