简体   繁体   English

通过用户数据方法将文件从 S3 复制到 EC2

[英]Copying File From S3 To EC2 by User Data Approach

I have been searching solution for this task, all I find CLI approaches which I don't want.我一直在寻找这个任务的解决方案,我找到了我不想要的 CLI 方法。

I simply want:我只想:

I have an S3 Bucket, which has one private file, file can be an image/zip file anything.我有一个 S3 Bucket,它有一个私有文件,文件可以是任何图像/zip 文件。

And I want when I launch any EC2 instance it should have taken that file from S3 bucket to EC2 instance directory.我希望当我启动任何 EC2 实例时,它应该将该文件从 S3 存储桶带到 EC2 实例目录。

And for this, I want to use only EC2 User Data Approach.为此,我只想使用 EC2 用户数据方法。

The User Data field in Amazon EC2 passes information to the instance that is accessible to applications running on the instance. Amazon EC2 中的用户数据字段将信息传递到实例上运行的应用程序可以访问的实例。

Amazon EC2 instances launched with Amazon-provided AMIs (eg Amazon Linux 2) include a program called Cloud-Init that looks at the User Data and, if a script is provided, runs that script the first time that the instance is booted.使用 Amazon 提供的 AMI(例如 Amazon Linux 2)启动的 Amazon EC2 实例包括一个名为 Cloud-Init 的程序,该程序查看用户数据,如果提供了脚本,则在实例首次启动时运行该脚本。

Therefore, you can configure a script (passed via User Data) that will run when the instance is first launched.因此,您可以配置将在首次启动实例时运行的脚本(通过用户数据传递)。 The script will run as the root user.该脚本将以root用户身份运行。 Your script could copy a file from Amazon S3 by using the AWS Command-Line Interface (CLI) , like this:您的脚本可以使用AWS 命令​​行界面 (CLI)从 Amazon S3 复制文件,如下所示:

#!
aws s3 cp s3://my-bucket/foo.txt /home/ec2-user/foo.txt
chown ec2-user foo.txt

Please note that you will need to assign an IAM Role to the instance that has permission to access the bucket.请注意,您需要为有权访问存储桶的实例分配一个 IAM 角色 The AWS CLI will use these permissions for the file copy. AWS CLI 将这些权限用于文件副本。

You mention that you do not wish to use the AWS CLI.您提到您不希望使用 AWS CLI。 You could, instead, write a program that calls the Amazon S3 API using a preferred programming language (eg Python), but using the CLI is much simpler.相反,您可以编写一个使用首选编程语言(例如 Python)调用 Amazon S3 API 的程序,但使用 CLI 要简单得多。

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

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