Permalink
Browse files

Implemented make_link in S3 lib, signed url's

  • Loading branch information...
1 parent 36bd0d0 commit 53a386510b7a02e54dd5e5e672fb6d3d086ae2f4 @jgordor jgordor committed Apr 20, 2012
Showing with 23 additions and 1 deletion.
  1. +23 −1 src/erlcloud_s3.erl
View
@@ -17,7 +17,8 @@
get_object_torrent/2, get_object_torrent/3,
get_object_metadata/2, get_object_metadata/3, get_object_metadata/4,
put_object/3, put_object/4, put_object/5, put_object/6,
- set_object_acl/3, set_object_acl/4]).
+ set_object_acl/3, set_object_acl/4,
+ make_link/3, make_link/4]).
-include_lib("erlcloud/include/erlcloud.hrl").
-include_lib("erlcloud/include/erlcloud_aws.hrl").
@@ -553,6 +554,27 @@ set_object_acl(BucketName, Key, ACL, Config)
XMLText = list_to_binary(xmerl:export_simple([XML], xmerl_xml)),
s3_simple_request(Config, put, BucketName, [$/|Key], "acl", [], XMLText, []).
+-spec make_link(integer(), string(), string()) -> string().
+
+make_link(Expire_time, BucketName, Key)
+ when is_integer(Expire_time), is_list(BucketName), is_list(Key) ->
+ make_link(Expire_time, BucketName, Key, default_config()).
+
+-spec make_link(integer(), string(), string(), aws_config()) -> string().
+
+make_link(Expire_time, BucketName, Key, Config)
+ when is_integer(Expire_time), is_list(BucketName), is_list(Key) ->
+ {Mega, Sec, _Micro} = erlang:now(),
+ Datetime = (Mega * 1000000) + Sec,
+ Expires = integer_to_list(Expire_time + Datetime),
+ To_sign = lists:flatten(["GET\n\n\n", Expires, "\n/", BucketName, "/", Key]),
+ Sig = base64:encode(crypto:sha_mac(Config#aws_config.secret_access_key, To_sign)),
+ Host = lists:flatten(["http://", BucketName, ".", Config#aws_config.s3_host]),
+ URI = lists:flatten(["/", Key, "?AWSAccessKeyId=", erlcloud_http:url_encode(Config#aws_config.access_key_id), "&Signature=", erlcloud_http:url_encode(Sig), "&Expires=", Expires]),
+ {list_to_integer(Expires),
+ binary_to_list(erlang:iolist_to_binary(Host)),
+ binary_to_list(erlang:iolist_to_binary(URI))}.
+
-spec set_bucket_attribute(string(), atom(), term()) -> ok.
set_bucket_attribute(BucketName, AttributeName, Value) ->

0 comments on commit 53a3865

Please sign in to comment.