Posted By

reboltutorial on 12/11/10


Tagged


Versions (?)

Amazon S3


 / Published in: R
 

URL: http://rebol.wik.is/S3

  1. Rebol [
  2. file: %s3.r
  3. author: [ "Graham Chiu" "Maarten Koopmans" "Gregg Irwin" ]
  4. date: 23-Nov-2008
  5. license: 'BSD
  6. purpose: {
  7. basic retrieve and upload of files to Amazon S3.
  8. }
  9. ]
  10.  
  11.  
  12. ; custom http prot - fetch from http://rebol.wik.is/Protocols/Http
  13. do %/d/rebol-sdk-276/source/prot-http.r
  14.  
  15. url-encode: func [
  16. "URL-encode a string"
  17. data "String to encode"
  18. /local new-data
  19. ] [
  20. new-data: make string! ""
  21. normal-char: charset [
  22. #"A" - #"Z" #"a" - #"z"
  23. #"@" #"." #"*" #"-" #"_"
  24. #"0" - #"9"
  25. ]
  26. if not string? data [return new-data]
  27. forall data [
  28. append new-data either find normal-char first data [
  29. first data
  30. ] [
  31. rejoin ["%" to-string skip tail (to-hex to-integer first data) -2]
  32. ]
  33. ]
  34. new-data
  35. ]
  36.  
  37. now-gmt: has [t] [
  38. t: now
  39. t: t - t/zone
  40. t/zone: none
  41. t
  42. ]
  43.  
  44. hmac-sha1: func [val key] [checksum/method/key val 'sha1 key]
  45.  
  46. make-sig: func [
  47. {Encodes the given string with the aws_secret_access_key, by taking the
  48. hmac-sha1 sum, and then base64 encoding it.}
  49. data [string!]
  50. secret-key [string!]
  51. /for-url "Make encoded string usable as a query string parameter"
  52. /local res
  53. ] [
  54. res: enbase/base hmac-sha1 data secret-key 64
  55. either for-url [url-encode res] [res]
  56. ]
  57.  
  58. Comment {
  59. StringToSign = HTTP-Verb + "\n" +
  60. Content-MD5 + "\n" +
  61. Content-Type + "\n" +
  62. Date + "\n" +
  63. CanonicalizedAmzHeaders +
  64. CanonicalizedResource;
  65.  
  66. }
  67.  
  68. create-string-to-sign: func [ verb md5 [none! string!] type url [url!]
  69. /local obj bucket resource s
  70. ][
  71. obj: make object! [path: target: port-id: host: none]
  72. net-utils/URL-Parser/parse-url obj url
  73. probe obj
  74. parse obj/host [ copy bucket to ".s3.amazonaws.com" ]
  75. resource: rejoin [ "/" bucket "/" any [ obj/path copy "" ] url-encode obj/target ]
  76. s: rejoin [ verb newline
  77. any [ md5 copy "" ] newline
  78. any [ type copy "" ] newline
  79. to-http-date newline
  80. resource
  81. ]
  82. ]
  83.  
  84. to-http-date: func [
  85. /local d
  86. ][
  87. d: now-gmt
  88. rejoin [
  89. copy/part pick system/locale/days d/weekday 3
  90. ", " next form 100 + d/day " "
  91. copy/part pick system/locale/months d/month 3
  92. " " d/year " "
  93. next form 100:00 + d/time " +0000"
  94. ]
  95. ]
  96.  
  97. Get-s3object: func [ url [url!] accesskey secretkey
  98. /info
  99. /local err signature result verb
  100. ][
  101. verb: either info [ 'HEAD ][ 'GET ]
  102. data: create-string-to-sign verb none none url
  103. signature: make-sig data secretkey
  104. if error? set/any 'err try [
  105. result: read/custom/binary url compose/deep [
  106. (verb) ""
  107. [
  108. Date: (to-http-date)
  109. Authorization: (rejoin [ "AWS " accesskey ":" signature ])
  110. Pragma: "no-cache"
  111. Cache-Control: "no-cache"
  112.  
  113. ]
  114. ]
  115. return result
  116. ][
  117. probe mold disarm err
  118. return none
  119. ]
  120. ]
  121.  
  122. Put-s3object: func [ url [url!] file [file!] accesskey secretkey
  123. /local err signature result content-type
  124. ][
  125. content-type: form switch/default suffix? file [
  126. %.html [ 'text/html ]
  127. %.jpg [ 'image/jpeg ]
  128. %.jpeg [ 'image/jpeg ]
  129. %.png [ 'image/png ]
  130. %.tiff [ 'image/tiff ]
  131. %.tif [ 'image/tiff ]
  132. %.pdf [ 'application/pdf ]
  133. %.txt [ 'text/plain ]
  134. %.xml [ 'application/xml ]
  135. %.mpeg [ 'video/mpeg ]
  136. ][ 'application/octet-stream ]
  137.  
  138. data: create-string-to-sign "PUT" none content-type url
  139. signature: make-sig data secretkey
  140. if error? set/any 'err try [
  141. result: read/custom url reduce compose/deep [
  142. 'PUT file
  143. [
  144. Date: (to-http-date)
  145. Authorization: (rejoin [ "AWS " accesskey ":" signature ])
  146. Content-type: (content-type)
  147. ]
  148. ]
  149. return result
  150. ][
  151. probe mold disarm err
  152. return none
  153. ]
  154. ]
  155.  
  156. ; as used in the examples at http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
  157. AWSAccessKeyId: "0PN5J17HBGZHT7JJ3X82" AWSSecretAccessKey: "uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o"
  158.  
  159. ; my secret keys
  160. ;;; ;============removed! ===============
  161.  
  162. shouldbe: {
  163. GET /photos/puppy.jpg HTTP/1.1
  164. Host: johnsmith.s3.amazonaws.com
  165. Date: Tue, 27 Mar 2007 19:36:42 +0000
  166. Authorization: AWS 0PN5J17HBGZHT7JJ3X82:xXjDGYUmKxnwqr5KXNPGldn5LbA=
  167. }
  168.  
  169. ; url: http://johnsmith.s3.amazonaws.com/photos/puppy.jpg
  170. url: http://comet.s3.amazonaws.com/CIMG0396.JPG
  171.  
  172. result: get-s3object url AWSAccessKeyId AWSSecretAccessKey
  173. result: put-s3object http://comet.s3.amazonaws.com/zonal.tiff %zonal.tif AWSAccessKeyId AWSSecretAccessKey

Report this snippet  

You need to login to post a comment.