简体   繁体   English

如何使用用户数据将文件从EC2传递到S3和从S3传递到EC2

[英]How to pass files from EC2 to S3 and S3 to EC2 using user data

Below code is shell script file which is created for creating New AWS EC2 instance with user data. 下面的代码是Shell脚本文件,该文件是为使用用户数据创建新AWS EC2实例而创建的。

In this code, it creates new instance and executes cd /home and creates dirctory named with pravin. 在此代码中,它将创建新实例并执行cd / home并创建以pravin命名的目录。

But after that it neither downloads file from s3 nor uploads to S3. 但是之后,它既不从s3下载文件,也不上传到S3。

What is wrong with that code(s3cmd get and put code). 该代码(s3cmd获取和放置代码)有什么问题。

And AMI used for this is pre-configured with AWS EC2 command line API and s3cmd. 并且为此使用的AMI已预先配置有AWS EC2命令行API和s3cmd。

str=$"#! /bin/bash"
str+=$"\ncd /home"
str+=$"\nmkdir pravin"
str+=$"\ns3cmd get inputFile.txt s3://bucketName/inputFile.txt"
str+=$\ns3cmd put resultFile.txt s3://bucketName/outputFile.txt"

echo "$str"|base64
ud=`echo -e "$str" |base64`
echo "$ud"

export JAVA_HOME=/usr
export EC2_HOME=/home/ec2-api-tools-1.6.7.1
export PATH=$PATH:$EC2_HOME/bin
export AWS_ACCESS_KEY=accesskey
export AWS_SECRET_KEY=secretkey
if [ "$3" = "us-east-1" ]
then
 ec2-run-instances ami-fa791231 -t t1.micro -g groupName -n 1 -k Key1 -d "$ud" --region $3 --instance-initiated-shutdown-behavior terminate
else
    echo "Not Valid region"
fi

There is a problem with your "s3cmd get" command. 您的“ s3cmd get”命令存在问题。 You have the parameter order backwards. 您将参数顺序向后。 From the "s3cmd --help" output: 从“ s3cmd --help”输出:

Put file into bucket
      s3cmd put FILE [FILE...] s3://BUCKET[/PREFIX]

Get file from bucket
      s3cmd get s3://BUCKET/OBJECT LOCAL_FILE

You can see that you need to change your get command to: 您会看到需要将get命令更改为:

str+=$"\ns3cmd get s3://bucketName/inputFile.txt inputFile.txt"

Note that the s3:// URI comes first, before the file name. 请注意,s3:// URI首先出现在文件名之前。 That should fix that issue. 那应该解决这个问题。 Your code also appears to be missing a quote for the put command: 您的代码也似乎缺少put命令的引号:

str+=$"\ns3cmd put resultFile.txt s3://bucketName/outputFile.txt"

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

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