B4J Library [CLASS] AMAZON WEB SERVICES S3 V4 Signature Calculator [B4X - works on B4A/B4I/B4J]

Here is a port of AWS_S3_1_1.zip of:

https://www.b4x.com/android/forum/threads/amazon-web-services-s3-v4-signature-calculator.81006/

The attached zip contains a code module that calculates V4 signatures for Amazon Web Services (AWS) Simple Storage Service (S3) REST API requests, enabling creation of AWS S3 PUT/GET/DELETE requests.

You basically just plug in the properties necessary for a signature calculation and call Signature.

The accompanying example code has 4 worked examples that actually generate the signatures the AWS documentation says they should.

There is extensive in-line documentation.

I could never get:

https://www.b4x.com/android/forum/threads/amazon-s3-library.38699/#post-335436

working but am indebted to it for some hints in a couple of areas.

NOTE: THIS VERSION OF THE CODE MODULE IS TRI CROSS PLATFORM (B4A, B4J and B4I).

NOTE: To use under B4J you need to copy the B4A Encryption and ByteConverter Additional Libraries to B4J.
Enjoy...

Refer to subsequent posts #2, #3, #4 and #5 of:

https://www.b4x.com/android/forum/threads/amazon-web-services-s3-v4-signature-calculator.81006/

for practical ways to use the module with AWS S3 buckets - I will leave it to you to do the porting...

UPGRADE 1.7

Attached is version 1.7 which utilizes the new GMTFormatter.B4Xlib to handle extraction of GMT without having to change time zones. The significance of this is that it means any B4A/i/J apps using it can run over day light saving change events and still have all DateTime.Date and DateTime.Time calls work correctly.

TO RUN THE TESTS CORRECTLY YOU MUST PAY CLOSE ATTENTION TO THE INTERNAL COMMENTS RE "FIDDLES"
 

Attachments

  • AWS_S3_1_1.zip
    8.1 KB · Views: 264
  • AWS_S3_Test_1_7.zip
    9.2 KB · Views: 69
Last edited:

Kerbeross

Member
Licensed User
Hi, sorry for my bad English.
Is it possible to use this code module for interface to the API of Amazon advertising product?
Are there any examples of code?

This is the php code provided by Amazon to get the signed url to be queried:


PHP:
<?php

// Your Access Key ID, as taken from the Your Account page
$access_key_id = "***";

// Your Secret Key corresponding to the above ID, as taken from the Your Account page
$secret_key = "***";

// The region you are interested in
$endpoint = "webservices.amazon.it";

$uri = "/onca/xml";

$params = array(
    "Service" => "AWSECommerceService",
    "Operation" => "ItemLookup",
    "AWSAccessKeyId" => "***",
    "AssociateTag" => "***",
    "ItemId" => "B073SBZ8YH",
    "IdType" => "ASIN",
    "ResponseGroup" => "Images,ItemAttributes,Offers"
);

// Set current timestamp if not set
if (!isset($params["Timestamp"])) {
    $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
}

// Sort the parameters by key
ksort($params);

$pairs = array();

foreach ($params as $key => $value) {
    array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
}

// Generate the canonical query
$canonical_query_string = join("&", $pairs);

// Generate the string to be signed
$string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;

// Generate the signature required by the Product Advertising API
$signature = base64_encode(hash_hmac("sha256", $string_to_sign, $secret_key, true));

// Generate the signed URL
$request_url = 'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature);

echo "Signed URL: \"".$request_url."\"";

?>
 

JackKirk

Well-Known Member
Licensed User
Hi,

I haven't used the AWS advertising stuff but I have accessed a number of other AWS services using this code as a signature model - you just need to read the code and the particular AWS service's requirements carefully and sort out what is relevant and what is not.

Sorry I can't be of more help but I am under the hammer at the moment.
 
Top