예제 #1
0
    def set_credential_file(self, credential_file, region=None,
            authenticate=False):
        """
        Reads in the credentials from the supplied file. It should be
        a standard config file in the format:

        [rackspace_cloud]
        username = myusername
        api_key = 1234567890abcdef

        """
        self._creds_file = credential_file
        cfg = ConfigParser.SafeConfigParser()
        try:
            if not cfg.read(credential_file):
                # If the specified file does not exist, the parser will
                # return an empty list
                raise exc.FileNotFound("The specified credential file '%s' "
                        "does not exist" % credential_file)
        except ConfigParser.MissingSectionHeaderError as e:
            # The file exists, but doesn't have the correct format.
            raise exc.InvalidCredentialFile(e)
        try:
            self.username = cfg.get("rackspace_cloud", "username")
            self.api_key = cfg.get("rackspace_cloud", "api_key")
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
            raise exc.InvalidCredentialFile(e)
        if region:
            self._region = region
        if authenticate:
            self.authenticate()
예제 #2
0
    def set_credential_file(self, credential_file, region=None,
            tenant_id=None, authenticate=False):
        """
        Reads in the credentials from the supplied file. It should be
        a standard config file in the format:

        [keystone]
        username = myusername
        password = top_secret
        tenant_id = my_id

        """
        self._creds_file = credential_file
        cfg = ConfigParser.SafeConfigParser()
        try:
            if not cfg.read(credential_file):
                # If the specified file does not exist, the parser returns an
                # empty list.
                raise exc.FileNotFound("The specified credential file '%s' "
                        "does not exist" % credential_file)
        except ConfigParser.MissingSectionHeaderError as e:
            # The file exists, but doesn't have the correct format.
            raise exc.InvalidCredentialFile(e)
        try:
            self._read_credential_file(cfg)
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError) as e:
            raise exc.InvalidCredentialFile(e)
        if region:
            self.region = region
        if authenticate:
            self.authenticate()
예제 #3
0
파일: client.py 프로젝트: tvaught/pyrax
    def upload_file(self, container, file_or_path, obj_name=None,
            content_type=None, etag=None, return_none=False,
            content_encoding=None):
        """
        Uploads the specified file to the container. If no name is supplied,
        the file's name will be used. Either a file path or an open file-like
        object may be supplied. A StorageObject reference to the uploaded file
        will be returned, unless 'return_none' is set to True.
        """
        cont = self.get_container(container)

        def get_file_size(fileobj):
            """Returns the size of a file-like object."""
            currpos = fileobj.tell()
            fileobj.seek(0, 2)
            total_size = fileobj.tell()
            fileobj.seek(currpos)
            return total_size

        def upload(fileobj, content_type, etag, headers):
            if isinstance(fileobj, basestring):
                # This is an empty directory file
                fsize = 0
            else:
                fsize = get_file_size(fileobj)
            if fsize < self.max_file_size:
                # We can just upload it as-is.
                return self.connection.put_object(cont.name, obj_name,
                        contents=fileobj, content_type=content_type,
                        etag=etag, headers=headers)
            # Files larger than self.max_file_size must be segmented
            # and uploaded separately.
            num_segments = int(math.ceil(float(fsize) / self.max_file_size))
            digits = int(math.log10(num_segments)) + 1
            # NOTE: This could be greatly improved with threading or other
            # async design.
            for segment in xrange(num_segments):
                sequence = str(segment + 1).zfill(digits)
                seg_name = "%s.%s" % (fname, sequence)
                with utils.SelfDeletingTempfile() as tmpname:
                    with open(tmpname, "wb") as tmp:
                        tmp.write(fileobj.read(self.max_file_size))
                    with open(tmpname, "rb") as tmp:
                        # We have to calculate the etag for each segment
                        etag = utils.get_checksum(tmp)
                        self.connection.put_object(cont.name, seg_name,
                                contents=tmp, content_type=content_type,
                                etag=etag, headers=headers)
            # Upload the manifest
            headers["X-Object-Meta-Manifest"] = "%s." % fname
            return self.connection.put_object(cont.name, fname,
                    contents=None, headers=headers)

        ispath = isinstance(file_or_path, basestring)
        if ispath:
            # Make sure it exists
            if not os.path.exists(file_or_path):
                raise exc.FileNotFound("The file '%s' does not exist" %
                        file_or_path)
            fname = os.path.basename(file_or_path)
        else:
            fname = file_or_path.name
        if not obj_name:
            obj_name = fname

        headers = {}
        if content_encoding is not None:
            headers["Content-Encoding"] = content_encoding

        if ispath and os.path.isfile(file_or_path):
            # Need to wrap the call in a context manager
            with open(file_or_path, "rb") as ff:
                upload(ff, content_type, etag, headers)
        else:
            upload(file_or_path, content_type, etag, headers)
        if return_none:
            return None
        else:
            return self.get_object(container, obj_name)