I have the following code written in Python. I wish to migrate it over to Bash, or use just plain old AWS CLI. Mission is to run a SQL query on a S3 bucket, using S3 Select. Note: the files in S3 are all gzipped.
Existing Python code (working)
ACCESS_KEY = 'Key1'
SECRET_KEY = 'Key2'
s3 = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY)
r = s3.select_object_content(
Bucket='bkt1',
Key=file2search,
ExpressionType='SQL',
Expression="SELECT * FROM s3object s where Lower(s._1) = (%r)" % ("SEARCH_STRING"),
InputSerialization = {'CompressionType': 'GZIP', 'CSV': {
'AllowQuotedRecordDelimiter': True,
'QuoteEscapeCharacter': '"',
'RecordDelimiter': '\n',
'FieldDelimiter': ':',
}},
OutputSerialization = {'CSV': {
'QuoteEscapeCharacter': '"',
'RecordDelimiter': '\n',
'FieldDelimiter': ':',
}}
)
Bash code (not working)
SEARCH_STRING="hello@world.com"
aws s3api select-object-content \
--bucket projectbucket2 \
--key abc.gz \
--expression "SELECT * FROM s3object s where Lower(s._1) = \'$SEARCH_STRING\'" \
--expression-type 'SQL' \
--input-serialization '{"CSV": {}, "CompressionType": "GZIP"}' \
--output-serialization '{"CSV": {}}' "output.csv"
The code throws an error:
An error occurred (LexerInvalidChar) when calling the SelectObjectContent operation: Invalid character at line 1, column 46.
It worked perfectly fine for me on Ubuntu:
ubuntu@ip-172-31-8-201:~$ aws --version
aws-cli/2.0.38 Python/3.7.3 Linux/5.3.0-1023-aws exe/x86_64.ubuntu.18
ubuntu@ip-172-31-8-201:~$ echo $SEARCH_STRING
taipei 101
ubuntu@ip-172-31-8-201:~$ aws s3api select-object-content --bucket my-bucket --key towers.csv --expression "SELECT * FROM s3object s where Lower(s._2) = '$SEARCH_STRING'" --expression-type 'SQL' --input-serialization '{"CSV": {}, "CompressionType": "NONE"}' --output-serialization '{"CSV": {}}' "output.csv"
ubuntu@ip-172-31-8-201:~$ cat output.csv
5,Taipei 101,Taipei,Taiwan,509,1670,101,2004
I was using AWS CLI v2.