[英]psql cli loop through query results and use AWS cli
I am trying to rename a large amount of files located in an AWS S3 bucket.我正在尝试重命名位于 AWS S3 存储桶中的大量文件。 What I am doing is using
psql
cli within my docker container image.我正在做的是在我的 docker 容器映像中使用
psql
cli。
I did docker exec -it [dbname] bash
我做了
docker exec -it [dbname] bash
Then did psql -c -d [dbname] select * from t1
然后
psql -c -d [dbname] select * from t1
How can I access the results from the psql query above and also loop through it so that for each iteration, I will use AWS CLI command operations to move and rename object in bucket A to bucket B如何访问上面 psql 查询的结果并循环遍历它,以便对于每次迭代,我将使用 AWS CLI 命令操作将存储桶 A 中的 object 移动和重命名为存储桶 B
E.g
//Results from query
**NAME**
George
Kim
Sam
**S3 Bucket A**
file_one.pdf
file_two.pdf
file_three.pdf
**S3 Bucket B (need to rename and move here)**
file_one_george.pdf
file_one_kim.pdf
file_one_same.pdf
I saw something like this aws s3 --recursive mv s3://<bucketname>/<folder_name_from> s3://<bucket>/<folder_name_to>
here stackoverflow我在这里看到了类似
aws s3 --recursive mv s3://<bucketname>/<folder_name_from> s3://<bucket>/<folder_name_to>
的东西stackoverflow
I need to run an AWS command similar to that post but need to loop through the psql query results.我需要运行类似于该帖子的 AWS 命令,但需要遍历 psql 查询结果。 Not sure about this but do I also need some kind of logic for each file in the AWS Bucket A. Such as
对此不确定,但我是否还需要为 AWS Bucket A 中的每个文件提供某种逻辑。例如
if (file one name in bucket A == psql query result row[x] name)
//run aws cli command to rename file and move to new bucket
You need to write the results from your query to a file.您需要将查询结果写入文件。 You can do this with the redirection operator
>
.您可以使用重定向运算符
>
来执行此操作。 The following will run the query and put the result into output.txt
以下将运行查询并将结果放入
output.txt
psql -d [dbname] -c 'select * from t1' > output.txt
Even though we got the query to store in the output.txt
, it is probably still on the docker image you initially logged into.尽管我们将查询存储在
output.txt
中,但它可能仍在您最初登录的 docker 映像中。 You will need to mount a local directory as a volume to have file output or pipe the output of the docker query to file. You will need to mount a local directory as a volume to have file output or pipe the output of the docker query to file. For Example, for getting the table
accounts
in a DB named my_db
with a user named postgres
, the command would be:例如,要使用名为
postgres
的用户在名为my_db
的数据库中获取表accounts
,命令将是:
docker exec -it my_db bash -c "psql -U postgres -d my_db -c 'select * from accounts'" > output.txt
Your command might be (although you might need to change the -U postgres
for the username for your DB):您的命令可能是(尽管您可能需要更改
-U postgres
为您的数据库的用户名):
docker exec -it [dbname] bash -c "psql -U postgres -d [dbname] -c 'select * from t1'" > output.txt
You can then load the query results from the file to run a script that will rename the S3 buckets with the aws
command.然后,您可以从文件加载查询结果以运行脚本,该脚本将使用
aws
命令重命名 S3 存储桶。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.