erlang aliyun upload Oss

引用
注意内外网上传 我是用外网的  根据自己的需求  都是一年前写的东西

Begin

-module (uploadrecall).
% (外网):oss.aliyuncs.com
% (内网):oss-internal.aliyuncs.com
-define(ACCESS_ID, "meLQf8cj5D*****").
-define(SECRET_ACCESS_KEY, "KHSYv1ttqywsMrnnlq*******").
% uploadrecall:recall("d:/4.wav","old/8001/2013/05/31/4.wav").
-export ([recall/2]).
% FilePath,FileName
recall(FilePath,FileName) ->

	CONTENT_LENGTH = filelib:file_size(FilePath),
    OBJECT=FileName,
    % HOST="http://oss.aliyuncs.com/",
    DATE = lists:flatten(get_timestamp()),
    % CONTENT_TYPE="audio/x-wav",application/octet-stream
    CONTENT_TYPE="application/octet-stream",
    % CONTENT= erlang:md5("d:/msg.wav"),
    CanonicalizedResource="/jdy-file/"++FileName,
    Signature = base64:encode_to_string(crypto:sha_mac("KHSYv1ttqywsMrnnlq*******",("PUT\n\n"++CONTENT_TYPE++"\n"++DATE++"GMT\n"++CanonicalizedResource))),
    Authorization ="Authorization: OSS "++?ACCESS_ID++":"++Signature,
    {ok,Sock} = gen_tcp:connect("oss.aliyuncs.com", 80, [{active,false}]),
    {ok, File} = file:read_file(FilePath),
    gen_tcp:send(Sock, "PUT /"++OBJECT++" HTTP/1.1\r\nHost: oss.aliyuncs.com\r\nAccept-Encoding: identity\r\nDate: "++DATE++"GMT\r\nHost: jdy-file.oss.aliyuncs.com\r\nContent-Type: "++CONTENT_TYPE++"\r\nContent-Length: "++integer_to_list(CONTENT_LENGTH)++"\r\nExpect: 100-Continue\r\n"++Authorization++"\r\n\r\n"),
    gen_tcp:send(Sock, File),
    file:delete(FilePath).
    % gen_tcp:recv(Sock,50000),
    % gen_tcp:close(Sock).

    %Date时间方法    
get_timestamp() ->
    UTime = erlang:universaltime(),
    aly_fmt(UTime).

 aly_fmt(DateTime) ->
    {Date,{Hour,Min,Sec}} = DateTime,
    {Year,Month,Day} = Date,
    MonStr = month_to_str(Month),
    DWStr = date_to_dw_str(Date),
    io_lib:format("~s ~2.10.0B ~s ~4.10.0B ~2.10.0B:~2.10.0B:~2.10.0B ",
		  [DWStr++"," ,Day, MonStr, Year, Hour, Min, Sec]).

month_to_str(Month) ->
    element(Month, {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}).

date_to_dw_str(Date) ->
    DW = calendar:day_of_the_week(Date),
    element(DW, {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}).


end 


祝你好运

猜你喜欢

转载自laoyaos.iteye.com/blog/2022924