2017-02-16 81 views
2

說我有一個網站,還給我JSON數據時,我送使用捲曲一個GET請求。我想將curl的輸出重新指向AWS S3。應該在S3上爲它創建一個新文件。控制檯重定向輸出到文件上的AWS S3

目前我能夠重定向輸出以將其存儲在本地。

curl -s -X GET 'http://website_that_returns_json.com' > folder_to_save/$(date +"%Y-%m-%d_%H-%M.json") 

我已安裝AWS CLIs3cmd。我如何重定向create的輸出以在AWS S3上創建新文件?

假設:

  1. AWS S3訪問密鑰和祕密密鑰已經被設置。
  2. 位置來存儲文件:mybucket/$(date +"%Y-%m-%d_%H-%M.json"
+0

的一種方式是在本地保存文件,然後使用 - 'aws s3 cp local_copy s3_path'。但是有沒有一種有效的方法(不保存中間文件)來做到這一點? –

回答

1

AWS Command-Line Interface (CLI)具有從Amazon S3的能力stream data到/:

以下cp命令上傳來自標準輸入的本地文件流到指定的桶和關鍵:

aws s3 cp - s3://mybucket/stream.txt 

所以,喲您可以使用:

curl xxx | aws s3 cp - s3://mybucket/object.txt 

但是,在本地保存文件然後將其複製到Amazon S3可能更安全。

0

如果您想在遠程運行命令,請使用aws ssm send-command

然後,將該命令的輸出重定向到S3,可以使用--output-s3-bucket-name參數。

這裏是bash腳本在遠程運行PowerShell腳本,並上傳到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 
相關問題