Beispiel #1
0
  def _invoke_with_redirect(self, method, path, params=None, data=None, headers=None):
    """
    Issue a request, and expect a redirect, and then submit the data to
    the redirected location. This is used for create, write, etc.

    Returns the response from the redirected request.
    """
    next_url = None
    try:
      # Do not pass data in the first leg.
      self._root.invoke(method, path, params, headers=headers)
    except WebHdfsException as ex:
      # This is expected. We get a 307 redirect.
      # The following call may throw.
      next_url = self._get_redirect_url(ex)

    if next_url is None:
      raise WebHdfsException(_("Failed to create '%s'. HDFS did not return a redirect") % path)

    # Now talk to the real thing. The redirect url already includes the params.
    client = self._make_client(next_url, self.security_enabled, self.ssl_cert_ca_verify)

    # Make sure to reuse the session in order to preserve the Kerberos cookies.
    client._session = self._client._session
    if headers is None:
      headers = {}
    headers["Content-Type"] = 'application/octet-stream'
    return resource.Resource(client).invoke(method, data=data, headers=headers)
Beispiel #2
0
  def upload(self, file, path, *args, **kwargs):
    username = kwargs.get('username')
    if not username:
      raise WebHdfsException(_("Failed to upload file. WebHdfs requires a valid username to upload files."))

    dst = self.join(path, file.name)
    tmp_file = file.get_temp_path()

    self.do_as_user(username, self.rename, tmp_file, dst)
Beispiel #3
0
    Issue a request, and expect a redirect, and then submit the data to
    the redirected location. This is used for create, write, etc.

    Returns the response from the redirected request.
    """
    next_url = None
    try:
      # Do not pass data in the first leg.
      self._root.invoke(method, path, params)
    except WebHdfsException, ex:
      # This is expected. We get a 307 redirect.
      # The following call may throw.
      next_url = self._get_redirect_url(ex)

    if next_url is None:
      raise WebHdfsException(_("Failed to create '%s'. HDFS did not return a redirect") % path)

    # Now talk to the real thing. The redirect url already includes the params.
    client = self._make_client(next_url, self.security_enabled, self.ssl_cert_ca_verify)

    # Make sure to reuse the session in order to preserve the Kerberos cookies.
    client._session = self._client._session

    headers = {'Content-Type': 'application/octet-stream'}
    return resource.Resource(client).invoke(method, data=data, headers=headers)


  def _get_redirect_url(self, webhdfs_ex):
    """Retrieve the redirect url from an exception object"""
    try:
      # The actual HttpError (307) is wrapped inside