skip to Main Content

I’ve used Azure (which has been a pleasure to use) but need to use AWS for work.

I’m attempting to send a local file to an s3 container and the code provided by AWS in documentation doesn’t seem to be working. Why is it not working and how can one fix it? Here is the Python code that’s based on. I did remove the logging due an error when installing that library with pip.

[https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html][1]
import boto3
from botocore.exceptions import ClientError
import os


def upload_file(file_name, bucket, object_name=None):
    """Upload a file to an S3 bucket

    :param file_name: File to upload
    :param bucket: Bucket to upload to
    :param object_name: S3 object name. If not specified then file_name is used
    :return: True if file was uploaded, else False
    """

    # If S3 object_name was not specified, use file_name
    if object_name is None:
        object_name = os.path.basename(file_name)

    # Upload the file
    s3_client = boto3.client('s3')
    try:
        response = s3_client.upload_file(file_name, bucket, object_name)
    except ClientError as e:
        return False
    return True

upload_file("test.csv", "call-lists")


  [1]: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-uploading-files.html

Error I am receiving:
botocore.exceptions.NoCredentialsError: Unable to locate credentials

I don’t see where I am supposed to place credentials nor am I sure what credentials need to be provided.

2

Answers


  1. If you run the code locally, you have to correctly setup AWS credentails on your computer. How to do it, is explained in details in AWS docs:

    Login or Signup to reply.
  2. If the code runs on an Amazon EC2 instance or an AWS Lambda function, then you should assign an IAM Role to the instance/function. This will provide credentials that the code can access.

    If you are running the code on your own computer (not an EC2 instance / Lambda function), then you will need to store credentials in a configuration file. The easiest way to do this is to use the AWS Command-Line Interface (CLI) aws configure command.

    It will request an Access Key and a Secret Key. You can obtain these from the Security credentials tab on an IAM User in the IAM management console (not IAM Identity Center).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search