Search code examples
pythonamazon-web-servicesamazon-s3aws-cloud9

Python error using S3: Not such file or directory


Environment = AWS-Cloud9 and using s3 buckets.

I am trying to do an Upload_file command to upload a simple .txt file from my local Desktop to one of my s3 buckets and the outcome is:

"FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Dylan/OneDrive/Desktop/Hey.txt'"

I have double checked the paths and the file is there.

def upload_object():
    s3 = boto3.resource('s3')
s3.Bucket('dylanreed123132').upload_file('C:/Users/Dylan/OneDrive/Desktop/Hey.txt', 'Hey.txt')

I do have "Import boto3" at the begging.

*** Error Output ***
*** Welcome to the AWS S3 Functionality Console***
-Select one of the options below-
1. Create a new bucket
2. Put object in existing bucket
3. Delete object from existing bucket
4. Delete bucket
5. Copy an object from one bucket to another
6. Download an existing object from a bucket
7. Exit Program
Enter option: 2
Traceback (most recent call last):
File "/home/ec2-user/environment/HW1-S3/HW1.py", line 78, in <module>
main()
File "/home/ec2-user/environment/HW1-S3/HW1.py", line 60, in main
upload_object()
File "/home/ec2-user/environment/HW1-S3/HW1.py", line 21, in upload_object
s3.Bucket('dylanreed123132').upload_file('C:/Users/Dylan/OneDrive/Desktop/Hey.txt', 'Hey.txt')
File "/usr/local/lib/python3.7/site-packages/boto3/s3/inject.py", line 239, in bucket_upload_file
Config=Config,
File "/usr/local/lib/python3.7/site-packages/boto3/s3/inject.py", line 148, in upload_file
callback=Callback,
File "/usr/local/lib/python3.7/site-packages/boto3/s3/transfer.py", line 292, in upload_file
File "/usr/local/lib/python3.7/site-packages/s3transfer/futures.py", line 103, in result
return self._coordinator.result()
File "/usr/local/lib/python3.7/site-packages/s3transfer/futures.py", line 266, in result
raise self._exception
File "/usr/local/lib/python3.7/site-packages/s3transfer/tasks.py", line 269, in _main
self._submit(transfer_future=transfer_future, **kwargs)
File "/usr/local/lib/python3.7/site-packages/s3transfer/upload.py", line 585, in _submit
upload_input_manager.provide_transfer_size(transfer_future)
File "/usr/local/lib/python3.7/site-packages/s3transfer/upload.py", line 244, in provide_transfer_size
self._osutil.get_file_size(transfer_future.meta.call_args.fileobj)
File "/usr/local/lib/python3.7/site-packages/s3transfer/utils.py", line 247, in get_file_size
return os.path.getsize(filename)
File "/usr/lib64/python3.7/genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Dylan/OneDrive/Desktop/Hey.txt'

Here is screenshot of the whole thing code and also the file located in the on desktop.

enter image description here

enter image description here


Solution

  • It appears you are trying to run this AWS Code example:

    https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/python/example_code/s3/s3_basics/scenario_getting_started.py

    This code works and has been tested many times. Looks like your issue is you are trying to run this code in an IDE in the cloud. Instead - run your Python code in an IDE such as PyCharm on your local desktop which can see your local files. Running an IDE in the cloud does not have access to files on your desktop.