将控制台的输出重定向到 AWS S3 上的文件
Redirect output of console to a file on AWS S3
假设我有一个网站,当我发送 GET 请求时,我 JSON 数据使用卷曲。我想将 curl 的输出重定向到 AWS S3。应该在 S3 上为其创建一个新文件。
目前我能够重定向输出以将其存储在本地。
curl -s -X GET 'http://website_that_returns_json.com' > folder_to_save/$(date +"%Y-%m-%d_%H-%M.json")
我安装了 AWS CLI
和 s3cmd
。我将如何重定向 create 的输出以在 AWS S3 上创建一个新文件?
假设:
- AWS S3 访问密钥和密钥已经设置。
- 存储文件的位置:
mybucket/$(date +"%Y-%m-%d_%H-%M.json"
AWS Command-Line Interface (CLI) has the ability to stream data to/from Amazon S3:
The following cp
command uploads a local file stream from standard input to a specified bucket and key:
aws s3 cp - s3://mybucket/stream.txt
因此,您可以使用:
curl xxx | aws s3 cp - s3://mybucket/object.txt
但是,将文件保存在本地然后将其复制到 Amazon S3 可能更安全。
如果您想 运行 遥控器上的命令,请使用 aws ssm send-command
。
然后将该命令的输出重定向到 S3,您可以使用 --output-s3-bucket-name
参数。
这里是远程 运行 PowerShell 脚本的 Bash 脚本,并将其上传到 S3 存储桶:
instanceId="i-xyz"
bucketName="bucket_to_save"
bucketDir="folder_to_save"
command="Invoke-WebRequest -UseBasicParsing -Uri http://example.com).Content"
cmdId=$(aws ssm send-command --instance-ids "$instanceId" --document-name "AWS-RunPowerShellScript" --query "Command.CommandId" --output text --output-s3-bucket-name "$bucketName" --output-s3-key-prefix "$bucketDir" --parameters commands="'${command}'")
while [ "$(aws ssm list-command-invocations --command-id "$cmdId" --query "CommandInvocations[].Status" --output text)" == "InProgress" ]; do sleep 1; done
outputPath=$(aws ssm list-command-invocations --command-id "$cmdId" --details --query "CommandInvocations[].CommandPlugins[].OutputS3KeyPrefix" --output text)
echo "Command output uploaded at: s3://${bucketName}/${outputPath}"
aws s3 ls "s3://${bucketName}/${outputPath}"
要输出上传的S3文件,运行:
aws s3 ls s3://${bucketName}/${outputPath}/stderr.txt && aws s3 cp --quiet s3://${bucketName}/${outputPath}/stderr.txt /dev/stderr
aws s3 cp --quiet s3://${bucketName}/${outputPath}/stdout.txt /dev/stdout
假设我有一个网站,当我发送 GET 请求时,我 JSON 数据使用卷曲。我想将 curl 的输出重定向到 AWS S3。应该在 S3 上为其创建一个新文件。
目前我能够重定向输出以将其存储在本地。
curl -s -X GET 'http://website_that_returns_json.com' > folder_to_save/$(date +"%Y-%m-%d_%H-%M.json")
我安装了 AWS CLI
和 s3cmd
。我将如何重定向 create 的输出以在 AWS S3 上创建一个新文件?
假设:
- AWS S3 访问密钥和密钥已经设置。
- 存储文件的位置:
mybucket/$(date +"%Y-%m-%d_%H-%M.json"
AWS Command-Line Interface (CLI) has the ability to stream data to/from Amazon S3:
The following
cp
command uploads a local file stream from standard input to a specified bucket and key:
aws s3 cp - s3://mybucket/stream.txt
因此,您可以使用:
curl xxx | aws s3 cp - s3://mybucket/object.txt
但是,将文件保存在本地然后将其复制到 Amazon S3 可能更安全。
如果您想 运行 遥控器上的命令,请使用 aws ssm send-command
。
然后将该命令的输出重定向到 S3,您可以使用 --output-s3-bucket-name
参数。
这里是远程 运行 PowerShell 脚本的 Bash 脚本,并将其上传到 S3 存储桶:
instanceId="i-xyz"
bucketName="bucket_to_save"
bucketDir="folder_to_save"
command="Invoke-WebRequest -UseBasicParsing -Uri http://example.com).Content"
cmdId=$(aws ssm send-command --instance-ids "$instanceId" --document-name "AWS-RunPowerShellScript" --query "Command.CommandId" --output text --output-s3-bucket-name "$bucketName" --output-s3-key-prefix "$bucketDir" --parameters commands="'${command}'")
while [ "$(aws ssm list-command-invocations --command-id "$cmdId" --query "CommandInvocations[].Status" --output text)" == "InProgress" ]; do sleep 1; done
outputPath=$(aws ssm list-command-invocations --command-id "$cmdId" --details --query "CommandInvocations[].CommandPlugins[].OutputS3KeyPrefix" --output text)
echo "Command output uploaded at: s3://${bucketName}/${outputPath}"
aws s3 ls "s3://${bucketName}/${outputPath}"
要输出上传的S3文件,运行:
aws s3 ls s3://${bucketName}/${outputPath}/stderr.txt && aws s3 cp --quiet s3://${bucketName}/${outputPath}/stderr.txt /dev/stderr
aws s3 cp --quiet s3://${bucketName}/${outputPath}/stdout.txt /dev/stdout