Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
OCaml S3 Library?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel Patterson <dbpatt@g...>
Subject: OCaml S3 Library?
Has anyone written a library to interact with amazon web services
(specifically, S3)?

I started writing one, but something about the authentication is not working
properly, and I thought I'd check to see if anyone had already done this
before I try to figure out what is going wrong. Sample code follows, as if
there does not already exist a library, I'd be curious if anyone else sees
what is wrong with the authentication signature (what I assume is not
working), as now it 403's every time.

open Netencoding.Base64
open Cryptokit
open Cryptokit.MAC
open Http_client

let id = "___PUT_YOUR_ID_HERE____"
let key = "__PUT_YOUR_SECRET_KEY_HERE____"

let sign verb content_type date bucket url =
  let hash = hmac_sha1 key in
  let str_to_sign = verb ^ "\n\n" ^ content_type ^ "\n" ^  date ^ "\n" ^
 "/" ^ bucket ^ url  in
      Printf.printf "---------\n%s\n---------\n" str_to_sign;
      "AWS " ^ id ^ ":" ^ encode (hash_string hash str_to_sign)

let date () = Netdate.mk_mail_date (Unix.time ())

let text_put_s3 bucket url contents =
  let uri = "http://" ^ bucket ^ "" ^ url in
  let req = new put uri contents in
  let head = req#request_header `Base in
  let content_type = "text/plain" in
  let now = date () in
  let pipeline = new pipeline in
      pipeline#set_options {pipeline#get_options with verbose_status=true;
verbose_request_header=true; verbose_response_header=true;
verbose_response_contents=true; verbose_connection=true};
      head#set_fields [("Date", now); ("Content-Type", content_type);
("Authentication", sign "PUT" content_type now bucket url)];
      req#set_request_header head;
      pipeline#add req;
      pipeline#run ();
      (req#response_status_code, req#response_status_text)

let _ = text_put_s3 "__BUCKET_NAME__" "/test" "This is a Test."