2013-04-25 21:28:25 +00:00
|
|
|
{- S3 remotes
|
2011-03-28 02:00:44 +00:00
|
|
|
-
|
2013-04-25 21:28:25 +00:00
|
|
|
- Copyright 2011-2013 Joey Hess <joey@kitenet.net>
|
2011-03-28 02:00:44 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-04-25 20:42:17 +00:00
|
|
|
module Remote.S3 (remote, iaHost, isIA, isIAHost, iaItemUrl) where
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2011-03-28 05:32:47 +00:00
|
|
|
import Network.AWS.AWSConnection
|
2013-04-27 21:01:24 +00:00
|
|
|
import Network.AWS.S3Object hiding (getStorageClass)
|
2011-04-21 14:31:54 +00:00
|
|
|
import Network.AWS.S3Bucket hiding (size)
|
2011-03-28 05:32:47 +00:00
|
|
|
import Network.AWS.AWSResult
|
2012-12-01 18:11:37 +00:00
|
|
|
import qualified Data.Text as T
|
2014-05-28 00:31:25 +00:00
|
|
|
import qualified Data.Text.Encoding as T
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
2011-03-29 17:49:54 +00:00
|
|
|
import qualified Data.Map as M
|
2011-05-16 15:20:30 +00:00
|
|
|
import Data.Char
|
2013-04-25 17:14:49 +00:00
|
|
|
import Network.Socket (HostName)
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2011-10-05 20:02:51 +00:00
|
|
|
import Common.Annex
|
2011-06-02 01:56:04 +00:00
|
|
|
import Types.Remote
|
|
|
|
import Types.Key
|
2011-06-30 17:16:57 +00:00
|
|
|
import qualified Git
|
2011-03-30 19:15:46 +00:00
|
|
|
import Config
|
2013-03-13 20:16:01 +00:00
|
|
|
import Config.Cost
|
2011-08-17 00:49:54 +00:00
|
|
|
import Remote.Helper.Special
|
2012-11-20 20:43:58 +00:00
|
|
|
import qualified Remote.Helper.AWS as AWS
|
2012-11-14 23:32:27 +00:00
|
|
|
import Creds
|
2013-03-28 21:03:04 +00:00
|
|
|
import Utility.Metered
|
2013-09-07 22:38:00 +00:00
|
|
|
import Annex.UUID
|
2013-04-25 21:28:25 +00:00
|
|
|
import Logs.Web
|
|
|
|
|
|
|
|
type Bucket = String
|
2011-03-28 02:00:44 +00:00
|
|
|
|
2012-01-06 03:14:10 +00:00
|
|
|
remote :: RemoteType
|
|
|
|
remote = RemoteType {
|
2011-03-29 18:55:59 +00:00
|
|
|
typename = "S3",
|
2011-03-30 18:00:54 +00:00
|
|
|
enumerate = findSpecialRemotes "s3",
|
|
|
|
generate = gen,
|
2011-03-29 18:55:59 +00:00
|
|
|
setup = s3Setup
|
|
|
|
}
|
2011-03-29 03:51:07 +00:00
|
|
|
|
2013-09-12 19:54:35 +00:00
|
|
|
gen :: Git.Repo -> UUID -> RemoteConfig -> RemoteGitConfig -> Annex (Maybe Remote)
|
2013-01-01 17:52:47 +00:00
|
|
|
gen r u c gc = new <$> remoteCost gc expensiveRemoteCost
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
2014-08-03 19:35:23 +00:00
|
|
|
new cst = Just $ specialRemote c
|
2014-08-02 19:51:58 +00:00
|
|
|
(prepareStore this)
|
|
|
|
(prepareRetrieve this)
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
(simplyPrepare $ remove this c)
|
|
|
|
(simplyPrepare $ checkKey this)
|
2011-04-17 15:01:34 +00:00
|
|
|
this
|
2012-11-30 04:55:59 +00:00
|
|
|
where
|
|
|
|
this = Remote {
|
|
|
|
uuid = u,
|
|
|
|
cost = cst,
|
|
|
|
name = Git.repoDescribe r,
|
2014-08-02 19:51:58 +00:00
|
|
|
storeKey = storeKeyDummy,
|
|
|
|
retrieveKeyFile = retreiveKeyFileDummy,
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
retrieveKeyFileCheap = retrieveCheap,
|
|
|
|
removeKey = removeKeyDummy,
|
|
|
|
checkPresent = checkPresentDummy,
|
2014-08-06 17:45:19 +00:00
|
|
|
checkPresentCheap = False,
|
2012-11-30 04:55:59 +00:00
|
|
|
whereisKey = Nothing,
|
2013-10-11 20:03:18 +00:00
|
|
|
remoteFsck = Nothing,
|
2013-10-27 19:38:59 +00:00
|
|
|
repairRepo = Nothing,
|
2012-11-30 04:55:59 +00:00
|
|
|
config = c,
|
|
|
|
repo = r,
|
2013-01-01 17:52:47 +00:00
|
|
|
gitconfig = gc,
|
2012-11-30 04:55:59 +00:00
|
|
|
localpath = Nothing,
|
|
|
|
readonly = False,
|
2014-01-13 18:41:10 +00:00
|
|
|
availability = GloballyAvailable,
|
2012-11-30 04:55:59 +00:00
|
|
|
remotetype = remote
|
|
|
|
}
|
2011-03-28 05:32:47 +00:00
|
|
|
|
2014-02-11 18:06:50 +00:00
|
|
|
s3Setup :: Maybe UUID -> Maybe CredPair -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
s3Setup mu mcreds c = do
|
2013-09-07 22:38:00 +00:00
|
|
|
u <- maybe (liftIO genUUID) return mu
|
2014-02-24 19:14:44 +00:00
|
|
|
c' <- setRemoteCredPair c (AWS.creds u) mcreds
|
|
|
|
s3Setup' u c'
|
|
|
|
s3Setup' :: UUID -> RemoteConfig -> Annex (RemoteConfig, UUID)
|
|
|
|
s3Setup' u c = if isIA c then archiveorg else defaulthost
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
remotename = fromJust (M.lookup "name" c)
|
|
|
|
defbucket = remotename ++ "-" ++ fromUUID u
|
|
|
|
defaults = M.fromList
|
2012-12-01 18:11:37 +00:00
|
|
|
[ ("datacenter", T.unpack $ AWS.defaultRegion AWS.S3)
|
2012-11-11 04:51:07 +00:00
|
|
|
, ("storageclass", "STANDARD")
|
|
|
|
, ("host", defaultAmazonS3Host)
|
|
|
|
, ("port", show defaultAmazonS3Port)
|
|
|
|
, ("bucket", defbucket)
|
|
|
|
]
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
use fullconfig = do
|
|
|
|
gitConfigSpecialRemote u fullconfig "s3" "true"
|
2014-02-24 19:14:44 +00:00
|
|
|
return (fullconfig, u)
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
defaulthost = do
|
|
|
|
c' <- encryptionSetup c
|
|
|
|
let fullconfig = c' `M.union` defaults
|
|
|
|
genBucket fullconfig u
|
|
|
|
use fullconfig
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
archiveorg = do
|
|
|
|
showNote "Internet Archive mode"
|
2013-10-16 20:35:47 +00:00
|
|
|
-- Ensure user enters a valid bucket name, since
|
|
|
|
-- this determines the name of the archive.org item.
|
|
|
|
let bucket = replace " " "-" $ map toLower $
|
|
|
|
fromMaybe (error "specify bucket=") $
|
|
|
|
getBucket c
|
|
|
|
let archiveconfig =
|
2012-11-11 04:51:07 +00:00
|
|
|
-- hS3 does not pass through x-archive-* headers
|
|
|
|
M.mapKeys (replace "x-archive-" "x-amz-") $
|
|
|
|
-- encryption does not make sense here
|
|
|
|
M.insert "encryption" "none" $
|
2013-10-16 20:35:47 +00:00
|
|
|
M.insert "bucket" bucket $
|
2012-11-11 04:51:07 +00:00
|
|
|
M.union c $
|
|
|
|
-- special constraints on key names
|
|
|
|
M.insert "mungekeys" "ia" $
|
|
|
|
-- bucket created only when files are uploaded
|
2013-10-16 20:35:47 +00:00
|
|
|
M.insert "x-amz-auto-make-bucket" "1" defaults
|
|
|
|
writeUUIDFile archiveconfig u
|
|
|
|
use archiveconfig
|
2011-03-29 20:21:21 +00:00
|
|
|
|
2014-08-02 19:51:58 +00:00
|
|
|
prepareStore :: Remote -> Preparer Storer
|
|
|
|
prepareStore r = resourcePrepare (const $ s3Action r False) $ \(conn, bucket) ->
|
|
|
|
fileStorer $ \k src p -> do
|
|
|
|
ok <- s3Bool =<< liftIO (store (conn, bucket) r k p src)
|
2013-04-25 21:28:25 +00:00
|
|
|
|
|
|
|
-- Store public URL to item in Internet Archive.
|
2014-08-02 19:51:58 +00:00
|
|
|
when (ok && isIA (config r) && not (isChunkKey k)) $
|
2013-04-25 21:28:25 +00:00
|
|
|
setUrlPresent k (iaKeyUrl r k)
|
|
|
|
|
|
|
|
return ok
|
2011-04-17 15:01:34 +00:00
|
|
|
|
2014-08-02 19:51:58 +00:00
|
|
|
store :: (AWSConnection, Bucket) -> Remote -> Key -> MeterUpdate -> FilePath -> IO (AWSResult ())
|
|
|
|
store (conn, bucket) r k p file = do
|
|
|
|
size <- (fromIntegral . fileSize <$> getFileStatus file) :: IO Integer
|
|
|
|
withMeteredFile file p $ \content -> do
|
|
|
|
-- size is provided to S3 so the whole content
|
|
|
|
-- does not need to be buffered to calculate it
|
|
|
|
let object = S3Object
|
|
|
|
bucket (bucketFile r k) ""
|
|
|
|
(("Content-Length", show size) : getXheaders (config r))
|
|
|
|
content
|
|
|
|
sendObject conn $
|
|
|
|
setStorageClass (getStorageClass $ config r) object
|
|
|
|
|
|
|
|
prepareRetrieve :: Remote -> Preparer Retriever
|
|
|
|
prepareRetrieve r = resourcePrepare (const $ s3Action r False) $ \(conn, bucket) ->
|
2014-08-03 05:12:24 +00:00
|
|
|
byteRetriever $ \k sink ->
|
2014-08-02 19:51:58 +00:00
|
|
|
liftIO (getObject conn $ bucketKey r bucket k)
|
2014-08-03 05:12:24 +00:00
|
|
|
>>= either s3Error (sink . obj_data)
|
2011-04-19 18:45:19 +00:00
|
|
|
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
retrieveCheap :: Key -> FilePath -> Annex Bool
|
|
|
|
retrieveCheap _ _ = return False
|
2012-01-20 17:23:11 +00:00
|
|
|
|
2013-04-25 21:28:25 +00:00
|
|
|
{- Internet Archive doesn't easily allow removing content.
|
|
|
|
- While it may remove the file, there are generally other files
|
|
|
|
- derived from it that it does not remove. -}
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
remove :: Remote -> RemoteConfig -> Remover
|
2013-04-25 19:20:31 +00:00
|
|
|
remove r c k
|
|
|
|
| isIA c = do
|
|
|
|
warning "Cannot remove content from the Internet Archive"
|
|
|
|
return False
|
|
|
|
| otherwise = remove' r k
|
|
|
|
|
|
|
|
remove' :: Remote -> Key -> Annex Bool
|
2013-04-25 21:28:25 +00:00
|
|
|
remove' r k = s3Action r False $ \(conn, bucket) ->
|
|
|
|
s3Bool =<< liftIO (deleteObject conn $ bucketKey r bucket k)
|
2011-04-19 18:50:09 +00:00
|
|
|
|
run Preparer to get Remover and CheckPresent actions
This will allow special remotes to eg, open a http connection and reuse it,
while checking if chunks are present, or removing chunks.
S3 and WebDAV both need this to support chunks with reasonable speed.
Note that a special remote might want to cache a http connection across
multiple requests. A simple case of this is that CheckPresent is typically
called before Store or Remove. A remote using this interface can certianly
use a Preparer that eg, uses a MVar to cache a http connection.
However, it's up to the remote to then deal with things like stale or
stalled http connections when eg, doing a series of downloads from a remote
and other places. There could be long delays between calls to a remote,
which could lead to eg, http connection stalls; the machine might even
move to a new network, etc.
It might be nice to improve this interface later to allow
the simple case without needing to handle the full complex case.
One way to do it would be to have a `Transaction SpecialRemote cache`,
where SpecialRemote contains methods for Storer, Retriever, Remover, and
CheckPresent, that all expect to be passed a `cache`.
2014-08-06 18:28:36 +00:00
|
|
|
checkKey :: Remote -> CheckPresent
|
2014-08-06 17:45:19 +00:00
|
|
|
checkKey r k = s3Action r noconn $ \(conn, bucket) -> do
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction $ "checking " ++ name r
|
2011-05-16 15:20:30 +00:00
|
|
|
res <- liftIO $ getObjectInfo conn $ bucketKey r bucket k
|
2011-03-29 22:21:05 +00:00
|
|
|
case res of
|
2014-08-06 17:45:19 +00:00
|
|
|
Right _ -> return True
|
|
|
|
Left (AWSError _ _) -> return False
|
|
|
|
Left e -> s3Error e
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2014-08-06 17:45:19 +00:00
|
|
|
noconn = error "S3 not configured"
|
2011-04-19 18:45:19 +00:00
|
|
|
|
|
|
|
s3Warning :: ReqError -> Annex Bool
|
|
|
|
s3Warning e = do
|
|
|
|
warning $ prettyReqError e
|
|
|
|
return False
|
|
|
|
|
|
|
|
s3Error :: ReqError -> a
|
|
|
|
s3Error e = error $ prettyReqError e
|
|
|
|
|
|
|
|
s3Bool :: AWSResult () -> Annex Bool
|
2011-07-15 16:47:14 +00:00
|
|
|
s3Bool (Right _) = return True
|
|
|
|
s3Bool (Left e) = s3Warning e
|
2011-04-19 18:50:09 +00:00
|
|
|
|
2013-04-25 21:28:25 +00:00
|
|
|
s3Action :: Remote -> a -> ((AWSConnection, Bucket) -> Annex a) -> Annex a
|
2011-05-01 18:05:10 +00:00
|
|
|
s3Action r noconn action = do
|
2012-11-30 04:55:59 +00:00
|
|
|
let bucket = M.lookup "bucket" $ config r
|
|
|
|
conn <- s3Connection (config r) (uuid r)
|
2011-05-01 18:05:10 +00:00
|
|
|
case (bucket, conn) of
|
|
|
|
(Just b, Just c) -> action (c, b)
|
|
|
|
_ -> return noconn
|
|
|
|
|
2011-12-31 08:11:39 +00:00
|
|
|
bucketFile :: Remote -> Key -> FilePath
|
2012-08-08 20:06:01 +00:00
|
|
|
bucketFile r = munge . key2file
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
munge s = case M.lookup "mungekeys" c of
|
2013-04-27 21:01:24 +00:00
|
|
|
Just "ia" -> iaMunge $ filePrefix c ++ s
|
|
|
|
_ -> filePrefix c ++ s
|
2012-11-30 04:55:59 +00:00
|
|
|
c = config r
|
2011-05-16 15:20:30 +00:00
|
|
|
|
2013-04-27 21:01:24 +00:00
|
|
|
filePrefix :: RemoteConfig -> String
|
|
|
|
filePrefix = M.findWithDefault "" "fileprefix"
|
|
|
|
|
2013-04-25 21:28:25 +00:00
|
|
|
bucketKey :: Remote -> Bucket -> Key -> S3Object
|
2011-05-16 15:20:30 +00:00
|
|
|
bucketKey r bucket k = S3Object bucket (bucketFile r k) "" [] L.empty
|
|
|
|
|
|
|
|
{- Internet Archive limits filenames to a subset of ascii,
|
|
|
|
- with no whitespace. Other characters are xml entity
|
|
|
|
- encoded. -}
|
|
|
|
iaMunge :: String -> String
|
2011-05-16 18:49:28 +00:00
|
|
|
iaMunge = (>>= munge)
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
|
|
|
munge c
|
|
|
|
| isAsciiUpper c || isAsciiLower c || isNumber c = [c]
|
|
|
|
| c `elem` "_-.\"" = [c]
|
|
|
|
| isSpace c = []
|
|
|
|
| otherwise = "&" ++ show (ord c) ++ ";"
|
2011-05-01 18:05:10 +00:00
|
|
|
|
2014-07-11 19:21:43 +00:00
|
|
|
{- Generate the bucket if it does not already exist, including creating the
|
|
|
|
- UUID file within the bucket.
|
|
|
|
-
|
|
|
|
- To check if the bucket exists, ask for its location. However, some ACLs
|
|
|
|
- can allow read/write to buckets, but not querying location, so first
|
|
|
|
- check if the UUID file already exists and we can skip doing anything.
|
|
|
|
-}
|
2012-09-26 16:06:44 +00:00
|
|
|
genBucket :: RemoteConfig -> UUID -> Annex ()
|
|
|
|
genBucket c u = do
|
|
|
|
conn <- s3ConnectionRequired c u
|
2011-07-19 18:07:23 +00:00
|
|
|
showAction "checking bucket"
|
2014-07-11 19:21:43 +00:00
|
|
|
unlessM ((== Right True) <$> checkUUIDFile c u conn) $ do
|
|
|
|
loc <- liftIO $ getBucketLocation conn bucket
|
|
|
|
case loc of
|
|
|
|
Right _ -> writeUUIDFile c u
|
|
|
|
Left err@(NetworkError _) -> s3Error err
|
|
|
|
Left (AWSError _ _) -> do
|
|
|
|
showAction $ "creating bucket in " ++ datacenter
|
|
|
|
res <- liftIO $ createBucketIn conn bucket datacenter
|
|
|
|
case res of
|
|
|
|
Right _ -> writeUUIDFile c u
|
|
|
|
Left err -> s3Error err
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2013-04-27 21:01:24 +00:00
|
|
|
bucket = fromJust $ getBucket c
|
2012-11-11 04:51:07 +00:00
|
|
|
datacenter = fromJust $ M.lookup "datacenter" c
|
2011-05-16 13:42:54 +00:00
|
|
|
|
2013-04-27 21:01:24 +00:00
|
|
|
{- Writes the UUID to an annex-uuid file within the bucket.
|
|
|
|
-
|
|
|
|
- If the file already exists in the bucket, it must match.
|
|
|
|
-
|
|
|
|
- Note that IA items do not get created by createBucketIn.
|
|
|
|
- Rather, they are created the first time a file is stored in them.
|
|
|
|
- So this also takes care of that.
|
|
|
|
-}
|
|
|
|
writeUUIDFile :: RemoteConfig -> UUID -> Annex ()
|
|
|
|
writeUUIDFile c u = do
|
|
|
|
conn <- s3ConnectionRequired c u
|
2014-07-11 19:21:43 +00:00
|
|
|
v <- checkUUIDFile c u conn
|
|
|
|
case v of
|
|
|
|
Left e -> error e
|
|
|
|
Right True -> return ()
|
|
|
|
Right False -> do
|
|
|
|
let object = setStorageClass (getStorageClass c) (mkobject uuidb)
|
|
|
|
either s3Error return =<< liftIO (sendObject conn object)
|
2013-04-27 21:01:24 +00:00
|
|
|
where
|
2014-07-11 19:21:43 +00:00
|
|
|
file = uuidFile c
|
|
|
|
uuidb = L.fromChunks [T.encodeUtf8 $ T.pack $ fromUUID u]
|
|
|
|
bucket = fromJust $ getBucket c
|
|
|
|
|
|
|
|
mkobject = S3Object bucket file "" (getXheaders c)
|
|
|
|
|
|
|
|
{- Checks if the UUID file exists in the bucket and has the specified UUID already. -}
|
|
|
|
checkUUIDFile :: RemoteConfig -> UUID -> AWSConnection -> Annex (Either String Bool)
|
|
|
|
checkUUIDFile c u conn = check <$> liftIO (tryNonAsync $ getObject conn $ mkobject L.empty)
|
|
|
|
where
|
|
|
|
check (Right (Right o))
|
|
|
|
| obj_data o == uuidb = Right True
|
|
|
|
| otherwise = Left $ "This bucket is already in use by a different S3 special remote, with UUID: " ++ show (obj_data o)
|
|
|
|
check _ = Right False
|
2013-04-27 21:01:24 +00:00
|
|
|
|
2014-06-06 18:04:35 +00:00
|
|
|
uuidb = L.fromChunks [T.encodeUtf8 $ T.pack $ fromUUID u]
|
2013-04-27 21:01:24 +00:00
|
|
|
bucket = fromJust $ getBucket c
|
2014-07-11 19:21:43 +00:00
|
|
|
file = uuidFile c
|
2013-04-27 21:01:24 +00:00
|
|
|
|
|
|
|
mkobject = S3Object bucket file "" (getXheaders c)
|
|
|
|
|
2014-07-11 19:21:43 +00:00
|
|
|
uuidFile :: RemoteConfig -> FilePath
|
|
|
|
uuidFile c = filePrefix c ++ "annex-uuid"
|
|
|
|
|
2012-09-26 16:06:44 +00:00
|
|
|
s3ConnectionRequired :: RemoteConfig -> UUID -> Annex AWSConnection
|
|
|
|
s3ConnectionRequired c u =
|
|
|
|
maybe (error "Cannot connect to S3") return =<< s3Connection c u
|
2011-04-19 18:50:09 +00:00
|
|
|
|
2012-09-26 16:06:44 +00:00
|
|
|
s3Connection :: RemoteConfig -> UUID -> Annex (Maybe AWSConnection)
|
2012-11-28 17:31:49 +00:00
|
|
|
s3Connection c u = go =<< getRemoteCredPairFor "S3" c (AWS.creds u)
|
2012-11-11 04:51:07 +00:00
|
|
|
where
|
2012-11-20 20:43:58 +00:00
|
|
|
go Nothing = return Nothing
|
2012-11-14 23:32:27 +00:00
|
|
|
go (Just (ak, sk)) = return $ Just $ AWSConnection host port ak sk
|
|
|
|
|
2012-11-11 04:51:07 +00:00
|
|
|
host = fromJust $ M.lookup "host" c
|
|
|
|
port = let s = fromJust $ M.lookup "port" c in
|
|
|
|
case reads s of
|
|
|
|
[(p, _)] -> p
|
|
|
|
_ -> error $ "bad S3 port value: " ++ s
|
2013-04-25 17:14:49 +00:00
|
|
|
|
2013-04-27 21:01:24 +00:00
|
|
|
getBucket :: RemoteConfig -> Maybe Bucket
|
|
|
|
getBucket = M.lookup "bucket"
|
|
|
|
|
|
|
|
getStorageClass :: RemoteConfig -> StorageClass
|
|
|
|
getStorageClass c = case fromJust $ M.lookup "storageclass" c of
|
|
|
|
"REDUCED_REDUNDANCY" -> REDUCED_REDUNDANCY
|
|
|
|
_ -> STANDARD
|
|
|
|
|
|
|
|
getXheaders :: RemoteConfig -> [(String, String)]
|
|
|
|
getXheaders = filter isxheader . M.assocs
|
|
|
|
where
|
|
|
|
isxheader (h, _) = "x-amz-" `isPrefixOf` h
|
|
|
|
|
2013-04-25 17:14:49 +00:00
|
|
|
{- Hostname to use for archive.org S3. -}
|
|
|
|
iaHost :: HostName
|
|
|
|
iaHost = "s3.us.archive.org"
|
|
|
|
|
2013-04-25 19:20:31 +00:00
|
|
|
isIA :: RemoteConfig -> Bool
|
|
|
|
isIA c = maybe False isIAHost (M.lookup "host" c)
|
|
|
|
|
2013-04-25 17:14:49 +00:00
|
|
|
isIAHost :: HostName -> Bool
|
|
|
|
isIAHost h = ".archive.org" `isSuffixOf` map toLower h
|
2013-04-25 20:42:17 +00:00
|
|
|
|
2013-04-25 21:28:25 +00:00
|
|
|
iaItemUrl :: Bucket -> URLString
|
2013-04-25 20:42:17 +00:00
|
|
|
iaItemUrl bucket = "http://archive.org/details/" ++ bucket
|
2013-04-25 21:28:25 +00:00
|
|
|
|
|
|
|
iaKeyUrl :: Remote -> Key -> URLString
|
|
|
|
iaKeyUrl r k = "http://archive.org/download/" ++ bucket ++ "/" ++ bucketFile r k
|
|
|
|
where
|
2013-04-27 21:01:24 +00:00
|
|
|
bucket = fromMaybe "" $ getBucket $ config r
|