OLD | NEW |
(Empty) | |
| 1 # Copyright 2012 Google Inc. All Rights Reserved. |
| 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at |
| 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. |
| 14 |
| 15 from gslib.command import Command |
| 16 from gslib.command import COMMAND_NAME |
| 17 from gslib.command import COMMAND_NAME_ALIASES |
| 18 from gslib.command import CONFIG_REQUIRED |
| 19 from gslib.command import FILE_URIS_OK |
| 20 from gslib.command import MAX_ARGS |
| 21 from gslib.command import MIN_ARGS |
| 22 from gslib.command import PROVIDER_URIS_OK |
| 23 from gslib.command import SUPPORTED_SUB_ARGS |
| 24 from gslib.command import URIS_START_ARG |
| 25 from gslib.exception import CommandException |
| 26 from gslib.help_provider import HELP_NAME |
| 27 from gslib.help_provider import HELP_NAME_ALIASES |
| 28 from gslib.help_provider import HELP_ONE_LINE_SUMMARY |
| 29 from gslib.help_provider import HELP_TEXT |
| 30 from gslib.help_provider import HelpType |
| 31 from gslib.help_provider import HELP_TYPE |
| 32 from gslib.util import NO_MAX |
| 33 |
| 34 |
| 35 _detailed_help_text = (""" |
| 36 <B>SYNOPSIS</B> |
| 37 gsutil setwebcfg [-m main_page_suffix] [-e error_page] bucket_uri... |
| 38 |
| 39 |
| 40 <B>DESCRIPTION</B> |
| 41 The Website Configuration feature enables you to configure a Google Cloud |
| 42 Storage bucket to behave like a static website. This means requests made via a |
| 43 domain-named bucket aliased using a Domain Name System "CNAME" to |
| 44 c.storage.googleapis.com will work like any other website, i.e., a GET to the |
| 45 bucket will serve the configured "main" page instead of the usual bucket |
| 46 listing and a GET for a non-existent object will serve the configured error |
| 47 page. |
| 48 |
| 49 For example, suppose your company's Domain name is example.com. You could set |
| 50 up a website bucket as follows: |
| 51 |
| 52 1. Create a bucket called example.com (see the "DOMAIN NAMED BUCKETS" |
| 53 section of "gsutil help naming" for details about creating such buckets). |
| 54 |
| 55 2. Create index.html and 404.html files and upload them to the bucket. |
| 56 |
| 57 3. Configure the bucket to have website behavior using the command: |
| 58 |
| 59 gsutil setwebcfg -m index.html -e 404.html gs://example.com |
| 60 |
| 61 4. Add a DNS CNAME record for example.com pointing to c.storage.googleapis.com |
| 62 (ask your DNS administrator for help with this). |
| 63 |
| 64 Now if you open a browser and navigate to http://example.com, it will display |
| 65 the main page instead of the default bucket listing. Note: It can take time |
| 66 for DNS updates to propagate because of caching used by the DNS, so it may |
| 67 take up to a day for the domain-named bucket website to work after you create |
| 68 the CNAME DNS record. |
| 69 |
| 70 Additional notes: |
| 71 |
| 72 1. Because the main page is only served when a bucket listing request is made |
| 73 via the CNAME alias, you can continue to use "gsutil ls" to list the bucket |
| 74 and get the normal bucket listing (rather than the main page). |
| 75 |
| 76 2. The main_page_suffix applies to each subdirectory of the bucket. For |
| 77 example, with the main_page_suffix configured to be index.html, a GET |
| 78 request for http://example.com would retrieve |
| 79 http://example.com/index.html, and a GET request for |
| 80 http://example.com/photos would retrieve |
| 81 http://example.com/photos/index.html. |
| 82 |
| 83 2. There is just one 404.html page: For example, a GET request for |
| 84 http://example.com/photos/missing would retrieve |
| 85 http://example.com/404.html, not http://example.com/photos/404.html. |
| 86 |
| 87 3. For additional details see |
| 88 https://developers.google.com/storage/docs/website-configuration. |
| 89 |
| 90 |
| 91 <B>OPTIONS</B> |
| 92 -m index.html Specifies the object name to serve when a bucket listing |
| 93 is requested via the CNAME alias to |
| 94 c.storage.googleapis.com. |
| 95 |
| 96 -e 404.html Specifies the error page to serve when a request is made |
| 97 for a non-existing object, via the is requested via the |
| 98 CNAME alias to c.storage.googleapis.com. |
| 99 """) |
| 100 |
| 101 def BuildGSWebConfig(main_page_suffix=None, not_found_page=None): |
| 102 config_body_l = ['<WebsiteConfiguration>'] |
| 103 if main_page_suffix: |
| 104 config_body_l.append('<MainPageSuffix>%s</MainPageSuffix>' % |
| 105 main_page_suffix) |
| 106 if not_found_page: |
| 107 config_body_l.append('<NotFoundPage>%s</NotFoundPage>' % |
| 108 not_found_page) |
| 109 config_body_l.append('</WebsiteConfiguration>') |
| 110 return "".join(config_body_l) |
| 111 |
| 112 def BuildS3WebConfig(main_page_suffix=None, error_page=None): |
| 113 config_body_l = ['<WebsiteConfiguration xmlns="http://s3.amazonaws.com/doc/200
6-03-01/">'] |
| 114 if not main_page_suffix: |
| 115 raise CommandException('S3 requires main page / index document') |
| 116 config_body_l.append('<IndexDocument><Suffix>%s</Suffix></IndexDocument>' % |
| 117 main_page_suffix) |
| 118 if error_page: |
| 119 config_body_l.append('<ErrorDocument><Key>%s</Key></ErrorDocument>' % |
| 120 error_page) |
| 121 config_body_l.append('</WebsiteConfiguration>') |
| 122 return "".join(config_body_l) |
| 123 |
| 124 class SetWebcfgCommand(Command): |
| 125 """Implementation of gsutil setwebcfg command.""" |
| 126 |
| 127 # Command specification (processed by parent class). |
| 128 command_spec = { |
| 129 # Name of command. |
| 130 COMMAND_NAME : 'setwebcfg', |
| 131 # List of command name aliases. |
| 132 COMMAND_NAME_ALIASES : [], |
| 133 # Min number of args required by this command. |
| 134 MIN_ARGS : 1, |
| 135 # Max number of args required by this command, or NO_MAX. |
| 136 MAX_ARGS : NO_MAX, |
| 137 # Getopt-style string specifying acceptable sub args. |
| 138 SUPPORTED_SUB_ARGS : 'm:e:', |
| 139 # True if file URIs acceptable for this command. |
| 140 FILE_URIS_OK : False, |
| 141 # True if provider-only URIs acceptable for this command. |
| 142 PROVIDER_URIS_OK : False, |
| 143 # Index in args of first URI arg. |
| 144 URIS_START_ARG : 1, |
| 145 # True if must configure gsutil before running command. |
| 146 CONFIG_REQUIRED : True, |
| 147 } |
| 148 help_spec = { |
| 149 # Name of command or auxiliary help info for which this help applies. |
| 150 HELP_NAME : 'setwebcfg', |
| 151 # List of help name aliases. |
| 152 HELP_NAME_ALIASES : [], |
| 153 # Type of help) |
| 154 HELP_TYPE : HelpType.COMMAND_HELP, |
| 155 # One line summary of this help. |
| 156 HELP_ONE_LINE_SUMMARY : 'Set a main page and/or error page for one or more b
uckets', |
| 157 # The full help text. |
| 158 HELP_TEXT : _detailed_help_text, |
| 159 } |
| 160 |
| 161 |
| 162 # Command entry point. |
| 163 def RunCommand(self): |
| 164 main_page_suffix = None |
| 165 error_page = None |
| 166 if self.sub_opts: |
| 167 for o, a in self.sub_opts: |
| 168 if o == '-m': |
| 169 main_page_suffix = a |
| 170 elif o == '-e': |
| 171 error_page = a |
| 172 |
| 173 uri_args = self.args |
| 174 |
| 175 # Iterate over URIs, expanding wildcards, and setting the website |
| 176 # configuration on each. |
| 177 some_matched = False |
| 178 for uri_str in uri_args: |
| 179 for blr in self.WildcardIterator(uri_str): |
| 180 uri = blr.GetUri() |
| 181 if not uri.names_bucket(): |
| 182 raise CommandException('URI %s must name a bucket for the %s command' |
| 183 % (str(uri), self.command_name)) |
| 184 some_matched = True |
| 185 print 'Setting website config on %s...' % uri |
| 186 uri.set_website_config(main_page_suffix, error_page) |
| 187 if not some_matched: |
| 188 raise CommandException('No URIs matched') |
| 189 |
| 190 return 0 |
OLD | NEW |