Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

__all__ = ("s3CheckFileExists", "bucketExists", "setAwsEnvCredentials", 

"unsetAwsEnvCredentials") 

 

import os 

 

try: 

import boto3 

except ImportError: 

boto3 = None 

 

from lsst.daf.butler.core.location import ButlerURI, Location 

 

 

def s3CheckFileExists(path, bucket=None, client=None): 

"""Returns (True, filesize) if file exists in the bucket and (False, -1) if 

the file is not found. 

 

Parameters 

---------- 

path : `Location`, `ButlerURI`, `str` 

Location or ButlerURI containing the bucket name and filepath. 

bucket : `str`, optional 

Name of the bucket in which to look. If provided, path will be assumed 

to correspond to be relative to the given bucket. 

client : `boto3.client`, optional 

S3 Client object to query, if not supplied boto3 will try to resolve 

the credentials as in order described in its manual_. 

 

Returns 

------- 

exists : `bool` 

True if key exists, False otherwise. 

size : `int` 

Size of the key, if key exists, in bytes, otherwise -1 

 

Notes 

----- 

S3 Paths are sensitive to leading and trailing path separators. 

 

.. _manual: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/\ 

configuration.html#configuring-credentials 

""" 

if boto3 is None: 

raise ModuleNotFoundError(("Could not find boto3. " 

"Are you sure it is installed?")) 

 

if client is None: 

client = boto3.client('s3') 

 

if isinstance(path, str): 

if bucket is not None: 

filepath = path 

else: 

uri = ButlerURI(path) 

bucket = uri.netloc 

filepath = uri.relativeToPathRoot 

elif isinstance(path, (ButlerURI, Location)): 

bucket = path.netloc 

filepath = path.relativeToPathRoot 

 

try: 

obj = client.head_object(Bucket=bucket, Key=filepath) 

return (True, obj["ContentLength"]) 

except client.exceptions.ClientError as err: 

# resource unreachable error means key does not exist 

if err.response["ResponseMetadata"]["HTTPStatusCode"] == 404: 

return (False, -1) 

# head_object returns 404 when object does not exist only when user has 

# s3:ListBucket permission. If list permission does not exist a 403 is 

# returned. In practical terms this generally means that the file does 

# not exist, but it could also mean user lacks s3:GetObject permission: 

# https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectHEAD.html 

# I don't think its possible to discern which case is it with certainty 

if err.response["ResponseMetadata"]["HTTPStatusCode"] == 403: 

raise PermissionError("Forbidden HEAD operation error occured. " 

"Verify s3:ListBucket and s3:GetObject " 

"permissions are granted for your IAM user. ") from err 

raise 

 

 

def bucketExists(bucketName, client=None): 

"""Check if the S3 bucket with the given name actually exists. 

 

Parameters 

---------- 

bucketName : `str` 

Name of the S3 Bucket 

client : `boto3.client`, optional 

S3 Client object to query, if not supplied boto3 will try to resolve 

the credentials as in order described in its manual_. 

 

Returns 

------- 

exists : `bool` 

True if it exists, False if no Bucket with specified parameters is 

found. 

 

.. _manual: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/\ 

configuration.html#configuring-credentials 

""" 

if boto3 is None: 

raise ModuleNotFoundError(("Could not find boto3. " 

"Are you sure it is installed?")) 

 

s3 = boto3.client("s3") 

try: 

s3.get_bucket_location(Bucket=bucketName) 

return True 

except s3.exceptions.NoSuchBucket: 

return False 

 

 

def setAwsEnvCredentials(accessKeyId='dummyAccessKeyId', secretAccessKey="dummySecretAccessKey"): 

"""Set AWS credentials environmental variables AWS_ACCESS_KEY_ID and 

AWS_SECRET_ACCESS_KEY. 

 

Parameters 

---------- 

accessKeyId : `str` 

Value given to AWS_ACCESS_KEY_ID environmental variable. Defaults to 

'dummyAccessKeyId' 

secretAccessKey : `str` 

Value given to AWS_SECRET_ACCESS_KEY environmental variable. Defaults 

to 'dummySecretAccessKey' 

 

Returns 

------- 

setEnvCredentials : `bool` 

True when environmental variables were set, False otherwise. 

 

Notes 

----- 

If either AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are not set, both 

values are overwritten. 

""" 

if "AWS_ACCESS_KEY_ID" not in os.environ or "AWS_SECRET_ACCESS_KEY" not in os.environ: 

os.environ["AWS_ACCESS_KEY_ID"] = accessKeyId 

os.environ["AWS_SECRET_ACCESS_KEY"] = secretAccessKey 

return True 

return False 

 

 

def unsetAwsEnvCredentials(): 

"""Unsets AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environmental 

variables. 

""" 

if "AWS_ACCESS_KEY_ID" in os.environ: 

del os.environ["AWS_ACCESS_KEY_ID"] 

if "AWS_SECRET_ACCESS_KEY" in os.environ: 

del os.environ["AWS_SECRET_ACCESS_KEY"]