I am trying to fetch a file from an Amazon S3 bucket
.我使用 aws 的 Chalice 微服务编写了代码。
我无法理解如何解决这个问题。由于我对 aws 非常陌生,并且根据我的理解,我授予了 Amazon S3 完整 acess 的 IAM 角色的权限。我该怎么办?
这是我得到的错误。
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'
关联的代码:
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')
感谢您的帮助。
由于代码是在 lambda 上执行的,因为使用了圣杯,因此从 S3 下载的文件应该存储在/tmp
中,而不是存储在执行 lambda 函数代码的位置。
建议的解决方案是更改:
s3.Bucket(os.environ["BUCKET_NAME"]).download_file( 'key', 'key.pem')
到:
s3.Bucket(os.environ["BUCKET_NAME"]).download_file('key', '/tmp/key.pem')