on develop help sign in
Updating Storage examples

Reference #62

commitc4d364b23ee751845c7c9266ab7b7bf52c509946 authorBrendan Hay <brendan.g.hay@gmail.com> dateThu 23 Feb 2017, 08:33 (10 years ago) parentd61c7f5
2 files changed+45−23
examples/gogol-examples.cabal+1−5 examples/src/Example/Storage.hs+44−18
examples/gogol-examples.cabal+1−5
@@ -25,13 +25,9 @@ library
2525 , bytestring
2626 , conduit
2727 , conduit-extra
28− , exceptions
2928 , gogol == 0.2.0.*
3029 , gogol-core
3130 , gogol-storage
32− , http-conduit
3331 , lens
34− , semigroups
32+ , resourcet
3533 , text
36− , time
37− , transformers
examples/src/Example/Storage.hs+44−18
@@ -1,3 +1,4 @@
1+{-# LANGUAGE FlexibleContexts #-}
12{-# LANGUAGE OverloadedStrings #-}
23{-# LANGUAGE ScopedTypeVariables #-}
34
@@ -11,13 +12,19 @@
1112--
1213module Example.Storage where
1314
14−import Control.Lens ((&), (.~), (<&>), (?~))
15−import Data.Text (Text)
16−import Network.Google
17−import Network.Google.Storage
18−import System.IO (stdout)
15+import Control.Lens ((&), (.~), (<&>), (?~))
16+import Control.Monad.Trans.Resource (liftResourceT, runResourceT)
17+
18+import Data.Conduit (($$+-))
19+import Data.Text (Text)
20+
21+import System.IO (stdout)
22+
23+import qualified Data.Conduit.Binary as Conduit
24+import qualified Data.Text as Text
25+import qualified Network.Google as Google
26+import qualified Network.Google.Storage as Storage
1927
20−import qualified Data.Text as Text
2128
2229-- This will calculate the MIME type (and therefore Content-Type) of
2330-- the stored object based on the file extension.
@@ -30,15 +37,34 @@ import qualified Data.Text as Text
3037-- ...
3138--
3239-- -}
33−example :: Text -> FilePath -> IO ()
34−example bkt f = do
35− l <- newLogger Debug stdout
36− e <- newEnv <&> (envLogger .~ l) . (envScopes .~ storageReadWriteScope)
37− b <- sourceBody f
38−
39− let key = Text.pack f
40−
41− runResourceT . runGoogle e $ do
42− _ <- upload (objectsInsert bkt object' & oiName ?~ key) b
43− _ <- download (objectsGet bkt key)
44− pure ()
40+--example :: Text -> FilePath -> IO ()
41+example bucket input output = do
42+ -- Setup a logger to emit 'Debug' (or higher) log statements to 'stdout':
43+ lgr <- Google.newLogger Debug stdout
44+
45+ -- Create a new environment which will discover the appropriate
46+ -- AuthN/AuthZ credentials, and explicitly state the OAuth scopes
47+ -- we will be using below, which will be enforced by the compiler:
48+ env <- Google.newEnv <&>
49+ (Google.envLogger .~ lgr)
50+ . (Google.envScopes .~ Storage.storageReadWriteScope)
51+
52+ -- Create a streaming 'Conduit' source of the input file contents:
53+ body <- Google.sourceBody input
54+
55+ let key = Text.pack input
56+
57+ runResourceT . Google.runGoogle env $ do
58+ -- Upload the 'input' file contents to the specified bucket, using
59+ -- the file path as the key:
60+ void $ Google.upload
61+ (Storage.objectsInsert bucket object' & Storage.oiName ?~ key)
62+ body
63+
64+ -- Download from the bucket/key and create a source of the HTTP stream:
65+ stream <- Google.download (Storage.objectsGet bucket key)
66+
67+ -- 'Conduit' actions such as '$$+-' need to be lifted into the 'Google'
68+ -- monad using 'liftResourceT', this will stream the received bytes
69+ -- into the 'output' file:
70+ liftResourceT (stream $$+- Conduit.sinkFile output)