title: boto3.exceptions.S3UploadFailedError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
body: <p>I'm running an Amazon EC2 (ubuntu) instance which outputs a JSON file daily.  I am now trying to copy this JSON to Amazon S3 so that I can eventually download it to my local machine.  Following the instructions here (<a href="https://stackoverflow.com/questions/59012982/reading-in-a-file-from-ubuntu-aws-ec2-on-local-machine">reading in a file from ubuntu (AWS EC2) on local machine?</a>), I'm using boto3 to copy the JSON from ubuntu to S3:</p>
  
  <pre><code>import boto3
  print("This script uploads the SF events JSON to s3")
  
  ACCESS_ID = 'xxxxxxxx'
  ACCESS_KEY = 'xxxxxxx'
  s3 = boto3.resource('s3',
           aws_access_key_id=ACCESS_ID,
           aws_secret_access_key= ACCESS_KEY)
  
  def upload_file_to_s3(s3_path, local_path):
      bucket = s3_path.split('/')[2]
      print(bucket)
      file_path = '/'.join(s3_path.split('/')[3:])
      print(file_path)
      response = s3.Object(bucket, file_path).upload_file(local_path)
      print(response)
  
  s3_path = "s3://mybucket/sf_events.json"
  local_path = "/home/ubuntu/bandsintown/sf_events.json"
  upload_file_to_s3(s3_path, local_path)
  </code></pre>
  
  <p>The credentials I'm using here are from creating a new user in Amazon Identity and Access Management (IAM): screenshot attached.</p>
  
  <p><a href="https://i.stack.imgur.com/afc8H.png" rel="noreferrer"><img src="https://i.stack.imgur.com/afc8H.png" alt="enter image description here"></a></p>
  
  <p>However, when I run this script, I get the following error:</p>
  
  <pre><code>boto3.exceptions.S3UploadFailedError: Failed to upload /home/ubuntu/bandsintown/sf_events.json to mybucket/sf_events.json: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
  </code></pre>
  
  <p>I've also tried attaching an IAM role to the EC2 instance and given that role full s3 permissions - but still no luck (see image below).</p>
  
  <p><a href="https://i.stack.imgur.com/bMPUM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/bMPUM.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/1SuZV.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1SuZV.png" alt="enter image description here"></a></p>
  
  <p>It appears to be a permissions issues - can anyone tell me how I might begin to solve this?  Do I need Amazon CLI?  I'm also reading in boto3 documentation that I may need an aws_session_token parameter in my script. </p>
  
  <p>Quite simply, I'm lost.  Thanks.</p>