Esempio n. 1
0
File: _s3fs.py Progetto: miarec/s3fs
def s3errors(path):
    """Translate S3 errors to FSErrors."""
    try:
        yield
    except ClientError as error:
        _error = error.response.get('Error', {})
        error_code = _error.get('Code', None)
        response_meta = error.response.get('ResponseMetadata', {})
        http_status = response_meta.get('HTTPStatusCode', 200)
        error_msg = _error.get('Message', None)
        if error_code == 'NoSuchBucket':
            raise errors.ResourceError(path, exc=error, msg=error_msg)
        if http_status == 404:
            raise errors.ResourceNotFound(path)
        elif http_status == 403:
            raise errors.PermissionDenied(path=path, msg=error_msg)
        else:
            raise errors.OperationFailed(path=path, exc=error)
    except SSLError as error:
        raise errors.OperationFailed(path, exc=error)
    except EndpointConnectionError as error:
        raise errors.RemoteConnectionError(
            path,
            exc=error,
            msg="{}".format(error)
        )
Esempio n. 2
0
    def openbin(self, path, mode="r", buffering=-1, **options):
        _mode = Mode(mode)
        _mode.validate_bin()
        self.check()
        _path = self.validatepath(path)
        _key = self._path_to_key(_path)

        if _mode.appending:
            raise errors.ResourceError(path,
                                       msg="append mode is not supported")

        if _mode.create:
            if self.strict:
                try:
                    dir_path = dirname(_path)
                    if dir_path != "/":
                        _dir_key = self._path_to_dir_key(dir_path)
                        self._get_object(dir_path, _dir_key)
                except errors.ResourceNotFound:
                    raise errors.ResourceNotFound(path)

            try:
                info = self._getinfo(path)
            except errors.ResourceNotFound:
                pass
            else:
                if _mode.exclusive:
                    raise errors.FileExists(path)
                if self.strict and info.is_dir:
                    raise errors.FileExpected(path)

            obj = self.s3.Object(self._bucket_name, _key)
            return S3OutputFile(obj, upload_kwargs=self._get_upload_args(_key))

        if self.strict:
            info = self.getinfo(path)
            if info.is_dir:
                raise errors.FileExpected(path)

        obj = self.s3.Object(self._bucket_name, _key)
        return S3InputFile(obj)