boto3: `FileNotFoundError` when using `download_file` method of s3 client object.

Describe the bug

When running download_file method of an s3 client object I am receiving this very unexpected error (see below traceback). Within the inner workings of boto it seems to be looking for a file at destination path, plus some random characters appended to the end (.6E57BFFa).

Strangely this code was working yesterday.

I’ve also tested in a minimal example outside of the code base and the same error persists.

I’ve tested with multiple files in multiple buckets.

Expected Behavior

I expected the download_file method to work as designed without throwing this error.

Current Behavior

  File "/home/nickpestell/apha-csu/repos/btb-phylo/utils.py", line 117, in s3_download_file
    s3.download_file(bucket, key, dest)
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/boto3/s3/inject.py", line 190, in download_file
    return transfer.download_file(
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/boto3/s3/transfer.py", line 320, in download_file
    future.result()
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/futures.py", line 103, in result
    return self._coordinator.result()
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/futures.py", line 266, in result
    raise self._exception
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/tasks.py", line 139, in __call__
    return self._execute_main(kwargs)
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/tasks.py", line 162, in _execute_main
    return_value = self._main(**kwargs)
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/download.py", line 642, in _main
    fileobj.seek(offset)
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/utils.py", line 378, in seek
    self._open_if_needed()
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/utils.py", line 361, in _open_if_needed
    self._fileobj = self._open_function(self._filename, self._mode)
  File "/home/nickpestell/python-virtualenvs/btb-phylo/lib/python3.8/site-packages/s3transfer/utils.py", line 272, in open
    return open(filename, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/fsx-042/share/phyloConsensus/phyloConsensus/AF-12-01538-15.fas.6E57BFFa'

Reproduction Steps

I expect this will not reproduce but I am simply running:

s3 = boto3.client('s3')
s3.download_file(<bucket>, <key>, <dest>)

where bucket, key and dest are all correctly formatted.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

‘1.24.28’

Environment details (OS name and version, etc.)

ubuntu 20.04

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

This bug is still present after following all the suggestions made above for boto3==1.26.90. Please provide some more guidance.

@FyzHsn the behavior you’re describing (a folder object is created if you use the S3 console to create folder/, but a folder object does not get created if an object is uploaded to folder/dog.png via boto3) is standard behavior across all S3 SDKs, not a bug. In the general case, there are no folder objects in S3 at all. An object folder/dog.png can exist without an object folder/ existing. Folders are, in a sense, virtual and inferred from key prefixes in existing objects.

It’s not sure but in my case, it seems to be raised when I try to save file to directory that not exists.

suppose that our code is:

bucket.download_file("my-s3-key", "foo/myfile.txt")

and if the foo directory not exists, the error is raised.

you should mkdir foo directory before download from s3. give it a try.

Thanks @nick-pestell for following up. Does the sub-directory destination path exist that you’re trying to download to? You can use an approach like this to make sure the directories are created:

import boto3
s3=boto3.client('s3')
list=s3.list_objects(Bucket='bucket')['Contents']
for s3_key in list:
    s3_object = s3_key['Key']
    if not s3_object.endswith("/"):
        s3.download_file('bucket', s3_object, s3_object)
    else:
        import os
        if not os.path.exists(s3_object):
            os.makedirs(s3_object)