Skip to content

Autorization API

0
<p> Hello, </p> <p> I developpe an application on C++. I use an basical AWS bucket for my different test. My question is, how i connect on the bucket. A use "curl" but, i have probelme with the autorization. I suppose, i need create token or other but, i don't understand how a can do make this. </p>
curl_easy_setopt(curl,CURLOPT_URL,"http://<bucket name>.s3.us-east-2.amazonaws.com" );  
        curl_easy_setopt(curl, CURLOPT_CAINFO, pCertFile);                   
        curl_easy_setopt(curl, CURLOPT_PUT, 1L);                            
        
sprintf(autorization,"Authorization: AWS4-HMAC-SHA256 Credential=<key id>/eu-west-3/s3/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=<signature>");");       
        list = curl_slist_append(list,autorization);

        sprintf(autorization,"Date: Wed, 12 Oct 2023 17:50:00 GMT");           
        list = curl_slist_append(list,autorization);

        sprintf(autorization,"Content-Type: text/plain");          
        list = curl_slist_append(list,autorization);

        sprintf(autorization,"Content-Length: 11434");          
        list = curl_slist_append(list,autorization);

        sprintf(autorization,"x-amz-meta-author: Janet");           
        list = curl_slist_append(list,autorization);

        sprintf(autorization,"Expect: 100-continue");           
        list = curl_slist_append(list,autorization);

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);          
        curl_easy_setopt(curl, CURLOPT_READDATA,fileToUpload);   
        res=  curl_easy_perform(curl);                           

        printf("%s",res);
        curl_easy_cleanup(curl);                                  
        fclose(fileToUpload);

[Edit] After reseache, i discover this : https://docs.aws.amazon.com/AmazonS3/latest/userguide/RESTAuthentication.html#UsingTemporarySecurityCredentials I suppose i do use this, but i don't understand how

[Re-Edit] I supoose, i understand the possible solution but i don't understand how we calcul the signature. i have the message "signatureDoesNotMatch"

for calcul the signature a use this :

import base64
import hmac
from hashlib import sha1

access_key = '<key id>'.encode("UTF-8")
secret_key = '<secret id key>'.encode("UTF-8")

string_to_sign = 'PUT\n\n\nTue, 03 Jan 2022 14:45:00 +0000\n/meropy/'.encode("UTF-8")
signature = base64.b16encode(
                                hmac.new(
                                         secret_key, string_to_sign, sha1
                                         ).digest()
                                ).strip()
print(f"AWS {access_key.decode()}:{signature.decode()}")

1 Answer
0

To answer your lastest question directly, how to calculate the Sig4 request for AWS API, you can see the documentation and pseudo code here:

https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

However, this is a difficult/manual way to do it since this is baked into the C++ SDK. The easier (less code) approach would be to use the SDK directly to access your S3 bucket. Here is the documentation for the GetObject method which will return the specified object in S3:

https://sdk.amazonaws.com/cpp/api/LATEST/class_aws_1_1_s3_1_1_s3_client.html#a89fe252d5e360abcd5887b2b3f581870

There are a lot of other examples depending on what you are trying to accomplish with S3 (list objects, list buckets, etc.)

Good Luck!

AWS
answered 4 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.