Observing <Response [403]> when try to fetch the files in S3 bucket rest api requests using request module in python

0

By using python module requests, I am trying to fetch the files in given S3 bucket but observing <Response [403]>

Steps I followed : 1> In python I imported import base64 2> I generated a string to sign, this consists of 'get', date, time and bucket name and encoded this string with UTF-8 3> Now I generated signature using base64.b64encode and given Mac of security credentials of access_key and secret_key 4> I kept bucket name and region in URL 5> I kept 'Authorization' with above generated signature and host as path in the header dict 6> now I did requests.get(url, headers=headers) with timeout

I am observing <Response [403]>

gefragt vor einem Jahr370 Aufrufe
1 Antwort
0

Hi, the exact signature used by AWS service is SigV4.

It is described here: https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html

My advice is to use boto3 (python SDK for AWS services): it will sign the requests for you.

See https://aws.amazon.com/sdk-for-python/

If you really want to code SigV4 on you own, you can get inspired by boto3 source code at https://github.com/boto/botocore/blob/develop/botocore/auth.py

Hope it helps!

Didier

profile pictureAWS
EXPERTE
beantwortet vor einem Jahr
  • Hi Didier, Thanks for the response.

    As suggested in https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html#

    In the directory where I am running python scripts, I created new folder ".aws" and created 2 files inside this folder. File 1 name is "credentials" and kept aws_access_key_id and aws_secret_access_key inside that file. Another file name is "config" and mention the reason in this file. Now I launched python3 interactive mode and did "import boto3" and "s3 = boto3.resource('s3')". Here I tried the example code to list the buckets available.

    for bucket in s3.buckets.all(): print(bucket.name)

    Here I am fasing exception... botocore.exceptions.NoCredentialsError: Unable to locate credentials

    What I am missing, can you help

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen