简体   繁体   English

在AWS s3中保存图像

[英]Save image in AWS s3

I am planning to have my app save images along with some form data in AWS. 我计划让我的应用程序在AWS中保存图像以及一些表单数据。 I already know how to save Angular form data in DynamoDB via API gateway and lambda. 我已经知道如何通过API网关和lambda在DynamoDB中保存Angular表单数据。 But I haven't found a good way to save the image and store the image S3 location in DynamoDB. 但是我还没有找到一种保存图像并将图像S3位置存储在DynamoDB中的好方法。 Could someone let me know what would be best practice? 有人可以让我知道什么是最佳做法吗? If you have any reference, would be greatly appreciated. 如果您有任何参考,将不胜感激。

I am thinking possibly saving the image to S3 directly from Angular then have a lambda listen an the event and save the image S3 location in DynamoDB. 我在想可能直接从Angular将图像保存到S3,然后让Lambda监听事件并将图像S3位置保存在DynamoDB中。 But haven't found a good way to save to S3 directly from Angular. 但是还没有找到直接从Angular保存到S3的好方法。

Another way could be to have the lambda storing data in DynamoDB to save the image then use the S3 location to make the DB save. 另一种方法是让lambda在DynamoDB中存储数据以保存映像,然后使用S3位置保存数据库。 But haven't find an example of this. 但是还没有找到一个例子。

Maybe there is a better way. 也许有更好的方法。 Please advise. 请指教。 Thank you! 谢谢!

First of all, its important to have a direct upload mechanism to upload Images to S3 without going through the API Gateway. 首先,必须具有直接上传机制以将图像上传到S3而不通过API网关,这一点很重要。 Since we also don't want to keep the S3 bucket public, you can use one of the following options to upload images while having access control in place. 由于我们也不想公开S3存储桶,因此可以在使用访问控制时使用以下选项之一上传图像。

  • Generate SignedUrls from an API endpoint, either with S3 Signed URLs or CloudFront Signed URLs (If you use CloudFront). 使用S3签名URLCloudFront签名URL (如果使用CloudFront)从API端点生成SignedUrl。
  • Use IAM temperory access credentials to upload files to S3, either by having an API endpoint to generate those or using Cognito Federated Identities (If you use Cognito Userpools as the IDP it will be more straightforward). 通过使用API​​端点来生成文件或使用Cognito联合身份 ,使用IAM临时访问凭据将文件上传到S3(如果您将Cognito用户池用作IDP,则更加简单)。

This way you will be able to directly upload to S3 from your Angular App. 这样,您将可以从Angular App直接上传到S3。 Then let us look at how you can save the S3 file path in DynamoDB. 然后让我们看一下如何在DynamoDB中保存S3文件路径。

  • Synchronous: After S3 upload callback, save the URL (Can generate the URL using bucket name + path + file name) in DynamoDB using an API call. 同步:在S3上传回调之后,使用API​​调用将URL(可以使用存储桶名称+路径+文件名生成URL)保存在DynamoDB中。
  • Asynchronous: Having event-driven pipeline with an S3 trigger to Lambda to do the saving to DynamoDB. 异步:具有事件驱动管道和S3触发器,以Lambda进行保存到DynamoDB。

Normally in an Angular app, you need to show the image as soon as you upload. 通常,在Angular应用中,您需要在上传后立即显示图片。 For this, if you use the Asynchronous approach, you can construct the image URL and display it in Angular App (If you have a screen or component to show it) while expecting Lambda to add it and store it in DynamoDB. 为此,如果您使用异步方法,则可以构造图像URL并在Angular App中显示(如果您有显示屏幕或组件的话),同时希望Lambda添加它并将其存储在DynamoDB中。

Note: Here, I haven't mentioned any approach better compared to other since each one has tradeoffs over the other. 注意:在这里,我没有提到比其他方法更好的方法,因为每个方法都需要权衡其他方法。

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

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