如何使用Web界面触发EC2实例&运行Python脚本?

发布于 2025-01-19 14:44:25 字数 330 浏览 1 评论 0 原文

我有一个 python 脚本,它获取 N 张图像并将其转换为一张全景图像。

工作流程如下:

  1. 通过 Web 界面将 n 个图像上传到 s3
  2. 这应该会触发 EC2 实例启动并运行脚本
  3. 将图像从 S3 复制到本地实例存储
  4. 执行脚本后,将结果复制到 S3存储桶
  5. 脚本竞争它已显示在网络上。
  6. 结果可以从网上下载

在此过程中,上传图片后如何启动实例?我怎么知道它执行了脚本?

我需要关于 2 和 5 的帮助。如何实施?我应该使用任何其他 AWS 服务吗? 注意:我不需要 lambda。

I have a python script that takes an N number of images and converts it to one panorama image.

The workflow is below:

  1. upload n number of images to s3 via the web interface
  2. This should trigger the EC2 instance to start and run the script
  3. Copy images from the S3 to local instance storage
  4. Once the script is executed, then copy the result to the S3 bucket
  5. Script competes it has shown to the web.
  6. Result downloadable from the web

In this process, how can I start the instance once I uploaded the images? How can I know it executed the script?

I need help on the 2 and 5. How to implement it? Should I use any other AWS Services?
Note: I don’t need lambda.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2025-01-26 14:44:25

如果经常转换映像,那么传统体系结构将是:

  • 某些东西将消息推入Amazon SQS队列(例如,单击Web应用程序上的“转换”按钮,或者上传到上传S3触发AWS lambda函数,该功能检查最终图像是否已上传)
  • Amazon EC2实例上的一个工人不断轮询SQS队列以查看是否有消息
  • ,如果找到消息,它会触发转换

过程只有很少处理图像,您需要启动/停止EC2实例来处理图像,那么架构将是:

  • 图像是上传到Amazon S3桶
  • Amazon S3的 触发每个上传的AWS lambda函数。 lambda函数确定是否已上传所有图像。如果是这样,它将调用 startInstances()打开已经创建并已加载所有必要软件的EC2实例。 (或者,如果您有一个正在控制该过程的Web应用程序,则可以启动EC2实例,并且不需要lambda函数。)
  • 存储在/var/var/lib/lib/cloud/scripts/scripts/script中的EC2实例上的任何脚本当实例是 start 时,每启动/将自动运行(请注意,这与仅在实例的第一个启动上运行的用户数据脚本不同)。脚本应从S3存储键中获取文件,并在处理完成后处理图像
  • ,脚本可以使用 sudo shotdown关闭EC2实例现在-h

但是,以上是异步过程。也就是说,Web应用程序(或lambda函数)启动了实例,但是没有返回“响应”。您将需要一些机制才能“知道”该实例已经完成工作。这可能就像在S3存储桶中查找具有特定名称的输出文件一样简单。此名称可以从输入映像的名称派生,也许您的应用程序在S3中创建一个配置文件,其中包含输入映像的文件名,还指定了输出图像的文件名。

因此,您的步骤5(“脚本竞争它已显示给网络”)可能需要网页每15秒检查一次结果,而不是简单地在发生时'显示'结果。

另请参阅:

If you are frequently converting images, then the traditional architecture would be:

  • Something pushes a message into an Amazon SQS queue (for example, clicking a 'convert' button on a web app, or maybe the upload to S3 triggers an AWS Lambda function that checks that the final image has been uploaded)
  • A worker on an Amazon EC2 instance continuously polls the SQS queue to see if any messages are available
  • If a message is found, it triggers the conversion process

However, if you are only rarely processing images and you need to start/stop the EC2 instance to process the images, then the architecture would be:

  • Images are uploaded to an Amazon S3 bucket
  • Amazon S3 triggers an AWS Lambda function for each upload. The Lambda function determines whether all images have been uploaded. If so, it then calls StartInstances() to turn on an EC2 instance that has already been created and has all necessary software loaded. (Alternatively, if you have a web app that is controlling the process, it can start the EC2 instance and the Lambda function is not required.)
  • Any script on the EC2 instance stored in /var/lib/cloud/scripts/per-boot/ will automatically run when the instance is started (note that this is different to User Data scripts that only run on the first boot of an instance). The script should grab the files from the S3 bucket and process the image
  • When processing is complete, the script can turn off the EC2 instance using sudo shutdown now -h

However, the above is an asynchronous process. That is, the web app (or Lambda function) starts the instance, but no 'response' is returned. You will need some mechanism to 'know' that the instance has finished its job. This could be as simple as looking in the S3 bucket for an output file with a particular name. This name could be derived from the names of the input images, or perhaps your app creates a configuration file in S3 that contains the filenames of the input images and also specifies the filename for the output image.

Therefore, your Step 5 ("Script competes it has shown to the web") will probably need the web page to check for a result every 15 seconds, rather than simply 'showing' a result when it happens.

See also: Auto-Stop EC2 instances when they finish a task - DEV Community

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文