Browse code

* S3/S3Uri.py: Added S3UriCloudFront. Added S3UriS3.httpurl_to_s3uri() static method.

git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3cmd/branches/s3cmd-airlock@343 830e0280-6d2a-0410-9c65-932aecc39d9d

Michal Ludvig authored on 2009/01/17 21:23:21
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+2009-01-18  Michal Ludvig  <michal@logix.cz>
2
+
3
+	* S3/S3Uri.py: Added S3UriCloudFront.
4
+	  Added S3UriS3.httpurl_to_s3uri() static method.
5
+
1 6
 2009-01-17  Michal Ludvig  <michal@logix.cz>
2 7
 
3 8
 	* S3/CloudFront.py: Initial support for creating new Distros,
... ...
@@ -90,6 +90,29 @@ class S3UriS3(S3Uri):
90 90
 	def compose_uri(bucket, object = ""):
91 91
 		return "s3://%s/%s" % (bucket, object)
92 92
 
93
+	@staticmethod
94
+	def httpurl_to_s3uri(http_url):
95
+		m=re.match("(https?://)?([^/]+)/?(.*)", http_url, re.IGNORECASE)
96
+		hostname, object = m.groups()[1:]
97
+		hostname = hostname.lower()
98
+		if hostname == "s3.amazonaws.com":
99
+			## old-style url: http://s3.amazonaws.com/bucket/object
100
+			if object.count("/") == 0:
101
+				## no object given
102
+				bucket = object
103
+				object = ""
104
+			else:
105
+				## bucket/object
106
+				bucket, object = object.split("/", 1)
107
+		elif hostname.endswith(".s3.amazonaws.com"):
108
+			## new-style url: http://bucket.s3.amazonaws.com/object
109
+			bucket = hostname[:-(len(".s3.amazonaws.com"))]
110
+		else:
111
+			raise ValueError("Unable to parse URL: %s" % http_url)
112
+		return S3Uri("s3://%(bucket)s/%(object)s" % { 
113
+			'bucket' : bucket,
114
+			'object' : object })
115
+
93 116
 class S3UriS3FS(S3Uri):
94 117
 	type = "s3fs"
95 118
 	_re = re.compile("^s3fs://([^/]*)/?(.*)", re.IGNORECASE)
... ...
@@ -126,6 +149,22 @@ class S3UriFile(S3Uri):
126 126
 	def uri(self):
127 127
 		return "/".join(["file:/", self.path()])
128 128
 
129
+class S3UriCloudFront(S3Uri):
130
+	type = "cf"
131
+	_re = re.compile("^cf://([^/]*)/?", re.IGNORECASE)
132
+	def __init__(self, string):
133
+		match = self._re.match(string)
134
+		if not match:
135
+			raise ValueError("%s: not a CloudFront URI" % string)
136
+		groups = match.groups()
137
+		self._dist_id = groups[0]
138
+
139
+	def dist_id(self):
140
+		return self._dist_id
141
+
142
+	def uri(self):
143
+		return "/".join(["cf:/", self.dist_id()])
144
+
129 145
 if __name__ == "__main__":
130 146
 	uri = S3Uri("s3://bucket/object")
131 147
 	print "type()  =", type(uri)
... ...
@@ -155,3 +194,11 @@ if __name__ == "__main__":
155 155
 	print "uri.type=", uri.type
156 156
 	print "path    =", uri.path()
157 157
 	print
158
+
159
+	uri = S3Uri("cf://1234567890ABCD/")
160
+	print "type()  =", type(uri)
161
+	print "uri     =", uri
162
+	print "uri.type=", uri.type
163
+	print "dist_id =", uri.dist_id()
164
+	print
165
+