Permalink
Browse files

Add s3 redirect methods to the Key class

  • Loading branch information...
jamesls committed Oct 4, 2012
1 parent a7cb9c4 commit e9034394b2982c19a9cbb73ca9494ed45bf3711b
Showing with 61 additions and 1 deletion.
  1. +35 −0 boto/s3/key.py
  2. +26 −1 tests/integration/s3/test_key.py
View
@@ -433,6 +433,41 @@ def set_xml_acl(self, acl_str, headers=None):
def set_canned_acl(self, acl_str, headers=None):
return self.bucket.set_canned_acl(acl_str, self.name, headers)
+ def get_redirect(self):
+ """Return the redirect location configured for this key.
+
+ If no redirect is configured (via set_redirect), then None
+ will be returned.
+
+ """
+ response = self.bucket.connection.make_request(
+ 'GET', self.bucket.name, self.name)
+ if response.status == 200:
+ return response.getheader('x-amz-website-redirect-location')
+ else:
+ raise self.provider.storage_response_error(
+ response.status, response.reason, response.read())
+
+ def set_redirect(self, redirect_location):
+ """Configure this key to redirect to another location.
+
+ When the bucket associated with this key is accessed from the website
+ endpoint, a 301 redirect will be issued to the specified
+ `redirect_location`.
+
+ :type redirect_location: string
+ :param redirect_location: The location to redirect.
+
+ """
+ headers = {'x-amz-website-redirect-location': redirect_location}
+ response = self.bucket.connection.make_request('PUT', self.bucket.name,
+ self.name, headers)
+ if response.status == 200:
+ return True
+ else:
+ raise self.provider.storage_response_error(
+ response.status, response.reason, response.read())
+
def make_public(self, headers=None):
return self.bucket.set_canned_acl('public-read', self.name, headers)
@@ -24,13 +24,14 @@
Some unit tests for S3 Key
"""
-import unittest
+from tests.unit import unittest
import time
import StringIO
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from boto.exception import S3ResponseError
+
class S3KeyTest (unittest.TestCase):
s3 = True
@@ -348,3 +349,27 @@ def callback(wrote, total):
self.assertTrue(self.my_cb_cnt <= 1000)
self.assertEqual(self.my_cb_last, 20)
self.assertEqual(s, content)
+
+ def test_website_redirects(self):
+ self.bucket.configure_website('index.html')
+ key = self.bucket.new_key('redirect-key')
+ self.assertTrue(key.set_redirect('http://www.amazon.com/'))
+ self.assertEqual(key.get_redirect(), 'http://www.amazon.com/')
+
+ self.assertTrue(key.set_redirect('http://aws.amazon.com/'))
+ self.assertEqual(key.get_redirect(), 'http://aws.amazon.com/')
+
+ def test_website_redirect_none_configured(self):
+ key = self.bucket.new_key('redirect-key')
+ key.set_contents_from_string('')
+ self.assertEqual(key.get_redirect(), None)
+
+ def test_website_redirect_with_bad_value(self):
+ self.bucket.configure_website('index.html')
+ key = self.bucket.new_key('redirect-key')
+ with self.assertRaises(key.provider.storage_response_error):
+ # Must start with a / or http
+ key.set_redirect('ftp://ftp.example.org')
+ with self.assertRaises(key.provider.storage_response_error):
+ # Must start with a / or http
+ key.set_redirect('')

0 comments on commit e903439

Please sign in to comment.