title: Upload file to S3 folder using python boto
body: <p>I am trying to upload files from local directory to S3 folder. I am able to upload files to S3 bucket but I am unable to upload files to folder within S3 bucket. </p>
  
  <p>Could any one help? What am i doing wrong here..</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import os
  import sys 
  import boto3
  import fnmatch
  import pprint
  import re
  import hashlib
  
  SOURCE_DIR  = '/home/user/Downloads/tracks/'
  BUCKET_NAME = 'mybucket'
  S3_FOLDER = 'mybucket/folder1/'
  
  client = boto3.client('s3')
  
  s3 = boto3.resource('s3')
  
  def get_md5(filename):
      f = open(filename, 'rb')
      m = hashlib.md5()
  
      while True:
          data = f.read(10240)
          if len(data) == 0:
              break
          m.update(data)
  
      return m.hexdigest()
  
  def get_etag(filebase,filepath):
      for item in bucket.objects.all():
          keyfile = S3_FOLDER + filebase
          if(keyfile == item.key):
              md5  = get_md5(filepath)
              etag = item.e_tag.strip('"').strip("'")
  
              if etag != md5:
                  print(filebase + ": " + md5 + " != " + etag)
                  return(files_to_upload.append(filepath))
          else:
              return(files_to_upload.append(filepath))
  
  files_to_upload = []
  for root, dirnames, filenames in os.walk(SOURCE_DIR):
      for filename in filenames:
          filepath = os.path.join(root, filename)
          get_etag(filename,filepath)
  
  for f in files_to_upload:
      client.put_object(Bucket=BUCKET_NAME, Key=f)
  </code></pre>