Skip to content

Commit

Permalink
Improved WebSite support
Browse files Browse the repository at this point in the history
* Renamed ws-list to ws-info
* Prettyfied ws-info output
* Include website endpoint in ws-info output
* Fixed --ws-error handling
  • Loading branch information
mludvig committed Jun 7, 2011
1 parent cde72d4 commit 3bf7d0c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -2,6 +2,8 @@ s3cmd 1.1.0 - ???
===========
* CloudFront invalidation via [sync --cf-invalidate] and [cfinvalinfo].
* Increased socket_timeout from 10 secs to 5 mins.
* Added "Static WebSite" support [ws-create / ws-delete / ws-info]
(contributed by Jens Braeuer)

s3cmd 1.0.0 - 2011-01-18
===========
Expand Down
3 changes: 2 additions & 1 deletion S3/Config.py
Expand Up @@ -77,7 +77,8 @@ class Config(object):
socket_timeout = 300
invalidate_on_cf = False
website_index = "index.html"
website_error = None
website_error = ""
website_endpoint = "http://%(bucket)s.s3-website-%(location)s.amazonaws.com/"

## Creating a singleton
def __new__(self, configfile = None):
Expand Down
30 changes: 22 additions & 8 deletions S3/S3.py
Expand Up @@ -238,27 +238,41 @@ def bucket_delete(self, bucket):
response = self.send_request(request)
return response

def bucket_info(self, uri):
def get_bucket_location(self, uri):
request = self.create_request("BUCKET_LIST", bucket = uri.bucket(), extra = "?location")
response = self.send_request(request)
response['bucket-location'] = getTextFromXml(response['data'], "LocationConstraint") or "any"
location = getTextFromXml(response['data'], "LocationConstraint")
if not location or location in [ "", "US" ]:
location = "us-east-1"
elif location == "EU":
location = "eu-west-1"
return location

def bucket_info(self, uri):
# For now reports only "Location". One day perhaps more.
response = {}
response['bucket-location'] = self.get_bucket_location(uri)
return response

def website_list(self, uri, bucket_location = None):
def website_info(self, uri, bucket_location = None):
headers = SortedDict(ignore_case = True)
bucket = uri.bucket()
body = ""

request = self.create_request("BUCKET_LIST", bucket = bucket, extra="?website")
response = None
try:
response = self.send_request(request, body)
response['index_document'] = getTextFromXml(response['data'], ".//IndexDocument//Suffix")
response['error_document'] = getTextFromXml(response['data'], ".//ErrorDocument//Key")
response['website_endpoint'] = self.config.website_endpoint % {
"bucket" : uri.bucket(),
"location" : self.get_bucket_location(uri)}
return response
except S3Error, e:
if e.status == 404:
debug("Could not get ?website. Assuming none set.")
else:
raise
return response
debug("Could not get /?website - website probably not configured for this bucket")
return None
raise

def website_create(self, uri, bucket_location = None):
headers = SortedDict(ignore_case = True)
Expand Down
15 changes: 8 additions & 7 deletions s3cmd
Expand Up @@ -164,20 +164,21 @@ def cmd_bucket_create(args):
else:
raise

def cmd_website_list(args):
def cmd_website_info(args):
s3 = S3(Config())
for arg in args:
uri = S3Uri(arg)
if not uri.type == "s3" or not uri.has_bucket() or uri.has_object():
raise ParameterError("Expecting S3 URI with just the bucket name set instead of '%s'" % arg)
try:
response = s3.website_list(uri, cfg.bucket_location)
response = s3.website_info(uri, cfg.bucket_location)
if response:
import xml.dom.minidom
xml = xml.dom.minidom.parseString(response['data'])
output(u"Bucket '%s': website configuration:\n%s" % (uri.uri(), xml.toprettyxml()))
output(u"Bucket %s: Website configuration" % uri.uri())
output(u"Website endpoint: %s" % response['website_endpoint'])
output(u"Index document: %s" % response['index_document'])
output(u"Error document: %s" % response['error_document'])
else:
output(u"Bucket '%s': unable to receive website configuration. None set?" % (uri.uri()))
output(u"Bucket %s: Unable to receive website configuration." % (uri.uri()))
except S3Error, e:
if S3.codes.has_key(e.info["Code"]):
error(S3.codes[e.info["Code"]] % uri.bucket())
Expand Down Expand Up @@ -1379,7 +1380,7 @@ def get_commands_list():
## Website commands
{"cmd":"ws-create", "label":"Create Website from bucket", "param":"s3://BUCKET", "func":cmd_website_create, "argc":1},
{"cmd":"ws-delete", "label":"Delete Website", "param":"s3://BUCKET", "func":cmd_website_delete, "argc":1},
{"cmd":"ws-list", "label":"List Websites", "param":"s3://BUCKET", "func":cmd_website_list, "argc":1},
{"cmd":"ws-info", "label":"Info about Website", "param":"s3://BUCKET", "func":cmd_website_info, "argc":1},

## CloudFront commands
{"cmd":"cflist", "label":"List CloudFront distribution points", "param":"", "func":CfCmd.info, "argc":0},
Expand Down

0 comments on commit 3bf7d0c

Please sign in to comment.