简体   繁体   English

带有AWS EC2 Endpoint或S3的iOS

[英]iOS with AWS EC2 Endpoint or S3

I have an iOS app backed by AWS (EC2 and other services). 我有一个受AWS支持的iOS应用程序(EC2和其他服务)。 But I have a question regarding uploading user generated files such as images and videos to the backend. 但是我有一个关于将用户生成的文件(例如图像和视频)上传到后端的问题。 I'm thinking either to upload them directly to S3 buckets or upload to EC2 and let EC2 pass them to S3 buckets. 我在考虑将它们直接上传到S3存储桶或上传到EC2,然后让EC2将它们传递到S3存储桶。 I'm leaning towards uploading to EC2 because I don't want anyone to have write access to my file system, however, this will probably require lots of heavy lifting for my EC2 instance. 我倾向于上传到EC2,因为我不希望任何人对我的文件系统具有写访问权,但是,这可能需要为我的EC2实例进行很多繁重的工作。 If I upload directly to S3, how can I ensure the security without using temporary urls (I'm using SDWebImage to download and cache the images, temp urls will cause SDWebImage to download the same images instead of retrieving them from local)? 如果直接上传到S3,如何在不使用临时URL的情况下确保安全性(我正在使用SDWebImage下载和缓存图像,临时URL将导致SDWebImage下载相同的图像,而不是从本地检索它们)? Any suggestions will be highly appreciated. 任何建议将不胜感激。

So you're going to want to decouple this via Lambda. 因此,您将要通过Lambda对此进行解耦。

Don't use EC2. 不要使用EC2。 Rather unnecessary. 完全没有必要。

Create an S3 bucket called "app-whatever" 创建一个名为“ app-whatever”的S3存储桶

Keep your bucket PRIVATE. 将您的存储桶保持私有状态。 You don't have to give users access to things. 您不必授予用户访问权限。 They can be private with generated URLs. 它们可以是生成的URL私有的。 Let every user have their own folder (this is the prefix). 让每个用户都有自己的文件夹(这是前缀)。

To be fancy, put every item in S3 from the users end. 为方便起见,请从用户端将每个项目放入S3。 Have each key randomly generated with ios 'arc4' formula or whatever you want, then append the file type. 使用ios'arc4'公式或您想要的任意值随机生成每个密钥,然后附加文件类型。 So blahblab.png 所以blahblab.png

Put a metadata field WITH the file called thumbnail and the value of the filename plus -thumb . 在元数据字段中放入名为thumbnail的文件和文件名plus -thumb的值。 So blahblah-thumb.png 所以blahblah-thumb.png

Recap: you are uploading a single file to S3 with the metadata field of thumbnail . 回顾:您正在将一个带有thumbnail的元数据字段的文件上传到S3。 That's it. 而已。

Then create a Lambda function that responds to every single PUT file from S3. 然后创建一个Lambda函数,以响应S3中的每个PUT文件。 If you like Node.JS, use the imagemagick module (nicely included for you) to generate a 128x128 thumbnail or whatever. 如果您喜欢Node.JS,请使用imagemagick模块(为您提供了一个不错的选择)来生成128x128缩略图或其他。 The lambda function receives the S3 data for the file. lambda函数接收文件的S3数据。 Get the main file (key name) for the source file, and the thumbnail metadata will be the output. 获取源文件的主文件(键名),缩略图元数据将作为输出。 Run your IM function and put the output file in the S3 bucket. 运行您的IM函数,然后将输出文件放入S3存储桶中。 Now you have two files in the bucket. 现在,存储桶中有两个文件。 Great. 大。

Now when you list these files, DONT DOWNLOAD EACH ONE. 现在,当您列出这些文件时,不要下载每个文件。 Get the metadata from the file and get a presignedUrl for the key in the metadata. 从文件中获取元数据,并为元数据中的密钥获取presignedUrl

This will be fast. 这样会很快。 For an example, check out HD Camera by iSkore on the App Store. 例如,在App Store上查看iSkore的HD Camera。

It really depends on what your reasons for restriction are. 这实际上取决于您限制的原因。 If you give the app permissions to upload the image to your EC2 server and then it just forwards it to S3, what are you gaining? 如果您授予应用程序将映像上传到EC2服务器的权限,然后它将映像转发到S3,您将获得什么? S3 allows you to use key pre-fixes to restrict objects based on the user (which is easy if you are using Cognito for authentication). S3允许您使用关键前缀来基于用户限制对象(如果使用Cognito进行身份验证,这很容易)。 For instance you can make it so your S3 structure is something like /images/{userId}/myImage.jpg and each user can only upload to their folder. 例如,您可以使它的S3结构类似于/images/{userId}/myImage.jpg,每个用户只能上载到其文件夹。 You can make it totally private, or you can let other users read from that folder but not write ect... 您可以将其设置为完全私有,也可以让其他用户从该文件夹读取但不写ect ...

Some thoughts: -You could black list users potentially -You could throttle requests, or inspect and reject certain images -You could have a more complicated security model than S3 provides 一些想法:-您可能会将用户列入黑名单-您可以限制请求,或者检查并拒绝某些图像-您可能拥有比S3提供的安全模型更复杂的安全模型

You could use API Gateway for the first 2 use cases. 您可以在前两个用例中使用API​​ Gateway。

If you do want to use ec2, yes the pre-signed URL will change. 如果确实要使用ec2,则可以更改预签名URL。 I'm not sure how SDWebImage works so I can't comment on it's caching ability. 我不确定SDWebImage的工作方式,因此无法评论它的缓存功能。

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

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