Skip to content

Commit

Permalink
Merge pull request esl#13 from talko/master
Browse files Browse the repository at this point in the history
S3 Fixes
  • Loading branch information
gleber committed Oct 3, 2012
2 parents e00284d + e711037 commit cbd6bc8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
1 change: 1 addition & 0 deletions include/erlcloud_aws.hrl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-record(aws_config, {
ec2_host="ec2.amazonaws.com"::string(),
s3_scheme="https://"::string(),
s3_host="s3.amazonaws.com"::string(),
s3_port=80::non_neg_integer(),
sdb_host="sdb.amazonaws.com"::string(),
Expand Down
43 changes: 32 additions & 11 deletions src/erlcloud_s3.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
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,
make_link/3, make_link/4]).
make_link/3, make_link/4,
make_get_url/3, make_get_url/4]).

-include_lib("erlcloud/include/erlcloud.hrl").
-include_lib("erlcloud/include/erlcloud_aws.hrl").
Expand Down Expand Up @@ -270,6 +271,8 @@ extract_contents(Nodes) ->
{owner, "Owner", fun extract_user/1}],
[erlcloud_xml:decode(Attributes, Node) || Node <- Nodes].

extract_user([]) ->
[];
extract_user([Node]) ->
Attributes = [{id, "ID", text},
{display_name, "DisplayName", optional_text}],
Expand Down Expand Up @@ -571,27 +574,45 @@ 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()) -> {integer(), 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()) -> {integer(), string(), string()}.

make_link(Expire_time, BucketName, Key, Config)
-spec sign_get(integer(), string(), string(), aws_config()) -> {string(), string()}.
sign_get(Expire_time, BucketName, Key, Config)
when is_integer(Expire_time), is_list(BucketName), is_list(Key) ->
{Mega, Sec, _Micro} = os:timestamp(),
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)),
{Sig, Expires}.

-spec make_link(integer(), string(), string()) -> {integer(), string(), string()}.

make_link(Expire_time, BucketName, Key) ->
make_link(Expire_time, BucketName, Key, default_config()).

-spec make_link(integer(), string(), string(), aws_config()) -> {integer(), string(), string()}.

make_link(Expire_time, BucketName, Key, Config) ->
{Sig, Expires} = sign_get(Expire_time, BucketName, Key, Config),
Host = lists:flatten(["http://", BucketName, ".", Config#aws_config.s3_host, port_spec(Config)]),
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 make_get_url(integer(), string(), string()) -> iolist().

make_get_url(Expire_time, BucketName, Key) ->
make_get_url(Expire_time, BucketName, Key, default_config()).

-spec make_get_url(integer(), string(), string(), aws_config()) -> iolist().

make_get_url(Expire_time, BucketName, Key, Config) ->
{Sig, Expires} = sign_get(Expire_time, BucketName, Key, Config),
[Config#aws_config.s3_scheme, BucketName, ".", Config#aws_config.s3_host, port_spec(Config), "/", Key,
"?AWSAccessKeyId=", erlcloud_http:url_encode(Config#aws_config.access_key_id),
"&Signature=", erlcloud_http:url_encode(Sig),
"&Expires=", Expires].

-spec set_bucket_attribute(string(), atom(), term()) -> ok.

set_bucket_attribute(BucketName, AttributeName, Value) ->
Expand Down Expand Up @@ -710,7 +731,7 @@ s3_request(Config, Method, Host, Path, Subresource, Params, POSTData, Headers) -
_ -> [{"content-md5", binary_to_list(ContentMD5)}]
end,
RequestURI = lists:flatten([
"https://",
Config#aws_config.s3_scheme,
case Host of "" -> ""; _ -> [Host, $.] end,
Config#aws_config.s3_host, port_spec(Config),
EscapedPath,
Expand Down

0 comments on commit cbd6bc8

Please sign in to comment.