Skip to content

Instantly share code, notes, and snippets.

@wdshin
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdshin/54896699bf1f5aed926e to your computer and use it in GitHub Desktop.
Save wdshin/54896699bf1f5aed926e to your computer and use it in GitHub Desktop.
%%%-------------------------------------------------------------------
%%% @author 신원동 <wodshin@gmail.com>
%%% @copyright (C) 2014, 신원동
%%% @doc
%%%
%%% @end
%%% Created : 4 Dec 2014 by 신원동 <wodshin@gmail.com>
%%%-------------------------------------------------------------------
-module(m_google_storage).
-compile(export_all).
%% API
-export([]).
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
%%%===================================================================
%%% Internal functions
%%%===================================================================
scope() ->
m_config:get(gce_storage_url,<<"https://www.googleapis.com/auth/devstorage.read_write">>).
iss() ->
m_config:get(gce_iss,<<"XXXXXX@developer.gserviceaccount.com">>).
pem_file() ->
m_config:get(gce_key_pem_file,"priv/google/some.key2.pem").
upload_file(Bucket,FilePath,Name) ->
{ ok , Token } = m_oauth2:access_token(iss(),scope(),pem_file()),
io:format("Token : ~s ~n",[Token]),
{ ok , Payload } = file:read_file(FilePath),
Authorization = << <<"Bearer ">>/binary , Token/binary >>,
Headers=[ {"Content-type", "image/jpeg" } ,
{"Content-Length", byte_size(Payload) },
{"Authorization" , binary_to_list(Authorization) }
],
io:format("Headers : ~p ~n",[Headers]),
URL = << <<"https://www.googleapis.com/upload/storage/v1/b/">>/binary , Bucket/binary ,
<<"/o?uploadType=media&predefinedAcl=publicRead&name=">>/binary , Name/binary
>>,
io:format("URL : ~p ~n",[URL]),
{ RetCode , OpCode, Json , Attachment } = ibrowse:send_req(binary_to_list(URL), Headers , post, Payload , [] ),
io:format("Ret : ~p ~n",[{ RetCode , OpCode, Json , Attachment }]),
PropList = jsx:decode(list_to_binary(Attachment)),
Link = proplists:get_value(<<"mediaLink">>,PropList),
io:format("link : ~s ~n",[Link]),
Link.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment