I’m trying to implement code in python for uploading multiple images into an S3 bucket. With only one image I can do it normally, but when I implemented this for loop, the following error started to appear:
JavaScript
x
2
1
FileNotFoundError: [Errno 2] No such file or directory: 'filename.jpg'
2
And this is the function I’ve been able to develop so far with the help of AWS documentation:
JavaScript
1
27
27
1
def upload_file(path, bucket, object_name=None):
2
"""
3
Upload files to an S3 bucket
4
5
:param bucket: Bucket to upload to
6
:param path: Path of the folder with files to upload
7
:param object_name: S3 object name. If not specified, then filename is used
8
:return: True if file was uploaded, else False
9
"""
10
11
# S3 bucket connection
12
s3_client = boto3.client('s3')
13
14
# List files from a folder
15
files = [f for f in listdir(path) if isfile(join(path, f))]
16
17
try:
18
# Upload the image
19
for file in files:
20
s3_client.upload_file(file, bucket, object_name)
21
22
except ClientError as e:
23
logging.error(e)
24
return False
25
26
return True
27
If anyone has any ideas to solve this problem, I appreciate it.
Advertisement
Answer
Change the line
JavaScript
1
2
1
files = [f for f in listdir(path) if isfile(join(path, f))]
2
to
JavaScript
1
2
1
files = [join(path, f) for f in listdir(path) if isfile(join(path, f))]
2
should resolve your issue.