简体   繁体   English

AWS 服务:使用 cloudwatch s3 put 事件触发批处理作业,并将文件 url 作为环境变量

[英]AWS Services: Use cloudwatch s3 put event to trigger batch job with the file url as an environemnt variable

Objective: Whenever an object is stored in the bucket, trigger a batch job (aws batch) and pass the uploaded file url as an environment variable目标:每当对象存储在存储桶中时,触发批处理作业(aws 批处理)并将上传的文件 url 作为环境变量传递

Situation: I currently have everything set up.情况:我目前已经设置好了一切。 I've got the s3 bucket with cloudwatch triggering batch jobs, but I am unable to get the full file url or to set environment variables.我有带有 cloudwatch 触发批处理作业的 s3 存储桶,但我无法获取完整的文件 url 或设置环境变量。

I have followed the following tutorial: https://docs.aws.amazon.com/batch/latest/userguide/batch-cwe-target.html "To create an AWS Batch target that uses the input transformer".我遵循了以下教程: https : //docs.aws.amazon.com/batch/latest/userguide/batch-cwe-target.html “创建使用输入转换器的 AWS Batch 目标”。

AWS Cloudwatch 输入转换器

The job is created and processed in AWS batch, and under the job details, i can see the parameters received are:作业是在 AWS 批处理中创建和处理的,在作业详细信息下,我可以看到收到的参数是:

S3bucket: mybucket
S3key: view-0001/custom/2019-08-07T09:40:04.989384.json

But the environment variables have not changed, and the file URL does not contain all the other parameters such as access and expiration tokens.但是环境变量没有改变,文件 URL 不包含所有其他参数,如访问和过期令牌。

I have also not found any information about what other variables can be used in the input transformer.我也没有找到关于输入变压器中可以使用哪些其他变量的任何信息。 If anyone has a link to a manual, it would be welcome.如果有人有手册的链接,那将是受欢迎的。

Also, in the WAS CLI documentation, it is possible to set the environment variables when submitting a job, so i guess it should be possible here as well?另外,在 WAS CLI 文档中,可以在提交作业时设置环境变量,所以我想这里也应该可以吗? https://docs.aws.amazon.com/cli/latest/reference/batch/submit-job.html https://docs.aws.amazon.com/cli/latest/reference/batch/submit-job.html

So the question is, how to submit a job with the file url as an environment variable?那么问题来了,如何以文件url作为环境变量提交作业呢?

You could accomplish this by triggering a Lambda function off the bucket and generating a pre-signed URL in the Lambda function and starting a Batch job from the Lambda function.您可以通过在存储桶外触发 Lambda 函数并在 Lambda 函数中生成预签名 URL 并从 Lambda 函数启动批处理作业来完成此操作。

However, a better approach would be to simply access the file within the Batch function using the bucket and key.但是,更好的方法是使用存储桶和键在 Batch 函数中简单地访问文件。 You could use the AWS SDK for your language or simply use awscli.您可以使用适用于您的语言的 AWS 开发工具包或简单地使用 awscli。 For example you could download the file:例如,您可以下载文件:

aws s3 cp s3://$BUCKET/$KEY /tmp/file.json

On the other hand, if you need a pre-signed URL outside of the Batch function, you could generate one with the AWS SDK or awscli :另一方面,如果您需要 Batch 函数之外的预签名 URL,您可以使用 AWS SDK 或awscli生成一个:

aws s3 presign s3://$BUCKET/$KEY

With either of these approaches with accessing the file within the Batch job, you will need to configure the instance role of your Batch compute environment with IAM access to your S3 bucket.使用这些方法中的任何一种访问批处理作业中的文件,您都需要配置批处理计算环境的实例角色,并通过 IAM 访问您的 S3 存储桶。

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

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