Darth Vader
Darth Vader

Reputation: 921

Unable to fetch file from Amazon S3 with an API

I am trying to fetch a file from an Amazon S3 bucket. I have written the code using Chalice microservice of aws.

I am unable to understand how to solve this. As I am very new to aws and as per my understanding I gave permission to the IAM role of Amazon S3 full acess . What should I do?

This is the error I am getting.

Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 1112, in _get_view_function_response
    response = view_function(**function_args)
  File "/var/task/app.py", line 58, in fetch
    s3.Bucket(os.environ["BUCKET_NAME"]).download_file( 'key', 'key.pem')
  File "/var/task/boto3/s3/inject.py", line 246, in bucket_download_file
    ExtraArgs=ExtraArgs, Callback=Callback, Config=Config)
  File "/var/task/boto3/s3/inject.py", line 172, in download_file
    extra_args=ExtraArgs, callback=Callback)
  File "/var/task/boto3/s3/transfer.py", line 307, in download_file
    future.result()
  File "/var/task/s3transfer/futures.py", line 106, in result
    return self._coordinator.result()
  File "/var/task/s3transfer/futures.py", line 265, in result
    raise self._exception
  File "/var/task/s3transfer/tasks.py", line 126, in __call__
    return self._execute_main(kwargs)
  File "/var/task/s3transfer/tasks.py", line 150, in _execute_main
    return_value = self._main(**kwargs)
  File "/var/task/s3transfer/download.py", line 571, in _main
    fileobj.seek(offset)
  File "/var/task/s3transfer/utils.py", line 367, in seek
    self._open_if_needed()
  File "/var/task/s3transfer/utils.py", line 350, in _open_if_needed
    self._fileobj = self._open_function(self._filename, self._mode)
  File "/var/task/s3transfer/utils.py", line 261, in open
    return open(filename, mode)
OSError: [Errno 30] Read-only file system: 'key.pem.a7A6afCF'

Code associated:

    s3 = boto3.resource('s3')
    # s3.Bucket(os.environ["BUCKET_NAME"]).upload_file(Filename="requirements.txt" , Key="tryagain")
    s3.Bucket(os.environ["BUCKET_NAME"]).download_file( 'key', 'key.pem')

Thanks for your help.

Upvotes: 1

Views: 694

Answers (1)

Marcin
Marcin

Reputation: 238299

Since the code is being executed on lambda due do the use of Chalice, files downloaded from S3 should be stored in /tmp, rather then in the location where the lambda function code is being executed.

The proposed solution is to change:

s3.Bucket(os.environ["BUCKET_NAME"]).download_file( 'key', 'key.pem')

into:

s3.Bucket(os.environ["BUCKET_NAME"]).download_file('key', '/tmp/key.pem')

Upvotes: 2

Related Questions