Readming .tif files in AWS S3 bucket through lambda function which are contained in another directory

Viewed 155
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')

try:
    response = s3.get_object(Bucket=bucket, Key=key)        
    x = gdal.Open(response).ReadAsArray()

I have used the default test event for fetching files from S3 in order to check its operation and I'm getting the below error when using the "response" variable.

{
  "errorMessage": "not a string",
  "errorType": "RuntimeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 107, in lambda_handler\n    raise e\n",
    "  File \"/var/task/lambda_function.py\", line 30, in lambda_handler\n    x = gdal.Open(response).ReadAsArray()\n",
    "  File \"/opt/python/osgeo/gdal.py\", line 3308, in Open\n    return _gdal.Open(*args)\n"
  ]
}

When I use the "key" variable which is a string, I am getting the below error:

{
  "errorMessage": "'NoneType' object has no attribute 'ReadAsArray'",
  "errorType": "AttributeError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 107, in lambda_handler\n    raise e\n",
    "  File \"/var/task/lambda_function.py\", line 30, in lambda_handler\n    x = gdal.Open(key).ReadAsArray()\n"
  ]
}


ERROR 4: cog_aridity.tif: No such file or directory

Although, when I replace the .tif file in the test event with a .json one the file can be read normally.

1 Answers

.tif doesn't work with lambda, I guess only png, pdf etc. You need to convert .tif to jpeg using lambda, save it in the s3 bucket, and pick jpeg from there to read the images.

Related