Пример #1
0
    def s3_connection(self):
        """
        Connect to the Amazon S3 API.

        If the connection attempt fails because Boto can't find credentials the
        attempt is retried once with an anonymous connection.

        Called on demand by :attr:`s3_bucket`.

        :returns: A :class:`boto.s3.connection.S3Connection` object.
        :raises: :exc:`.CacheBackendError` when the connection to the Amazon
                 S3 API fails.
        """
        if not hasattr(self, 'cached_connection'):
            self.check_prerequisites()
            with PatchedBotoConfig():
                import boto
                from boto.exception import BotoClientError, BotoServerError, NoAuthHandlerFound
                from boto.s3.connection import S3Connection, SubdomainCallingFormat, OrdinaryCallingFormat
                try:
                    # Configure the number of retries and the socket timeout used
                    # by Boto. Based on the snippet given in the following email:
                    # https://groups.google.com/d/msg/boto-users/0osmP0cUl5Y/X4NdlMGWKiEJ
                    if not boto.config.has_section(BOTO_CONFIG_SECTION):
                        boto.config.add_section(BOTO_CONFIG_SECTION)
                    boto.config.set(BOTO_CONFIG_SECTION,
                                    BOTO_CONFIG_NUM_RETRIES_OPTION,
                                    str(self.config.s3_cache_retries))
                    boto.config.set(BOTO_CONFIG_SECTION,
                                    BOTO_CONFIG_SOCKET_TIMEOUT_OPTION,
                                    str(self.config.s3_cache_timeout))
                    logger.debug("Connecting to Amazon S3 API ..")
                    endpoint = urlparse(self.config.s3_cache_url)
                    host, _, port = endpoint.netloc.partition(':')
                    kw = dict(
                        host=host,
                        port=int(port) if port else None,
                        is_secure=(endpoint.scheme == 'https'),
                        calling_format=(SubdomainCallingFormat()
                                        if host == S3Connection.DefaultHost
                                        else OrdinaryCallingFormat()),
                    )
                    try:
                        self.cached_connection = S3Connection(**kw)
                    except NoAuthHandlerFound:
                        logger.debug(
                            "Amazon S3 API credentials missing, retrying with anonymous connection .."
                        )
                        self.cached_connection = S3Connection(anon=True, **kw)
                except (BotoClientError, BotoServerError):
                    raise CacheBackendError("""
                        Failed to connect to the Amazon S3 API! Most likely your
                        credentials are not correctly configured. The Amazon S3
                        cache backend will be disabled for now.
                    """)
        return self.cached_connection
Пример #2
0
    def s3_bucket(self):
        """
        Connect to the user defined Amazon S3 bucket.

        Called on demand by :func:`get()` and :func:`put()`. Caches its
        return value so that only a single connection is created.

        :returns: A :class:`boto.s3.bucket.Bucket` object.
        :raises: :exc:`.CacheBackendDisabledError` when the user hasn't
                 defined :attr:`.Config.s3_cache_bucket`.
        :raises: :exc:`.CacheBackendError` when the connection to the Amazon
                 S3 bucket fails.
        """
        if not hasattr(self, 'cached_bucket'):
            self.check_prerequisites()
            with PatchedBotoConfig():
                from boto.exception import BotoClientError, BotoServerError, S3ResponseError
                # The following try/except block translates unexpected exceptions
                # raised by Boto into a CacheBackendError exception.
                try:
                    # The following try/except block handles the expected exception
                    # raised by Boto when an Amazon S3 bucket does not exist.
                    try:
                        logger.debug("Connecting to Amazon S3 bucket: %s",
                                     self.config.s3_cache_bucket)
                        self.cached_bucket = self.s3_connection.get_bucket(
                            self.config.s3_cache_bucket)
                    except S3ResponseError as e:
                        if e.status == 404 and self.config.s3_cache_create_bucket:
                            logger.info(
                                "Amazon S3 bucket doesn't exist yet, creating it now: %s",
                                self.config.s3_cache_bucket)
                            self.s3_connection.create_bucket(
                                self.config.s3_cache_bucket)
                            self.cached_bucket = self.s3_connection.get_bucket(
                                self.config.s3_cache_bucket)
                        else:
                            # Don't swallow exceptions we can't handle.
                            raise
                except (BotoClientError, BotoServerError):
                    raise CacheBackendError("""
                        Failed to connect to the configured Amazon S3 bucket
                        {bucket}! Are you sure the bucket exists and is accessible
                        using the provided credentials? The Amazon S3 cache backend
                        will be disabled for now.
                    """,
                                            bucket=repr(
                                                self.config.s3_cache_bucket))
        return self.cached_bucket
Пример #3
0
    def s3_connection(self):
        """
        Connect to the Amazon S3 API.

        If the connection attempt fails because Boto can't find credentials the
        attempt is retried once with an anonymous connection.

        Called on demand by :py:attr:`s3_bucket`.

        :returns: A :py:class:`boto.s3.connection.S3Connection` object.
        :raises: :py:exc:`.CacheBackendError` when the connection to the Amazon
                 S3 API fails.
        """
        if not hasattr(self, 'cached_connection'):
            from boto.exception import BotoClientError, BotoServerError, NoAuthHandlerFound
            from boto.s3.connection import S3Connection, SubdomainCallingFormat, OrdinaryCallingFormat
            try:
                logger.debug("Connecting to Amazon S3 API ..")
                endpoint = urlparse(self.config.s3_cache_url)
                host, _, port = endpoint.netloc.partition(':')
                is_secure = (endpoint.scheme == 'https')
                calling_format = SubdomainCallingFormat(
                ) if host == S3Connection.DefaultHost else OrdinaryCallingFormat(
                )
                try:
                    self.cached_connection = S3Connection(
                        host=host,
                        port=int(port) if port else None,
                        is_secure=is_secure,
                        calling_format=calling_format)
                except NoAuthHandlerFound:
                    logger.debug(
                        "Amazon S3 API credentials missing, retrying with anonymous connection .."
                    )
                    self.cached_connection = S3Connection(
                        host=host,
                        port=int(port) if port else None,
                        is_secure=is_secure,
                        calling_format=calling_format,
                        anon=True)
            except (BotoClientError, BotoServerError):
                raise CacheBackendError("""
                    Failed to connect to the Amazon S3 API! Most likely your
                    credentials are not correctly configured. The Amazon S3
                    cache backend will be disabled for now.
                """)
        return self.cached_connection